日期:2014-05-20  浏览次数:20731 次

Dialog的模态问题
package mycheckbox;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MyDialog extends Frame
{
 private Button but1,but2,but3;
 private TextField tf;
 private Dialog dl;
 private static MyDialog md;
public static void main(String[] args)  
{
 md=new MyDialog();
}
MyDialog()
{
this.setLayout(new FlowLayout());
but1 =new Button("静态模式");
this.add(but1);
but1.addActionListener(new Girl());
but2 =new Button("非静态模式");
this.add(but2);
but2.addActionListener(new Girl());
tf=new TextField(30);
this.add(tf);
dl=new Dialog(md,"对话框",true);
but3=new Button("退出");
dl.add(but3);
dl.setSize(200, 300);
but3.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent e) {
  dl.dispose();
  }
  });
this.setSize(300, 350);
this.setVisible(true);
}

class Girl implements ActionListener
{
  public void actionPerformed(ActionEvent e) {
  if(e.getSource()==but1)
  { tf.setText("静态模式");
  dl.setVisible(true);
  dl.setModal(true);
   
  }
  else
  if(e.getSource()==but2)
  {tf.setText("非静态模式");
  dl.setVisible(true);
  dl.setModal(false);  
  }
  }

}
}

上面是我写的程序,但是为什么两个都是模态对话框?怎么dl.setModal(false);这句话并没有其作用呢?是不是和执行的先后顺序有关?
希望高手教我!

------解决方案--------------------
只要是模态对话框
在 setVisible(true); 之后的语句将会被阻塞
直到这个对话框不可见为止

你可以放过来写
dl.setModal(false);
dl.setVisible(true);