java - How to close child jFrames automatically when i close the parent? -
i have parent jframe , 2 'child' jframes in package jframe. imagine open parent jframe , it's child jframes - 3 forms open. close parent jframe.
what should close child frames automatically after close parent jframe?
here code:
class parent:
public class parent extends 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); }}
class child1:
public class child1 extends jframe {
public child1() { this.setbounds(200, 300, 300, 200); this.settitle("child 1"); }}
class child2:
public class child2 extends jframe {
public child2() { this.setbounds(200, 300, 300, 200); this.settitle("child 1"); }}
add window listener frame implements windowclosed(windowevent e)
method javadoc windowlistener
Comments
Post a Comment