我遇到我的按钮不起作用的问题。我以前使用过JButton,以前也没有遇到过问题。从外观上看,程序看起来像预期的那样。
有人可以告诉我为什么按钮不起作用吗?该类使用JDialog。
JButton cancel;
public CodeExample() {
setLayout(new FlowLayout(FlowLayout.RIGHT));
add(cancel = new JButton ("Cancel"));
setAlwaysOnTop(true);
setModal(true);
setVisible(true);
cancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
System.out.println("test");
}
});
}
正如《冷冻豌豆》的Roddy所指出的那样,您要做的最后一件事是使对话框可见。
这里的问题是这两行:
this.setModal(true);
this.setVisible(true);
如果对话框是模式对话框,则setVisible
它将阻塞,直到不再可见或不再显示该对话框为止。
这意味着setVisible
在用户单击红色的X关闭窗口后,所有后续操作都将执行。但是,此时对话框不再可见,您不再显示该对话框。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句