i am trying to make a simple interface in java,but i have a problem with adding multiple panels into a frame.I want it to be a cafe software so there will be multiple tables.Here is my code
public class CafeView extends JFrame{
private JButton takeMoneyBtn = new JButton("Hesabı Al");
public CafeView(){
JPanel table1 = new JPanel();
this.setSize(800,600);
this.setLocation(600, 200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
table1.setBounds(100, 100, 150, 150);
table1.setLayout(null);
this.add(table1);
table1.add(takeMoneyBtn);
}
}
When i run it, i just see an empty frame.In this code i just tried to add one simple panel,if i can do it i can add others too i think.So how can i solve it and add small and many panels into a frame,what am i missing? Thank you for your help.(There isn't a main method cause i call this interface class from another class.)
You're arranging the position of your component which is in this case JPanel
using .setBounds(..,..)
, You should instead use it to the top level container (JFrame
, JWindow
, JDialog
, or JApplet
.) not to JPanel
.
So remove :
table1.setBounds(100, 100, 150, 150);
We are provided by LayoutManager
to arrange components, see LayoutManager
public class CafeView extends JFrame{
private JButton takeMoneyBtn = new JButton("Hesabı Al");
public CafeView(){
JPanel table1 = new JPanel();
this.setSize(800,600);
this.setLocation(600, 200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
//table1.setBounds(100, 100, 150, 150);
//table1.setLayout(null);
this.add(table1,BorderLayout.CENTER);
table1.add(takeMoneyBtn);
}