WEB开发网      婵犵數濞€濞佳囧磹婵犳艾鐤炬い鎰堕檮閸嬬喐銇勯弽銊с€掗梻鍕閺岋箑螣娓氼垱笑闂佽姘﹂褔婀佸┑鐘诧工妤犲憡绂嶉崜褏纾奸弶鍫涘妼缁楁岸鏌熷畡鐗堝殗闁诡喒鏅犲畷褰掝敃閵堝棙顔忔繝鐢靛仦閸ㄥ爼骞愰幘顔肩;闁规崘绉ぐ鎺撳亹闁绘垶锕╁Λ鍕⒑閹肩偛濡奸悗娑掓櫇缁顓兼径妯绘櫇闂佹寧绻傞弻濠囨晝閸屾稓鍘甸柣搴㈢⊕閿氶柣蹇ョ稻缁绘繃绻濋崘銊т紝闂佽鍨伴崯鏉戠暦閻旂⒈鏁傞柛鈾€鏅欑槐妯衡攽閻愬樊鍤熷┑顔藉劤铻為柛鏇ㄥ墯閸欏繘鏌嶉崫鍕櫣缂佲偓婢跺绠鹃柟瀛樼箘閿涘秵顨ラ悙顏勭伈闁诡喖缍婂畷鎯邦槻婵℃彃顭烽弻娑㈠Ω閵夈儺鍔夌紓浣稿€哥粔褰掑极閹剧粯鏅搁柨鐕傛嫹 ---闂傚倷鐒︾€笛兠洪埡鍛闁跨噦鎷�
开发学院软件开发Java JFrame,JDialog,JInternalFrame关闭的拦截 阅读

JFrame,JDialog,JInternalFrame关闭的拦截

 2009-09-22 00:00:00 来源:WEB开发网 闂傚倷绶氬ḿ褍螞閹绢喖绠柨鐕傛嫹闂傚倷绀侀幉锟犲垂閻㈠灚宕查柟鎵閸庡秵銇勯幒鎴濃偓鐢稿磻閹炬枼妲堟繛鍡楃С濞岊亞绱撻崒姘扁枌闁瑰嚖鎷�婵犵數濮幏鍐川椤撴繄鎹曢梻渚€娼уú銈吤洪妸鈺佺劦妞ゆ帊鑳堕埊鏇㈡煏閸モ晛浠х紒杈╁仱閺佹捇鏁撻敓锟�闂傚倷绶氬ḿ褍螞閹绢喖绠柨鐕傛嫹  闂傚倷鑳舵灙缂佺粯顨呴埢宥夊即閵忕姵鐎梺缁樺姈椤愮厧鈽夊Ο閿嬬€婚梺褰掑亰閸撴稑鈻斿鑸碘拺闁告稑饪村▓鏃€绻涚仦鍌氬闁崇粯鎹囬獮瀣攽閹邦剚顔傛俊鐐€栧濠氬储瑜忛幉鎾晸閿燂拷
核心提示: 总的测试代码如下:importjava.awt.event.WindowEvent;importjavax.swing.JButton;importjavax.swing.JDesktopPane;importjavax.swing.JFrame;importjavax.swing.JInter

总的测试代码如下:

import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
/** *//***
 * JFrame和JInternalFrame关闭前先确认是否关闭
 * @author kissjava
 * www.blogjava.net/kissjava
 * **/
public class CloseFrameDemo extends JFrame{
    public CloseFrameDemo(){
        this.setTitle("CloseFrameDemo");
        jbInit();
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(800, 800);
        this.setVisible(true);
    }
    
    private void jbInit(){
        JDesktopPane desktop = new JDesktopPane();
        JInternalFrame internalFrame = this.createJInternalFrame();
        JButton btn = new JButton("DEMO");
        internalFrame.add(btn, "Center");
        desktop.add(internalFrame, JDesktopPane.PALETTE_LAYER);
        internalFrame.setSize(400,400);
        internalFrame.setVisible(true);
        this.add(desktop, "Center");
    }
    
    private JInternalFrame createJInternalFrame(){
        JInternalFrame internalFrame = new JInternalFrame("JInternalFrame Close Demo", true, true, true, true){
            /** *//***
             * 重写此方法让用户确认是否需要关闭
             * */
            @Override
            public void doDefaultCloseAction(){
                //关闭的提示选择
                int result= JOptionPane.showConfirmDialog(
                          this,
                          ("确认要关闭JInternalFrame吗?"),
                          ("关闭"),
                          JOptionPane.YES_NO_OPTION);
                  if(result == JOptionPane.YES_OPTION){
                      //如果选择YES,交由上层设置的关闭处理
                      super.doDefaultCloseAction();
                  }
            }
        };
        return internalFrame;
    }
    /** *//***
     * 重写此方法让用户确认是否需要关闭
     * */
    @Override
    protected void processWindowEvent(WindowEvent e) {
        boolean flag = false;
        
        if (e.getID() == WindowEvent.WINDOW_CLOSING) { 
          //关闭的提示选择
          int result= JOptionPane.showConfirmDialog(
                              this,
                              ("确认要关闭吗?"),
                              ("关闭"),
                              JOptionPane.YES_NO_OPTION);
              if(result == JOptionPane.NO_OPTION){
                  //不关闭,系统托盘????
                  flag = true;
              }else{
                  //关闭的处理
              }
        }
         if(!flag){
           //点击的了YES,那么交给上面去处理关闭的处理
            super.processWindowEvent(e);
          }
     }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        SwingUtilities.invokeLater(new Runnable(){
            public void run(){
                new CloseFrameDemo();
            }
        }) ;
    }
}

上一页  1 2 3 

Tags:JFrame JDialog JInternalFrame

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接