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

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)

10-04 11:36