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(); } }