java同时关闭两个窗口_在一个窗口中同时关闭多个窗口的问题(Swing中事件多点传送的问题) | 学步园...

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

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值