在调用方法和 JOptionPane 后允许代码继续执行所需的计时器或其他想法 [英] Timer or other idea required to allow code to continue execution after calling method and JOptionPane

查看:9
本文介绍了在调用方法和 JOptionPane 后允许代码继续执行所需的计时器或其他想法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一种方法来让我的程序在调用此方法后继续运行代码.

目前,它等待半小时,获取信息,将其存储到对象 WeatherCard,并显示它,然后重复.但它挂在 JOptionPane 上.我需要一种方法来使程序要么继续在 JOptionPane 下运行,要么在大约 10 秒后关闭该窗格.我不确定如何在我的代码中工作,目前

I need a way to allow my program to keep running code after this method is called.

Currently, it waits for half an hour, gets the info, stores it to the object WeatherCard, and displays it, and repeats. 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

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 + ".
Current humidity: " + w.currentHumidity + 
            "%.
Chance of precipitation: " + w.chancePrecip + "%.", "Weather Update: " + w.location.zipCode, JOptionPane.INFORMATION_MESSAGE, icon);
}

推荐答案

但它挂在 JOptionPane 上.我需要一种方法来做到这一点程序要么继续在 JOptionPane 下运行,要么关闭约 10 秒后窗格.我不知道如何在我的代码,目前

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 作为local variable,通过setVisible(false/true)

  • (better easier, comfortable) create JDialog(JFrame parent, boolean true), with default close operation HIDE_ON_CLOSE, only one JDialog as local variable, reuse this Object by setVisible(false/true)

在所有 JOptionPanes 的数组内循环(所有这些都存在直到当前 JVM 处于活动状态) by @kleopatra

这篇关于在调用方法和 JOptionPane 后允许代码继续执行所需的计时器或其他想法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆