0

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.)

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Koray Durudogan
  • 624
  • 4
  • 12
  • 31
  • 2
    Maybe your frame isn't empty at all. Try adding a border to your panels using setBorder() so you can actually see the JPanel. – MarsAtomic Sep 30 '14 at 17:40
  • 3
    Also it is wrong to call setVisible(true) before you are done putting all of your visual components together – ControlAltDel Sep 30 '14 at 17:43
  • Java GUIs have to work on different OS', screen size, screen resolution etc. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Oct 01 '14 at 08:21

2 Answers2

1

I would use a LayoutManager to place all the controls and access the content pane directly. How to use FlowLayout

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);
    Container c = this.getContentPanel();
    c.setLayout(new FlowLayout());

    c.add(table1);
    c.add(takeMoneyBtn);
    //c.add() more stuff..

    this.setVisible(true);
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
morpheus05
  • 4,772
  • 2
  • 32
  • 47
  • +1. Further note. Swing GUIs should be created and updated on the EDT. See [Concurrency in Swing](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/) for more details. – Andrew Thompson Oct 01 '14 at 08:23
1

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);

}
Fevly Pallar
  • 3,059
  • 2
  • 15
  • 19