Note that there are some explanatory texts on larger screens.

plurals
  1. POHowcome I can't use a BorderLayout with my panel?
    text
    copied!<p>I am making a program in which there is one frame, which contains a GridLayout g = (2,2), meaning essentially a square divided into four parts. In each part i have added a panel and named them accordingly (tl = top left, tr = to pright, bl = bottom left, br = bottom right) and now I want to add panels into those panels. I want to set the layout of Panel tl to a BorderLayout, and then add a another panel - called tl1 - into tl. Here is my code, but for some reason it won't let me do this. I'm a beginner in java so i'm not really sure exactly how everything works so this might be a stupid question, but as i looked at the syntax for border layout it said that it was written like (component, BORDER.LAYOUT North) or something. Is JPanel not a component and is that why this isn't working?</p> <pre><code>public class BattleShipGrid { JFrame frame = new JFrame("Battleship"); //JPanel tl = new JPanel(); JPanel tl = new JPanel(); JPanel tl1 = new JPanel(); JPanel tr = new JPanel(); JPanel bl = new JPanel(); JPanel br = new JPanel(); JButton b1 = new JButton(); GridLayout g = new GridLayout(2,2,2,2); GridLayout gtl = new GridLayout(11,11,2,2); JButton[][] tlba; public BattleShipGrid(){ frame.setVisible(true); frame.setSize(800,800); frame.setBackground(Color.RED); frame.setLayout(g); frame.add(tl);tl.setBackground(Color.GREEN); tl.setLayout(new BorderLayout()); frame.add(tr);//tr.setBackground(Color.BLUE); frame.add(bl);//bl.setBackground(Color.GREEN); frame.add(br);//br.setBackground(Color.YELLOW); /*GridBagConstraints gbc = new GridBagConstraints(); gbc.insets = new Insets(15,15,0,0); tl.setLayout(gtl); gbc.gridx = 3; gbc.gridy = 3; tl1.add(b1); */ //ADD A TOOLBAR SO THAT THE USER CAN MAKE A NEW GAME OR ACCESS OTHER OPTIONS tl1.setBackground(Color.yellow); tl.add(tl1, BorderLayout); tl1.setPreferredSize(new Dimension(200,200)); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload