java - JDialog 消失得太早,用户无法查看它包含的信息

标签 java swing timer jsoup jdialog

当我尝试显示包含正确信息(通过自定义对象 WeatherCard 输入)的 JDialog 时,它在创建后立即消失。我是否错过了什么,或者我只是捏造了太多东西?

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);
    JPanel panel = new JPanel();
    panel.setLayout(new GridLayout(0, 1));
    Label lb = new Label("It is currently " + w.currentTemp + " \u00B0 F in " + w.location.city + ", " + w.location.state + ".\nCurrent humidity: " + w.currentHumidity);
    panel.add(lb);
    final JDialog dialog = new JDialog(controlFrame, "Weather Update: " + w.location.zipCode);
    dialog.setSize(500,500);
    dialog.add(panel);
    dialog.pack();
    Timer timer = new Timer(10000, new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            dialog.setVisible(false);
            dialog.dispose();
        }
    });
    timer.setRepeats(false);
    timer.start();

    dialog.setVisible(true);
}

最佳答案

你的方法适用于此 complete example以下。您的结果可能是由于忽略在 event dispatch thread 上构建 GUI 而造成的。 。另外,在对话框可见后start() Timer

plain image

还要考虑这个example显示剩余时间。

timed image

import java.awt.EventQueue;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

/**
 * @see https://stackoverflow.com/a/19003038/230513
 */
public class Test {

    static class TestAction extends AbstractAction {

        private final JFrame f;

        public TestAction(String name, JFrame f) {
            super(name);
            this.f = f;
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            JPanel panel = new JPanel();
            Label label = new Label("It is currently sunny & warm somewhere.");
            panel.add(label);
            final JDialog dialog = new JDialog(f, "Weather Update");
            dialog.add(panel);
            dialog.pack();
            dialog.setLocationRelativeTo(f);
            dialog.setVisible(true);
            Timer timer = new Timer(5000, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    dialog.setVisible(false);
                    dialog.dispose();
                }
            });
            timer.setRepeats(false);
            timer.start();
        }
    }

    private void display() {
        JFrame f = new JFrame("Test");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(new JButton(new TestAction("Test", f)));
        f.pack();
        f.setLocationByPlatform(true);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Test().display();
            }
        });
    }
}

关于java - JDialog 消失得太早,用户无法查看它包含的信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19000024/

相关文章:

java - hibernate 3 : unable to query PostgreSQL database

java - 两个 AVL 树的替代方案

java - 单击 JmenuItem 时更新边框标题

c++ - 错误 : ‘struct sigevent’ has no member named ‘sigev_notify_thread_id’

Java - 更新现有秒表代码以包括倒计时

Java War 文件不会在 Eclipse 中使用 WildFly 进行更新

java - 使用正则表达式匹配单词 '90%'

java - 从 JTable 中的排序中排除列

java - 用 Java 构建棋盘

swift - 我在 SpriteKit 上的 scheduledTimer 有问题