Java JOptionPane.showInputDialog自动退出

Java JOptionPane.showInputDialog自动退出,java,swing,Java,Swing,有了这个, JOptionPane.showInputDialog(null,"Enter Your Name"); 我希望用户输入对话框自动退出。我的意思是在2或3秒内只显示JOptionPane用户输入对话框,然后关闭。它应该只自动关闭输入对话框,而不是整个程序。System.exit(0)方法会导致完整程序退出,但我只想关闭该输入对话框,而不是完整程序。然后我只需要Swing输入对话框,而不需要消息框和确认框。如中所示,您可以在属性更改侦听器中添加截取。基于此,下面的变体将标签、提示和文

有了这个,

JOptionPane.showInputDialog(null,"Enter Your Name");
我希望用户输入对话框自动退出。我的意思是在2或3秒内只显示
JOptionPane
用户输入对话框,然后关闭。它应该只自动关闭输入对话框,而不是整个程序。
System.exit(0)
方法会导致完整程序退出,但我只想关闭该输入对话框,而不是完整程序。然后我只需要Swing输入对话框,而不需要消息框和确认框。

如中所示,您可以在
属性更改侦听器中添加截取。基于此,下面的变体将
标签
提示
文本
字段添加到选项窗格中。一个Swing
计时器从
超时
开始倒计时,每次更新
标签
。当按下OK按钮或时间用完时,
PropertyChangeListener
会发送一个
窗口关闭
事件

如中所示,您可以在
PropertyChangeListener
中添加截取。基于此,下面的变体将
标签
提示
文本
字段添加到选项窗格中。一个Swing
计时器从
超时
开始倒计时,每次更新
标签
。当按下OK按钮或时间用完时,
PropertyChangeListener
会发送一个
窗口关闭
事件


究竟为什么会有一个应用程序。将输入姓名的时间限制为2-3秒@AndrewThompson因为我们讨厌用户@MadProgrammer很好地使用Unicode字符!下面是一个问题。为什么会有一个应用程序。将输入姓名的时间限制为2-3秒@AndrewThompson因为我们讨厌用户@MadProgrammer很好地使用Unicode字符!给你。
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.Timer;

/**
 * @see https://stackoverflow.com/a/44417958/230513
 * @see https://stackoverflow.com/a/12451673/230513
 */

public class JOptionTimeTest implements ActionListener, PropertyChangeListener {

    private static final int TIME_OUT = 10;
    private int count = TIME_OUT;
    private final Timer timer = new Timer(1000, this);
    private final JDialog dialog = new JDialog();
    private final JOptionPane optPane = new JOptionPane();
    private final JLabel label = new JLabel(message());
    private final JLabel prompt = new JLabel("Enter Your Name:");
    private final JTextField text = new JTextField("Name");

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            public void run() {
                new JOptionTimeTest().createGUI();
            }
        });
    }

    private void createGUI() {
        JFrame frame = new JFrame("Title");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        timer.setCoalesce(false);
        text.selectAll();
        Object[] array = {label, prompt, text};
        optPane.setMessage(array);
        optPane.setMessageType(JOptionPane.INFORMATION_MESSAGE);
        optPane.setOptionType(JOptionPane.DEFAULT_OPTION);
        optPane.addPropertyChangeListener(this);
        dialog.add(optPane);
        dialog.pack();
        frame.add(new JLabel(frame.getTitle(), JLabel.CENTER));
        frame.pack();
        frame.setVisible(true);
        dialog.setLocationRelativeTo(frame);
        dialog.setVisible(true);
        timer.start();
    }

    public void propertyChange(PropertyChangeEvent e) {
        String prop = e.getPropertyName();
        if (JOptionPane.VALUE_PROPERTY.equals(prop)) {
            thatsAllFolks();
        }
    }

    public void actionPerformed(ActionEvent e) {
        count--;
        label.setText(message());
        if (count == 0) {
            thatsAllFolks();
        }
        timer.restart();
    }

    private String message() {
        return "Closing in " + count + " seconds.";
    }

    private void thatsAllFolks() {
        dialog.setVisible(false);
        dialog.dispatchEvent(new WindowEvent(
            dialog, WindowEvent.WINDOW_CLOSING));
    }
}