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,请高手指点。

解决方案 »

  1.   

    楼主为什么要用阻断模式调用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);
    }
    });
    }
    }
    }