2

I'm still trying to figure out BorderLayout in JavaSwing, and it's really frustrating.

I want to be able to split a pane into 3 contained sub-panels, but I'm not entirely sure how to contain it.

Here is my class Game, which contains panels ControlPanel, StatePanel, and Board (really just an extension of JPanel). I want to split the panel into those three sections like this: simple picture

Here is my code so far:

public class Game extends JPanel {
    private int panespace=2;
    private JPanel ControlPanel;
    private JPanel StatePanel;
    private Board board;
    public Game()
    {
         setLayout(new BorderLayout(panespace, panespace));
         ControlPanel=new JPanel();
         ControlPanel.setLayout(new BorderLayout(panespace, panespace));
         ControlPanel.setBorder(new LineBorder(Color.red, 10));
         ControlPanel.setSize(100,100);
         super.add(ControlPanel, BorderLayout.EAST);
         StatePanel=new JPanel();
         StatePanel.setLayout(new BorderLayout(panespace, panespace));
         StatePanel.setBorder(new LineBorder(Color.green, 10));
         StatePanel.setSize(200, 200);
         super.add(StatePanel, BorderLayout.EAST);
         board = new Board();
         board.setLayout(new BorderLayout(panespace, panespace));
         board.setBorder(new LineBorder(Color.yellow, 10));
         board.setSize(300, 300);
         super.add(board, BorderLayout.NORTH);
         setVisible(true);
    }
}

Preferably, this would use BorderLayout, but I seem to be doing it incorrectly. I've tried reading the materials that Oracle provides, but they haven't been particularly helpful. At this point, I am at the point where I want to hard code values in, but I know that I should be able to split it to have each panel take up as much of the larger Game panel as possible.

mKorbel
  • 109,525
  • 20
  • 134
  • 319

3 Answers3

3

As mkhelif said, you can't have two things in the same slot. What you want is board to go into CENTER, then create another JPanel in SOUTH which contains ControlPanel and StatePanel in a FlowLayout.

Zarkonnen
  • 22,200
  • 14
  • 65
  • 81
1

You are adding ControlPanel and StatePanel on EAST both. Try adding ControlPanel on WEST.

mkhelif
  • 1,551
  • 10
  • 18
1

Besides the East/East thing, you also extend JPanel instead of JFrame. ie, where you have

public class Game extends JPanel

You probably want

public class Game extends JFrame

You also set up borders, but put nothing in the panels, so the borders surround an empty space, making them look like thick lines instead of actual borders. You might try adding a JTextArea in each panel.

Also, when you change size by dragging the border, the CENTER section (which you don't use) is what grows larger. If you want the result I think you want, you'll have to use a different Layout.

Good luck!

Roy
  • 974
  • 6
  • 11
  • 2
    no need to extend JFrame (same as there is no need to extend JPanel, except for doing custom painting) - all JSomething should be _used_ as-are. – kleopatra Oct 12 '12 at 08:37
  • @kleopatra You may be right on the JFrame thing. When I copied the code to my computer, I added a setDefaultCloseOperation which works on JFrame and not JPanel, but that's above and beyond what was asked. But I do think she needs to extend one or the other, or else the setLayout, setVisible, and .add don't work. – Roy Oct 12 '12 at 20:26
  • _she needs to extend one or the other_ being in the scope of the question or not: it's simply wrong (hint: usage vs. inheritance). You might consider deleting that part of the answer. – kleopatra Oct 13 '12 at 22:30