操作侦听器在模式 JDialog 中不起作用



我为模态对话框编写了自己的类,但是当我从代码中调用它时,单击按钮没有任何反应。如果我定义setModal(false)一切都很好。我想并发性有一些问题,但我不确定。我的错误在哪里?

public class PauseTaskDialog extends JDialog {
private JPanel contentPane;
private JButton buttonOK;
private JButton buttonCancel;
private JCheckBox prioritisingCheckBox;
private JCheckBox simultaneousWorkCheckBox;
private JCheckBox problemsWithDataCheckBox;
private JTextArea comment;
private String taskID;
public PauseTaskDialog(String task) {
    this.setContentPane(contentPane);
    this.setModal(true);
    this.setLocationRelativeTo(null);
    this.pack();
    this.setTitle("Task pause reasons");
    this.taskID = task;
    comment.setFont(comment.getFont().deriveFont(14f));
    comment.setLineWrap(true);
    comment.setWrapStyleWord(true);
    buttonOK.addActionListener(e -> {
        onOK();
    });
    buttonCancel.addActionListener(e -> {
        onCancel();
    });
    this.setVisible(true);
}

private void onOK() {
    // some code here        
}
private void onCancel() {
    // some code there
}
}

我以这种方式从我的代码调用对话框:

PauseTaskDialog dialog = new PauseTaskDialog(taskID);

从文档中:

注意:更改可见对话框的模式可能不起作用,直到它被隐藏然后再次显示。

尝试在setVisible之前致电setModal(true)

但是setModal已被弃用,您应该改为调用setModalityType(您需要的类型可能是APPLICATION_MODAL(,请查看本教程。它与 JButton 侦听器不工作无关,如果您可以单击 JButton,则意味着您正在运行它们的侦听器(如果有(,如果您无法单击它们(JButton 有动画显示它们正在被单击(,那么它们是隐藏的/不在前面,这与并发无关。

最新更新