A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© cehkongfu 中级黑马   /  2012-12-10 20:30  /  1290 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 cehkongfu 于 2012-12-10 20:35 编辑

package cn.module.gui.对话框;
import java.awt.*;
import java.awt.event.*;
public class TestDialog {
TextField tf = new TextField(10);
Button b1 = new Button("模态显示");
Button b2 = new Button("非模态显示");
Frame f = new Frame("对话框");
Button b3 = new Button("确定");
Dialog dlg = new Dialog(f, "对话框", true);
//第三个参数为true时,可以操作其他组件以及写入数据,为false时,
//则对其他组件不能进行任何操作
FlowLayout fl = new FlowLayout();
TestDialog() {
  f.setLayout(fl);
  f.add(tf);
  f.add(b1);
  f.add(b2);
  
  
  b1.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    dlg.setModal(true);
    dlg.setTitle("模态测试");
    dlg.setVisible(true);
    try {Thread.sleep(5000);}catch(Exception ee) {System.out.println(ee);}
    dlg.setVisible(false);
    tf.setText("模态测试");
   }
  });
  b2.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    dlg.setModal(false);
    dlg.setTitle("非模态测试");
    dlg.setVisible(true);
    try {Thread.sleep(5000);}catch(Exception ee) {System.out.println(ee);}
    dlg.setVisible(false);
    tf.setText("非模态测试");
   }
  });
  //?????????????????????????????????????????????????????????????????????????????????
  //对比以上两个按钮所触发的事件,第二个(b2)5秒后对话框可以消失,而第一个(b)却不可以,Why?
  //????????????????????????????????????????????????1?????????????????????????????????
  
  f.setBounds(0, 0, 400, 200);
  f.setVisible(true);
  f.addWindowListener(new WindowAdapter() {
   public void windowClosing(WindowEvent e) {
    System.exit(0);
   }
  });
  dlg.setLayout(fl);
  dlg.add(b3);
  b3.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    dlg.dispose();
    dlg.setVisible(false);
    //以上两行代码产生的效果一样,有关联吗?
   }
  });
  dlg.setBounds(0, 0, 200, 150);
}
public static void main(String[] args) {
  new TestDialog();
}
}

那位高手帮忙查下代码中注释的问题(通过查找“Why?”可以看到问题),非常感谢你的时间!

评分

参与人数 1技术分 +1 收起 理由
冯海霞 + 1

查看全部评分

1 个回复

倒序浏览
您需要登录后才可以回帖 登录 | 加入黑马