您的位置:首页 > 其它

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

2012-05-29 14:17 344 查看
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();
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: