我在JFrame包中有一个父JFrame和两个'子'JFrame。想象一下,我打开父JFrame,然后是它的子JFrames - 目前所有3个表单都是打开的。现在我关闭父JFrame。
关闭父JFrame后,如何自动关闭所有子框架? 这是我的代码:
班级家长:
public class Parent扩展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");
}}
class Child2:
公共类Child2扩展了JFrame {
public Child2() {
this.setBounds(200, 300, 300, 200);
this.setTitle("child 1");
}}