Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 正在更新JOptionPane中的消息_Java_Swing_Jlabel_Joptionpane - Fatal编程技术网

Java 正在更新JOptionPane中的消息

Java 正在更新JOptionPane中的消息,java,swing,jlabel,joptionpane,Java,Swing,Jlabel,Joptionpane,我正在尝试制作一个数字时钟,它在一个JOptionPane中显示时间。我已经在消息对话框中显示了时间。但是,我不知道如何让它在对话框中每秒钟更新一次时间 这就是我目前拥有的: Date now = Calendar.getInstance().getTime(); DateFormat time = new SimpleDateFormat("hh:mm:ss a."); String s = time.format(now); JLabel label =

我正在尝试制作一个数字时钟,它在一个JOptionPane中显示时间。我已经在消息对话框中显示了时间。但是,我不知道如何让它在对话框中每秒钟更新一次时间

这就是我目前拥有的:

    Date now = Calendar.getInstance().getTime();
    DateFormat time = new SimpleDateFormat("hh:mm:ss a.");

    String s = time.format(now);

    JLabel label = new JLabel(s, JLabel.CENTER);
    label.setFont(new Font("DigifaceWide Regular", Font.PLAIN, 20));

    Toolkit.getDefaultToolkit().beep();

    int choice = JOptionPane.showConfirmDialog(null, label, "Alarm Clock", JOptionPane.YES_NO_OPTION, JOptionPane.PLAIN_MESSAGE);

那太可怕了,我认为应该更容易做到

基本上,你需要某种“股票代码”,你可以用它来更新标签的文本

public class OptionClock {

    public static void main(String[] args) {
        new OptionClock();
    }

    public OptionClock() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                Date now = Calendar.getInstance().getTime();
                final DateFormat time = new SimpleDateFormat("hh:mm:ss a.");

                String s = time.format(now);

                final JLabel label = new JLabel(s, JLabel.CENTER);
                label.setFont(new Font("DigifaceWide Regular", Font.PLAIN, 20));

                Timer t = new Timer(500, new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        Date now = Calendar.getInstance().getTime();
                        label.setText(time.format(now));
                    }
                });
                t.setRepeats(true);
                t.start();

                int choice = JOptionPane.showConfirmDialog(null, label, "Alarm Clock", JOptionPane.YES_NO_OPTION, JOptionPane.PLAIN_MESSAGE);

                t.stop();
            }
        });
    }
}
因为我们不想违反Swing的单线程规则,最简单的解决方案是使用
javax.Swing.Timer
,它大约每500毫秒滴答一次(捕捉边缘情况)


通过虚拟设置标签的文本,它会自动发布重新绘制请求,这使我们的生活变得简单…

所以你绝对没有试图更新时间的代码?