如果我想等待用户互动(等待用户按某个按钮时),请确保此方法是否正确:
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();
}
}
});
答案 0 :(得分:0)
如果您需要等待用户处理对话,请先使用someDialog.setModal(true)
。