现在的位置: 首页 > 综合 > 正文

在一个窗口中同时关闭多个窗口的问题(Swing中事件多点传送的问题)

2018年01月30日 ⁄ 综合 ⁄ 共 1388字 ⁄ 字号 评论关闭

class   A{}
A中包含有:
JFrame   frameA;
JButton   btnA;

class   B{}
B中包含有:
final   JFrame   frameB;
Static   boolean   wantToSave   =   true;
frameB.addWindowListener(   new   WindowAdapter()
{  
pubilic   void   windowClosing(windowEvent   we)
{JDialog   dlg   }
...........
}
);

问题描述:实现的功能是每点击一次btnA则弹出一个frameB(即一个class   B的instance被new)。当要关闭frameB的时候dlg弹出提示我选择Yes/No/No   to   all,当选No   to   all的时候(此时将wantToSave设成false)class   B的全部instance的frameB都能够关闭。

实现方法代码如下:

 

package com.mansuo.test;
import   java.awt.*; 
import   java.awt.event.*; 
import   javax.swing.*; 
/**
 * 多窗口关闭
 * @author Administrator
 *
 */
public   class   CloseAll   extends   Frame   implements   ActionListener   { 
        public   CloseAll()   { 
                super( "title "); 
                setSize(300,   200); 
                addWindowListener(new   WindowAdapter()   { 
                        public   void   windowClosing(WindowEvent   ew)   { 
                                System.exit(0); 
                        } 
                }); 
                JPanel   p   =   new   JPanel(); 
                p.setLayout(new   FlowLayout(FlowLayout.CENTER)); 
                newButton   =   new   JButton( "new "); 
                p.add(newButton); 
                newButton.addActionListener(this); 
                closeButton   =   new   JButton( "close "); 
                p.add(closeButton); 
                
                add(p,   BorderLayout.NORTH); 

        } 

        public   void   actionPerformed(ActionEvent   e)   { 
            
                NewFrame   f   =   new   NewFrame(); 
                  f.show(); 
              closeButton.addActionListener(f); 

        } 

        public   static   void   main(String   args[])   { 
                CloseAll   c   =   new   CloseAll(); 
                c.show(); 
        } 

        private   JButton   closeButton; 
        private   JButton   newButton; 
} 

class   NewFrame   extends   JFrame   implements   ActionListener   { 
	static int   counter=1; 
        public   NewFrame()   { 
                    
                          setTitle( "titile "   +   counter++); 
                                setSize(300,   200); 
                      setLocation(30   *   counter,   30   *   counter); 

        } 
        public   void   actionPerformed(ActionEvent   e){ 
                dispose(); 
        } 


} 

抱歉!评论已关闭.