java中如何设置Dialog关闭

lihongxu2010 2010-12-04 10:17:58
import java.awt.*;
import java.awt.event.*;
public class TestWindowClosing{
public static void main(String[] args) {
new FrameWindow("HELLO");
}
}
class FrameWindow extends Frame {
public static int i = 1; //设置计数器,计算窗口的数量,当界面没有显示窗口时退出系统
FrameWindow(String s) {
super(s);
setLayout(new FlowLayout());
setBackground(Color.yellow);
setBounds(300,300,400, 600);
setVisible(true);
Button bt = new Button("点击弹出窗口");
this.add(bt);
bt.addActionListener(new ButtonActionL());
this.addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
i--;//每调用一次关闭事件,窗口数量减少1
setVisible(false);
if(i == 0) {
System.exit(0);
} else {
}
}
});
}
class ButtonActionL implements ActionListener {
public void actionPerformed(ActionEvent e) {
i++; //每调用一次,窗口数增1
FrameWindow c = new FrameWindow("子窗口");
c.setBounds(200,200,200,200);
c.setBackground(Color.blue);
c.setVisible(true);
Dialog a = new Dialog(c,true);
a.setVisible(true);
a.addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
setVisible(false);
//System.exit(-1);
}
});
}
}
}
为什么无法关闭Dialog,请高手指点。
...全文
769 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
lihongxu2010 2010-12-05
  • 打赏
  • 举报
回复
还是不行,无法关闭
hezhiyonging 2010-12-05
  • 打赏
  • 举报
回复
楼上应该正解
sunyiz 2010-12-04
  • 打赏
  • 举报
回复
楼主为什么要用阻断模式调用dialog?

这样试试
import java.awt.*;
import java.awt.event.*;

public class TestWindowClosing {
public static void main(String[] args) {
new FrameWindow("HELLO");
}
}

class FrameWindow extends Frame {
public static int i = 1; // 设置计数器,计算窗口的数量,当界面没有显示窗口时退出系统

FrameWindow(String s) {
super(s);
setLayout(new FlowLayout());
setBackground(Color.yellow);
setBounds(300, 300, 400, 600);
setVisible(true);
Button bt = new Button("点击弹出窗口");
this.add(bt);
bt.addActionListener(new ButtonActionL());
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
i--;// 每调用一次关闭事件,窗口数量减少1
setVisible(false);
if (i == 0) {
System.exit(0);
} else {
}
}
});
}

class ButtonActionL implements ActionListener {
public void actionPerformed(ActionEvent e) {
i++; // 每调用一次,窗口数增1
FrameWindow c = new FrameWindow("子窗口");
c.setBounds(200, 200, 200, 200);
c.setBackground(Color.blue);
c.setVisible(true);
c.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
i--;// 每调用一次关闭事件,窗口数量减少1
setVisible(false);
if (i == 0) {
System.exit(0);
} else {
}
}
});
final Dialog a = new Dialog(c);
a.setVisible(true);
a.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
a.dispose();
// setVisible(false);
// System.exit(-1);
}
});
}
}
}

62,634

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧