java – 调用方法和JOptionPane后允许代码继续执行所需的定时器或其他想法

我需要一种方法来允许我的程序在调用此方法后继续运行代码.
目前,它等待半小时,获取信息,将其存储到对象WeatherCard,并显示它,并重复.但它挂在JOptionPane上.我需要一种方法来使程序保持在JOptionPane下面或在大约10秒后关闭窗格.目前,我不知道如何使用我的代码

public void printWeatherCard(WeatherCard w, JFrame controlFrame) throws MalformedURLException, IOException{
    /* Displays a dialog box containing the temperature and location */
    BufferedImage img = ImageIO.read(new URL(w.imgSrc));
    ImageIcon icon = new ImageIcon(img);

    JOptionPane.showMessageDialog(controlFrame, "It is currently " + w.currentTemp + " \u00B0 F in " + w.location.city + ", " + w.location.state + ".\nCurrent humidity: " + w.currentHumidity + 
            "%.\nChance of precipitation: " + w.chancePrecip + "%.", "Weather Update: " + w.location.zipCode, JOptionPane.INFORMATION_MESSAGE, icon);
}

解决方法:

But it hangs on the JOptionPane. I need a way to make it so that the
program either keeps going underneath the JOptionPane or to close the
pane after about 10 seconds. I am not sure how to work either into my
code, currently

有两种方法

>(更容易,更舒服)创建JDialog(JFrame parent,boolean true),默认关闭操作HIDE_ON_CLOSE,只有一个JDialog作为局部变量,通过setVisible重用此Object(false / true)
> @ looping inside arrays of all JOptionPanes (all exists there untill current JVM is alive) by @kleopatra

上一篇:java – 在主窗口中切换JPanel


下一篇:java – 在JOptionPane中更新消息