[请教]关于一个Dialog的小问题

Layout 2003-05-27 01:34:04
如果我在dialog(JDialog)上面添加了一个button(JButton)
如何让我在点击这个button的时候注销这个dialog?
当然不能用system.exit();
我的意思就是让这个dialog不可见(或者dispose())
让父容器显示出来:
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
/////请教大侠~~~~!

}
});

System.out.println("感谢大侠相救,没齿难忘");
...全文
17 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
helpall 2003-05-27
  • 打赏
  • 举报
回复
Or more fun:

class DlgMain extends JDialog {
JButton btn = new JButton("create child");
DlgKid xx1;
public DlgMain() {
this.getContentPane().add(btn);
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(btn.getText().indexOf("create") != -1) {
xx1 = new DlgKid();
xx1.setVisible(true);
btn.setText("kill child");
}else {
btn.setText("create child");
xx1.dispose();
}
}
});
this.show();
}
public static void main(String[] args) {
DlgMain xx = new DlgMain();
xx.setSize(200,200);
xx.pack();

}
}
class DlgKid extends JDialog {
JButton btn = new JButton("kill me");
DlgKid handle;
public DlgKid() {
this.getContentPane().add(btn);
this.setSize(100,100);
this.setLocation(200,200);
handle = this;
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handle.dispose();
}
});
pack();
}
}
Layout 2003-05-27
  • 打赏
  • 举报
回复
呵呵,对哦..我居然没有想到可以公用变量
嘿嘿...谢谢了.
helpall 2003-05-27
  • 打赏
  • 举报
回复
class DlgMain extends JDialog {
JButton btn = new JButton("create");
public DlgMain() {
this.getContentPane().add(btn);
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
DlgKid xx1 = new DlgKid();
xx1.setVisible(true);
}
});
this.show();
}
public static void main(String[] args) {
DlgMain xx = new DlgMain();
xx.setSize(200,200);
xx.pack();

}
}
class DlgKid extends JDialog {
JButton btn = new JButton("kill me");
DlgKid handle;
public DlgKid() {
this.getContentPane().add(btn);
this.setSize(100,100);
this.setLocation(200,200);
handle = this;
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
handle.dispose();
}
});
pack();
}
}

62,614

社区成员

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

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