簡體   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