Manning JavaSwing書中有一個例子:
myJFrame.setDefaultCloseOperation(
WindowConstants.DO_NOTHING_ON_CLOSE);
WindowListener l = new WindowAdapter() {
public void windowClosing(WindowEvent e) {
int confirm = JOptionPane.showOptionDialog(myJFrame,
"Really Exit?", "Exit Confirmation",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE,
null, null, null);
if (confirm == 0) {
myJFrame.dispose();
System.exit(0);
}
}
};
myJFrame.addWindowListener(l);
但是在JBuilderX下卻會報錯
修改程序如下:
this.setDefaultCloseOperation(
WindowConstants.DO_NOTHING_ON_CLOSE);
WindowListener l = new WindowAdapter() {
public void windowClosing(WindowEvent e) {
int confirm = JOptionPane.showOptionDialog(null,
"Really Exit?", "Exit Confirmation",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE,
null, null, null);
if (confirm == 0) {
System.exit(0);
}
}
};
this.addWindowListener(l);
才可以運行\n請問:1為什麼只有把myJFrame改為this才可以設置setDefaultCloseOperation?
2為什麼要把int confirm = JOptionPane.showOptionDialog(null,
"Really Exit?", "Exit Confirmation",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE,
null, null, null);
中的MyJFrame改為null才可以運行?
原來的MyJFrame會報錯?
JOptionPane.showOptionDialog()
中的第一項不是應該是component嗎?
有哪位高手可以給我詳細解答一下,十分感謝