为什么去掉 setvisible(false) 程序就不能正常运行呢?

nikankan88 2009-04-27 08:36:31
import java.awt.event.*; import java.awt.*;
class MyDialog extends Dialog implements ActionListener //建立对话框类
{ static final int YES=1,NO=0;
int message=-1; Button yes,no;
MyDialog(Frame f,String s,boolean b) //构造方法
{ super(f,s,b);
yes=new Button("Yes"); yes.addActionListener(this);
no=new Button("No"); no.addActionListener(this);
setLayout(new FlowLayout());
add(yes); add(no);
setBounds(60,60,100,100);
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{ message=-1;setVisible(false);
}
}
);
}
public void actionPerformed(ActionEvent e)
{ if(e.getSource()==yes)
{ message=YES;
setVisible(false);
}
else if(e.getSource()==no)
{ message=NO;
setVisible(false);
}
}
public int getMessage()
{ return message;
}
}
class Dwindow extends Frame implements ActionListener
{ TextArea text; Button button; MyDialog dialog;
Dwindow(String s)
{ super(s);
text=new TextArea(5,22); button=new Button("打开对话框");
button.addActionListener(this);
setLayout(new FlowLayout());
add(button); add(text);
dialog=new MyDialog(this,"我有模式",true);
setBounds(60,60,300,300); setVisible(true);
validate();
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{ System.exit(0);
}
}
);
}
public void actionPerformed(ActionEvent e)
{ if(e.getSource()==button)
{ dialog.setVisible(true); //对话框激活状态时,堵塞下面的语句
//对话框消失后下面的语句继续执行:
if(dialog.getMessage()==MyDialog.YES) //如果单击了对话框的"yes"按钮
{ text.append("\n你单击了对话框的yes按钮");
}
else if(dialog.getMessage()==MyDialog.NO) //如果单击了对话框的"no"按钮
{ text.append("\n你单击了对话框的No按钮");
}
}
}
}
public class Example8_1
{ public static void main(String args[])
{ new Dwindow("带对话框的窗口");
}
}





我什么红色字体两句话去掉后程序不能正常运行呢???
我想让对话框一直显示
不知道哪位高手大哥帮我解决一下这个问题
...全文
348 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
nikankan88 2009-04-29
  • 打赏
  • 举报
回复
哥 ,我照着你给改的程序试了一下,可是要点击两次确定按钮 才能在text 里显示 内容啊???
你知道为什么会造成这种原因吗???
sjkof 2009-04-28
  • 打赏
  • 举报
回复
对话框分模态和非模态,
模态对话框能阻塞程序,不让后续程序运行和父窗口获得焦点
nikankan88 2009-04-28
  • 打赏
  • 举报
回复
什么意思?能更具体点吗???为什么说设置模态 太能堵塞???
qybao 2009-04-28
  • 打赏
  • 举报
回复
在 MyDialog构造函数中加入
setModal(true);
setVisible(true);

把setVisible(false); 改成dispose()

Dwindow中

dialog=new MyDialog(this,"我有模式",true);
这行放到
dialog.setVisible(true); //对话框激活状态时,堵塞下面的语句
之前
构造函数不要直接就把模态对话框创建


GeekZFZ 2009-04-28
  • 打赏
  • 举报
回复
学习一下
qybao 2009-04-27
  • 打赏
  • 举报
回复
你的Dialog要调用setModal(true);设置模态太能堵塞
gongfuliang 2009-04-27
  • 打赏
  • 举报
回复
Dialog为模态

62,614

社区成员

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

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