繁体   English   中英

如何停止执行Java代码并等待ActionEvent?

[英]How to stop executing Java code and wait for ActionEvent?

如果我要等待用户交互(在用户按下某个按钮时等待),只想确保这种方法是否正确:

SomeDialog someDialog = new SomeDialog(frame);

someDialog.setVisible(true);

try {

       synchronized (someDialog) {
          someDialog.wait();
       }

     } catch (InterruptedException ex) {
       Logger.getLogger(SomeDialog.class.getName()).log(Level.SEVERE, null, ex);
     }

int a = 0; a++; // <---- this line should not be processed before user clicks a button

接着:

someDialogButton.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {

    synchronized (SomeDialog.this) {
       SomeDialog.this.notify();
    }

  }
});

如果需要在执行其他操作之前等待用户处理对话框,请使用someDialog.setModal(true)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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