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();
}
}
總結
以上是生活随笔為你收集整理的java同时关闭两个窗口_在一个窗口中同时关闭多个窗口的问题(Swing中事件多点传送的问题) | 学步园...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php 内容编码错误,PHP输出缓冲,o
- 下一篇: 按钮点击打开新页面_PDF怎么打开?如何