在调用方法和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); }
但它挂在JOptionPane上。 我需要一种方法来使程序保持在JOptionPane下面或在大约10秒后关闭窗格。 我目前还不确定如何使用我的代码
有两种方法
-
(更容易,更舒服)创建
JDialog(JFrame parent, boolean true)
,默认关闭操作HIDE_ON_CLOSE
,只有一个JDialog
作为local variabl
,通过setVisible(false/true)
重用此Object
setVisible(false/true)
-
@kleopatra在所有JOptionPanes的数组内部循环(所有存在,直到当前的JVM
@kleopatra
活动状态)
在一些延迟之后关闭modal dialog并更新modal dialog后面的显示是不同的问题。
-
在此示例中 ,
javax.swing.Timer
用于标记时间,当计数器达到零或用户将其解除时,对话框将关闭。 -
modal dialog仅阻止用户交互。 在此示例中添加模式对话框,以查看GUI更新是否继续以响应
javax.swing.Timer
。public void run() { ... f.setVisible(true); JOptionPane.showMessageDialog(dt, TITLE); }
JOptionPane是模态的,这意味着代码执行块直到用户解除或确认它为止。 您需要使用非modal dialog。 考虑创建自己的JDialog。 http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html