Topic: 關於JOption Pane的問題

  Print this page

1.關於JOption Pane的問題 Copy to clipboard
Posted by: hundred
Posted on: 2005-03-14 10:54

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嗎?
有哪位高手可以給我詳細解答一下,十分感謝

2.Re:關於JOption Pane的問題 [Re: hundred] Copy to clipboard
Posted by: cnfree
Posted on: 2005-03-14 14:11

你的MyJFrame怎么写的?不会又是抄错例子的吧

3.Re:關於JOption Pane的問題 [Re: hundred] Copy to clipboard
Posted by: hundred
Posted on: 2005-03-14 15:26

沒有啊,我的JFrame名字是FrameMain
在程序中已經更改了,程序是原文拷貝的,將MyJFrame更改為FrameMain後還是報錯,FrameMain後面加" . "後程序認出來的下拉單中沒有setdefaultcloseoption這個選項,只有改成this後才有,所以不太明白為什麼.

4.Re:關於JOption Pane的問題 [Re: hundred] Copy to clipboard
Posted by: hundred
Posted on: 2005-03-14 16:41

hehe
歐自己搞定了
原來是要在FrameMain中創建
JFrame FrameMain
然後構造方法中
實例化一下
public FrameMain(JFrame FrameMain) {

enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch (Exception e) {
e.printStackTrace();
}
this.FrameMain=FrameMain;
}
這樣就可以引用參數FrameMain了
謝謝版主


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923