Reputation: 51
I want to use 2 panel in the same frame. But button is not working? How do i do that? I want to put couple of buttons in one panel and other panel will do some other stuff.
public class TestingPage extends JFrame {
JFrame frame=new JFrame();
JPanel panel01;
JPanel panel02;
JButton bttn1;
/**
* @param args
*/
public TestingPage(){
super("Test");
setBounds(700,700,650,500);
setVisible(true);
setLayout(new BorderLayout());
Container cont=frame.getContentPane();
panel01=new JPanel();
panel02=new JPanel();
cont.add(panel01,BorderLayout.EAST);
cont.add(panel02,BorderLayout.WEST);
//setLayout(new BorderLayout());
bttn1=new JButton("Button");
bttn1.setBounds(77, 75, 100,26);
add(bttn1);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Runnable guiCreator= new Runnable(){
@Override
public void run (){
TestingPage page=new TestingPage();
}
};
javax.swing.SwingUtilities.invokeLater(guiCreator);
}
}
Upvotes: 1
Views: 254
Reputation: 347214
You have three problems...
setVisible
before you've finished creating the UI. This is well know common problem. If you need to add content to the frame after it is set visible, you will need to call revalidate
to ensure that the layout is updatedJFrame
, but you create another JFrame
and use it's content pane to add your components to, but make your TestingPage
visible...import java.awt.BorderLayout;
import java.awt.Container;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class TestingPage extends JFrame {
// JFrame frame = new JFrame();
JPanel panel01;
JPanel panel02;
JButton bttn1;
public TestingPage() {
super("Test");
setBounds(700, 700, 650, 500);
setLayout(new BorderLayout());
Container cont = getContentPane();
panel01 = new JPanel();
panel02 = new JPanel();
cont.add(panel01, BorderLayout.EAST);
cont.add(panel02, BorderLayout.WEST);
//setLayout(new BorderLayout());
bttn1 = new JButton("Button");
panel01.add(bttn1);
setVisible(true);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Runnable guiCreator = new Runnable() {
@Override
public void run() {
TestingPage page = new TestingPage();
}
};
javax.swing.SwingUtilities.invokeLater(guiCreator);
}
}
Upvotes: 3