Java 允许代码在调用方法和JOptionPane后继续执行所需的计时器或其他想法
我需要一种方法,允许我的程序在调用此方法后继续运行代码。 目前,它等待半小时,获取信息,将其存储到对象WeatherCard,并显示它,然后重复。但它挂在JOptionPane上。我需要一种方法,使它使程序要么继续在JOptionPane下运行,要么在大约10秒后关闭窗格。目前,我不确定如何在我的代码中使用这两种方法Java 允许代码在调用方法和JOptionPane后继续执行所需的计时器或其他想法,java,swing,timer,joptionpane,Java,Swing,Timer,Joptionpane,我需要一种方法,允许我的程序在调用此方法后继续运行代码。 目前,它等待半小时,获取信息,将其存储到对象WeatherCard,并显示它,然后重复。但它挂在JOptionPane上。我需要一种方法,使它使程序要么继续在JOptionPane下运行,要么在大约10秒后关闭窗格。目前,我不确定如何在我的代码中使用这两种方法 public void printWeatherCard(WeatherCard w, JFrame controlFrame) throws MalformedURLExcept
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秒后打开窗格。我也不知道如何将这两种方法运用到我的工作中
代码,当前
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);
}
有两种方法
- (更容易、更舒适)创建
,使用默认的关闭操作JDialog(JFrame parent,boolean true)
,只有一个HIDE\u ON\u close
作为JDialog
e,通过local variabl
重用此setVisible(false/true)
对象
- 作者:
@kleopatra
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);
}
有两种方法
- (更容易、更舒适)创建
,使用默认的关闭操作JDialog(JFrame parent,boolean true)
,只有一个HIDE\u ON\u close
作为JDialog
e,通过local variabl
重用此setVisible(false/true)
对象
- 作者:
@kleopatra
- 在这种情况下,使用
来标记时间,当计数器达到零或用户取消计数器时,对话框关闭javax.swing.Timer
- 模式对话框仅阻止用户交互。向该对话框添加一个模式对话框,以查看GUI更新是否继续响应
javax.swing.Timer
public void run() { ... f.setVisible(true); JOptionPane.showMessageDialog(dt, TITLE); }
- 在这种情况下,使用
来标记时间,当计数器达到零或用户取消计数器时,对话框关闭javax.swing.Timer
- 模式对话框仅阻止用户交互。向该对话框添加一个模式对话框,以查看GUI更新是否继续响应
javax.swing.Timer
public void run() { ... f.setVisible(true); JOptionPane.showMessageDialog(dt, TITLE); }
JOptionPane
本身不会这样做。对不起,我没有领会你的意思。我怀疑@Nick的“customJDialog
”意味着一个非模态对话框,例如。模态对话框只会阻止用户交互,而不会阻止执行。@trashgod-它会阻止当前线程上的执行。在代码中,Nick发布了他的程序正在等待JOptionPane的消息,JOptionPane停止了他的代码的执行。在Jframe、random bubbdle方法或action listener代码中的setVisible之前放置一个模态对话框,所有这些都会停止示例的执行。如果将其放置在paintComponent代码中,您将看到重复的弹出窗口,但paintComponent代码每次都会暂停。在每个实例中,当前线程都在等待输入,并且在用户确认模式对话框之前不会执行代码。我怀疑将它放在setVisible之后不会阻塞,因为此时计时器已经在运行,并且RepaitManager正在拦截所有的重新绘制调用。当然可以阻塞EDT,但JOptionPane
本身不会这样做。对不起,我没有领会你的意思。我怀疑@Nick的“customJDialog
”意味着一个无模式对话框,例如。我决定使用一个自定义JDialog(我认为它需要在我的程序中进行大量的修改)。非常感谢你!我决定使用一个定制的JDialog(我认为它需要在我的程序中进行大量的修改)。非常感谢你!