Reputation: 215
I'm trying to make a program with three JPanels. Two of them should take up half the height each and end at the same point, so the third can take up the whole screen height and fill in the rest of the open space. Here's what I want it to look like, approximately, without the black border:
However, when I was trying to code it, the panels were either taking up too much of the width or not filling in enough height. I played around with all sorts of combinations of BorderLayout restraints when adding them to the content panel, but nothing worked. With certain combinations, the blue and red were the correct height but took up the whole width, and the green wasn't present at all. Other times, such as with the code I'll provide below, the blue looks like it should, but the red takes up the whole width, making the green half the height it should be.
JPanel contentPanel = new JPanel(new BorderLayout());
JPanel redPanel = new JPanel();
JPanel greenPanel = new JPanel();
JPanel bluePanel = new JPanel();
//left out background setting lines
//Dimension screenSize defined above, this is a fullscreen program
redPanel.setPreferredSize(new Dimension(screenSize.width-300,screenSize.height/2));
bluePanel.setPreferredSize(new Dimension(screenSize.width - 300,screenSize.height/2));
greenPanel.setPreferredSize(new Dimension(300, screenSize.height));
contentPanel.add(greenPanel, BorderLayout.EAST);
contentPanel.add(bluePanel, BorderLayout.WEST);
contentPanel.add(redPanel, BorderLayout.SOUTH);
And here's what it looks like with this code:
I tried adding the blue one as north and green as south and shuffling the orders, but nothing worked. What's going wrong and how do I fix it?
Upvotes: 0
Views: 822
Reputation: 347194
You'll probably need a compound layout.
Start by creating a container whose layout is a GridLayout
, add the two components you want split to it, then add that to the centre of the container with the BorderLayout
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
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 center = new JPanel(new GridLayout(2, 1));
center.add(makePanel(Color.BLUE));
center.add(makePanel(Color.RED));
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(center);
frame.add(makePanel(Color.GREEN), BorderLayout.EAST);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
protected JPanel makePanel(Color background) {
TestPane panel = new TestPane();
panel.setBackground(background);
return panel;
}
public class TestPane extends JPanel {
public TestPane() {
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 100);
}
}
}
Upvotes: 3