• Post Reply Bookmark Topic Watch Topic
  • New Topic

Simple Question  RSS feed

 
Angelo Watson
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.awt.event.*;
import javax.swing.*;
public class ClassA extends JFrame {
public ClassA() {
super("Hello World");
Container container = getContentPane();
JButton button1 = new JButton("Button 1");
button1.addActionListener(
new ActionListener() {
pubic void actionPerformed(ActionEvent e) {
ClassB app = new ClassB();
app.setVisible(true);
}
}
);
container.add(button1);
setSize(400,400);
setVisible(true);
}
}
Another Class:
import java.awt.event.*;
import javax.swing.*;
public class ClassB extends JFrame {
public ClassB() {
super("Hello World");
Container container = getContentPane();
JButton button2 = new JButton("Button 2");
container.add(button1);
setSize(400,400);
setVisible(true);
}
}
When I hit "Button 1" in Class A it will show ClassB, but when I close ClassB it closes Class A also.
My question is is there a way I can ONLY close ClassB.
Hopefully this helps.
 
Chloe Loh
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think there is some error in the code that you posted, ClassB shouldn't be compiled successfully because the button1 that called in it should be unknown variable for classB.
 
jianqiang huhu
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Class A and Class B are all running in a same application,so you if you exit the application in Class B, so Class A also can not contine to run.
If you only want want to close Class B and not Class A, you can use the this.dispose() or this.setVisible(fales) to close the object of Class B.
Hope helps!
 
John Penalosa
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank You
 
Angelo Watson
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank You jianqiang huhu I kindof figured it was that simple. Thanks again
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!