Three options here:
1 - Keep the border layout and set a prefered size on the two JPanels. This will work with fixed size window.
2 - Use the GridBagLayout:
public class TestJPanel extends JFrame {
public TestJPanel() {
JPanel rootPanel = new JPanel();
rootPanel.setBackground( Color.WHITE );
rootPanel.setPreferredSize( new Dimension( 0, 100 ) );
rootPanel.setLayout( new GridBagLayout() );
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.BOTH;
c.weighty = 1;
JPanel leftPanel = new JPanel();
leftPanel.setBackground( Color.BLUE );
c.weightx = 0.2;
c.gridx = 0;
rootPanel.add( leftPanel, c );
JPanel rightPanel = new JPanel();
c.weightx = 0.8;
c.gridx = 1;
rightPanel.setBackground( Color.RED );
rootPanel.add( rightPanel, c );
this.add( rootPanel, BorderLayout.CENTER );
}
public static void main( String[] args ) {
TestJPanel window = new TestJPanel();
window.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
window.setSize( 1024, 768 );
window.setVisible( true );
}
}
3 - Use the famous SpringLayout. This layout manager is really hard to master but here is a starter:
public class TestJPanel extends JFrame {
public TestJPanel() {
JPanel rootPanel = new JPanel();
rootPanel.setBackground( Color.WHITE );
rootPanel.setPreferredSize( new Dimension( 0, 100 ) );
SpringLayout layout = new SpringLayout();
rootPanel.setLayout( layout );
SpringLayout.Constraints rootPaneCons = layout.getConstraints( rootPanel );
rootPaneCons.setWidth( Spring.width( this ) );
rootPaneCons.setHeight( Spring.height( this ) );
JPanel leftPanel = new JPanel();
leftPanel.setBackground( Color.BLUE );
rootPanel.add( leftPanel );
SpringLayout.Constraints leftPaneCons = layout.getConstraints( leftPanel );
leftPaneCons.setWidth( Spring.scale( rootPaneCons.getWidth(), .2f ) );
leftPaneCons.setHeight( Spring.scale( rootPaneCons.getHeight(), 1 ) );
JPanel rightPanel = new JPanel();
rightPanel.setBackground( Color.RED );
rootPanel.add( rightPanel );
SpringLayout.Constraints rightPaneCons = layout.getConstraints( rightPanel );
rightPaneCons.setX( Spring.scale( rootPaneCons.getWidth(), .2f ) );
rightPaneCons.setWidth( Spring.scale( rootPaneCons.getWidth(), .8f ) );
rightPaneCons.setHeight( Spring.scale( rootPaneCons.getHeight(), 1 ) );
this.add( rootPanel, BorderLayout.CENTER );
}
public static void main( String[] args ) {
TestJPanel window = new TestJPanel();
window.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
window.setSize( 1024, 768 );
window.setVisible( true );
}
}