-1
我在包JFrame中有一个父JFrame和两个'子'JFrames。想象一下,我打开父JFrame,然后它是小孩JFrames - 目前所有3种形式都是开放的。现在我关闭父JFrame。如何关闭父项时自动关闭子jFrames?
我应该怎样在关闭父JFrame后自动关闭所有子框架?
这里是我的代码:
类家长:
公共类父扩展的JFrame {
public Parent() {
JButton child1 = new JButton("child1");
child1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new Child1().setVisible(true);;
}
});
JButton child2 = new JButton("child2");
child2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new Child2().setVisible(true);
}
});
JButton closeAllJframe = new JButton("closeAllJframe");
closeAllJframe.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
dispose();
}
});
this.setBounds(500, 200, 400, 200);
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
dispose();
}
});
JPanel jPanel = new JPanel();
jPanel.add(child1);
jPanel.add(child2);
jPanel.add(closeAllJframe);
this.add(jPanel);
}
@Override
public void dispose() {
super.dispose();
}
public static void main(String[] args) {
new Parent().setVisible(true);
}}
类Child1:
公共类Child1扩展的JFrame {
public Child1() {
this.setBounds(200, 300, 300, 200);
this.setTitle("child 1");
}}
类Child2:
公共类CHILD2扩展的JFrame {
public Child2() {
this.setBounds(200, 300, 300, 200);
this.setTitle("child 1");
}}
形式?你的意思是jframes? –
如果它是jframes,则在窗口关闭事件上添加窗口侦听器并在子jframes上调用处理。 –
1)提示:添加@FastSnail(或者,'@'是重要的)* *通知*新的评论的人。 2)为了更快地获得更好的帮助,请发布[MCVE]或[简短,独立,正确的示例](http://www.sscce.org/)。 3)请参阅[使用多个JFrames,好/坏实践?](http://stackoverflow.com/q/9554636/418556)提示:'子框架'应该是对话框。 –