0

I want my frame to have 3 panels that will look like that

enter image description here

I'm a total newbie at JPanel and i cant organize it, so if someone can help i'd appreciate it

1

3 Answers 3

2

Start by having a look at Laying Out Components Within a Container. While you could use a GridBagLayout, you could simply use a series of compound containers using BorderLayouts

For example...

JPanel left = new JPanel(new BorderLayout());
left.add(new TestPane(), BorderLayout.NORTH);
left.add(new TestPane());

JPanel main = new JPanel(new BorderLayout());
main.add(left);
main.add(new TestPane(), BorderLayout.EAST);

SimpleLayout

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.LineBorder;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JPanel left = new JPanel(new BorderLayout());
                left.add(new TestPane(), BorderLayout.NORTH);
                left.add(new TestPane());

                JPanel main = new JPanel(new BorderLayout());
                main.add(left);
                main.add(new TestPane(), BorderLayout.EAST);

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(main);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setBorder(new LineBorder(Color.RED));
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

    }

}
1
  • @noam Glad it could help Commented Aug 19, 2015 at 6:51
0

You must use a LayoutManager.

There's plenty of information about SWING and layout manager on the net.

Check

Or just google for SWING and layout.

0

I prefer to use BorderLayout to arrange my panels. I set up 3 panels: east (width 100px), north(height 100px) and center:

public class Frame1 extends JFrame {
  BorderLayout borderLayout1 = new BorderLayout();
  JPanel jPanelEast = new JPanel();
  JPanel jPanelCenter = new JPanel();
  JPanel jPanelNorth = new JPanel();
  BorderLayout borderLayout2 = new BorderLaout();
  JPanel jPanelCentral = new JPanel();

  public Frame1() {
    try {
      jbInit();
    }
    catch(Exception ex) {
      ex.printStackTrace();
    }
  }

  void jbInit() throws Exception {
    this.getContentPane().setLayout(borderLayout1);

    jPanelEast.setBackground(Color.red);
    jPanelEast.setForeground(Color.black);
    jPanelEast.setMinimumSize(new Dimension(10, 10));
    jPanelEast.setOpaque(true);
    jPanelEast.setPreferredSize(new Dimension(100, 10));

    jPanelCenter.setLayout(borderLayout2);

    jPanelNorth.setBackground(Color.lightGray);
    jPanelNorth.setMinimumSize(new Dimension(10, 10));
    jPanelNorth.setOpaque(true);
    jPanelNorth.setPreferredSize(new Dimension(10, 100));

    this.getContentPane().add(jPanelEast, BorderLayout.EAST);
    this.getContentPane().add(jPanelCenter, BorderLayout.CENTER);
    jPanelCenter.add(jPanelNorth, BorderLayout.NORTH);
    jPanelCenter.add(jPanelCentral, BorderLayout.CENTER);
  }

  public static void main(String[] args) {
    Frame1 frame1 = new Frame1();
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.