在一个窗口中同时关闭多个窗口的问题(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都能够关闭。
实现方法代码如下:
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(); } }
相关文章推荐
- IE6中,一个Button同时打开两个下载窗口,并且可以自动关闭
- ASP.NET关闭当前窗口同时打开一个新窗口
- [js综合问题汇总]js窗口关闭事件,表单名称,父窗口子窗口,var变量名
- MFC中按下Button按钮,弹出一个窗口的同时关闭本窗口
- ASP.NET关闭当前窗口同时打开一个新窗口
- ASP.NET关闭当前窗口同时打开一个新窗口
- 关闭模态子窗口的同时刷新父窗口的问题
- C#关闭一个窗口的同时打开另一个窗口
- javascript实现点击按钮弹出一个可关闭层窗口同时网页背景变灰的方法
- 使用window.close关闭窗口后,出现一个新的页面问题
- C#Form打开一个新窗口的同时关闭这个窗体
- 如何使一个窗口关闭的同时另一个窗口刷新
- C#Form打开一个新窗口的同时关闭这个窗体
- 点击关闭窗口时,弹出提醒的一个事件
- 问题: 写一个多线程程序实现铁路卖票系统,4个窗口同时售票,线程同步
- C#如何关闭一个窗口的同时打开另一个窗口
- C#关闭一个窗口的同时打开另一个窗口
- C#关闭一个窗口的同时打开另一个窗口
- ASP.NET关闭当前窗口同时打开一个新窗口
- 关于子窗口关闭同时刷新父窗口问题详解