Java:如何将计时器放入导入的JOptionPane中?

时间:2015-09-17 11:50:14

标签: java swing timer joptionpane

我导入javax.swing.*;并拥有一个JOptionPane:

String meny = JOptionPane.showInputDialog(null, "What's the answer?").trim();

我希望在用户插入的秒数后关闭此窗口:

String time = JOptionPane.showInputDialog(null, "How much time").trim();

int timer1 = Integer.parseInt(time);

我不知道该怎么做。我应该这样做吗?

if (timer1 == 0) {
meny.setVisible(false);
}

提前致谢!

1 个答案:

答案 0 :(得分:0)

试试这段代码:

            JOptionPane pane = new JOptionPane("Message", JOptionPane.INFORMATION_MESSAGE);
            JDialog dialog = pane.createDialog(null, "Title");
            dialog.setModal(false);
            dialog.setVisible(true);

            new Timer(10000, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    dialog.setVisible(false);
                }
            }).start();