我有一个JOptionPane
。如果用户在 10 分钟内没有单击它,则 JOptionPane
应自动单击“确定”。
我该怎么做?
最佳答案
首先,从 JOptionPane
对象创建一个 JDialog
。然后,创建一个运行 10 分钟(600,000 毫秒)的计时器,并在完成后处理该对话框。然后,从 JOptionPane
对象中检索所选值,如果对话框是由计时器处理的,请确保考虑到未初始化的值。
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.Timer;
public class Tester {
public static void main(String[] args) {
final JOptionPane pane = new JOptionPane("Hello world?", JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_CANCEL_OPTION);
final JDialog dialog = pane.createDialog(null, "Hello world");
Timer timer = new Timer(600000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
dialog.dispose();
}
});
timer.start();
dialog.setVisible(true);
dialog.dispose();
Integer choice = (Integer) (pane.getValue() == JOptionPane.UNINITIALIZED_VALUE ? JOptionPane.OK_OPTION : pane.getValue());
}
}
关于java - 如何自动点击JOptionPane的OK,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22277093/