1

Like this,

image

How to set two panels in east of frame and maintain fixed size of panel one and two?

How to set image BG of frame?

Community
  • 1
  • 1

3 Answers3

2

By nested layouts. Add a BoxLayout on the EAST of your BoarderLayout. See Andrew Thompson answer to this question.

Community
  • 1
  • 1
Maroun
  • 94,125
  • 30
  • 188
  • 241
1

If you don't mind a bit of manual tinkering you can use the SpringLayout to take manual control of layouts. Using this approach I have made a little mock-up based on your picture. Have a look and see if it can help. If you use the commented out line as well (replacing the line above it) it should keep the sizes the same

public class SO{
    public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel panel1 = new JPanel();
    panel1.setBackground(java.awt.Color.RED);

    JPanel panel2 = new JPanel();
    JButton but1 = new JButton("Button 1");
    JButton but2 = new JButton("Button 2");
    panel2.setBackground(java.awt.Color.BLUE);
    panel2.add(but1);
    panel2.add(but2);

    SpringLayout layout = new SpringLayout();
    frame.setLayout(layout);

    frame.add(panel1);
    layout.putConstraint(SpringLayout.EAST, panel1, -5, SpringLayout.EAST, frame.getContentPane());
    layout.putConstraint(SpringLayout.WEST, panel1, -200, SpringLayout.EAST, frame.getContentPane());
    layout.putConstraint(SpringLayout.NORTH, panel1, 5, SpringLayout.NORTH, frame.getContentPane());
    layout.putConstraint(SpringLayout.SOUTH, panel1, -5, SpringLayout.NORTH, panel2);
    //layout.putConstraint(SpringLayout.SOUTH, panel1, 200, SpringLayout.NORTH, frame.getContentPane()); swap this for the above one to keep the sizes the same

    frame.add(panel2);
    layout.putConstraint(SpringLayout.EAST, panel2, -5, SpringLayout.EAST, frame.getContentPane());
    layout.putConstraint(SpringLayout.WEST, panel2, -200, SpringLayout.EAST, frame.getContentPane());
    layout.putConstraint(SpringLayout.NORTH, panel2, -100, SpringLayout.SOUTH, frame.getContentPane());
    layout.putConstraint(SpringLayout.SOUTH, panel2, -5, SpringLayout.SOUTH, frame.getContentPane());

    frame.setSize(400, 350);
    frame.setVisible(true);
   }
}

Good luck!

Levenal
  • 3,796
  • 3
  • 24
  • 29
0

What you do is:

  • Make a new panel (for this example called outerPanel)
  • add the 2 panels you want to this outerPanel
  • THEN add this outerPanel to the frame
Voidpaw
  • 910
  • 1
  • 5
  • 18