Кажется, GridBagLayout может сделать этот трюк. Пожалуйста, взгляните на этот пример кода и убедитесь, что он подходит. Пожалуйста, дайте мне знать, если у вас возникнут вопросы:
import java.awt.*;
import javax.swing.*;
public class Example {
private final int hGap = 5;
private final int vGap = 5;
private GridBagConstraints gbc;
public Example () {
gbc = new GridBagConstraints ();
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
gbc.insets = new Insets ( hGap, vGap, hGap, vGap );
}
private void createAndDisplayGUI () {
JFrame frame = new JFrame ( "Example" );
frame.setDefaultCloseOperation ( JFrame.DISPOSE_ON_CLOSE );
JPanel contentPane = new JPanel ();
contentPane.setLayout ( new BorderLayout ( hGap, vGap ) );
JPanel gridBagPanel = new JPanel( new GridBagLayout () );
gridBagPanel.setBorder (
BorderFactory.createTitledBorder ( "GridBagLayout" ) );
gridBagPanel.setOpaque ( true );
gridBagPanel.setBackground ( Color.WHITE );
JPanel leftPanel = new JPanel ();
leftPanel.setOpaque ( true );
leftPanel.setBackground ( Color.red );
addComp ( gridBagPanel, leftPanel, 0, 0, 1, 1
, GridBagConstraints.BOTH, 0.33, 1.0 );
JPanel centerPanel = new JPanel ();
centerPanel.setOpaque ( true );
centerPanel.setBackground ( Color.blue );
addComp ( gridBagPanel, centerPanel, 1, 0, 1, 1
, GridBagConstraints.BOTH, 0.33, 1.0 );
JPanel leftLeftPanel = new JPanel ();
leftLeftPanel.setOpaque ( true );
leftLeftPanel.setBackground ( Color.white );
addComp ( gridBagPanel, leftLeftPanel, 2, 0, 1, 1
, GridBagConstraints.BOTH, 0.165, 1.0 );
JPanel rightPanel = new JPanel ();
rightPanel.setOpaque ( true );
rightPanel.setBackground ( Color.green );
addComp ( gridBagPanel, rightPanel, 3, 0, 1, 1
, GridBagConstraints.BOTH, 0.165, 1.0 );
contentPane.add ( gridBagPanel, BorderLayout.CENTER );
frame.setContentPane ( contentPane );
frame.pack ();
frame.setLocationByPlatform ( true );
frame.setVisible ( true );
}
private void addComp ( JPanel panel, JComponent comp
, int x, int y, int gWidth
, int gHeight, int fill
, double weightx, double weighty ) {
gbc.gridx = x;
gbc.gridy = y;
gbc.gridwidth = gWidth;
gbc.gridheight = gHeight;
gbc.fill = fill;
gbc.weightx = weightx;
gbc.weighty = weighty;
panel.add ( comp, gbc );
}
public static void main ( String [] args ) {
EventQueue.invokeLater ( new Runnable () {
@Override
public void run () {
new Example ().createAndDisplayGUI ();
}
} );
}
}