简体   繁体   中英

Java: How to put timer in JOptionPane that's imported?

I import javax.swing.*; and have a JOptionPane:

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

I want this window to close after the amount of seconds the user inserts:

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

int timer1 = Integer.parseInt(time);

I do not know how to do this. Should I do something like this?

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

Thanks in advance!

Try this code:

            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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM