在(新)GUI等待输入时保持代码空闲

时间:2017-07-19 21:23:57

标签: java user-interface user-input wait

我正在尝试获取用户电子邮件以验证它是否是用户。在此之前,我运行一系列测试,看看用户是否确实需要输入电子邮件。我正在努力创建框架一次,并等待用户将他们的电子邮件输入到必填字段。

如果窗口没有生成并且占用了所有可用的处理能力,会发生什么。我尝试了wait() / notify()和计时器,但都没有做我需要的。

是否有更好的方法(在if / else块内)生成窗口并在做其他事情之前等待用户输入?

public class Security extends JFrame{

   //Code stuff here to see if this is needed

   String email = "";
   boolean windowCreation = true;

   do{
       do{
           EventQueue.invokeLater(new Runnable() {
                public void run() {
                   try {
                       Security frame = new Security();
                       frame.setVisible(true); 
                       windowCreation = false;
                   } catch (Exception e) {
                       e.printStackTrace();
                   }
               }
           });
       }while(windowCreation);
   } while(!email.equals(""));

   //More code stuff here to use "email" the way I need to 
}

以下是窗口正在做的事情

 Security()
 {
    getContentPane().setLayout(null);

    textField = new JTextField();
    textField.addKeyListener(new KeyAdapter() {
        public void keyPressed(KeyEvent key) {
            if(key.getKeyCode() == KeyEvent.VK_ENTER)
            {
                Security.email = field.getText();
            }
        }
    });
    textField.setBounds(10, 115, 415, 20);
    getContentPane().add(textField);
    textField.setColumns(10);

}

0 个答案:

没有答案