0

当我尝试让我的 JDialog 包含正确的信息(通过我的自定义对象 WeatherCard 输入)来显示时,它会在创建后立即消失。我错过了什么还是我只是太用力地捏造它?

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);
}
4

1 回答 1

2

您的方法适用于下面这个完整的示例。您的结果可能是忽略在事件调度线程上构建 GUI 的产物。此外,start()对话框Timer之后是可见的。

普通图像

还要考虑这个显示剩余时间的例子。

定时图像

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();
            }
        });
    }
}
于 2013-09-25T10:57:25.477 回答