How do I set different sizes of each of my panels in the layout
First, understand that CardLayout
will use the preferredSize
property of all the view's it's managing to determine the best resulting size that the container it's managing should use. This means that if you call pack
on the frame (instead of setSize
), it will be sized (automatically) to the largest component been managed (by the CardLayout
)
How would I change the code so that if I wanted to add another panel with a different size?
Each component you add to the CardLayout
should either be calculating it's size through one or more appropriate layout managers, or, in the case of custom components, be providing a sizing hint through the getPreferredSize
method
public class Title extends JPanel
{
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
@Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
g.fillRect(100,100,100,100);
}
}
Then, instead of using setSize
, use pack
public void run()
{
frame = new JFrame("Greek Olympics");
//frame.setSize(1000,1000);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(cards);
frame.pack();
CardLayout cl = (CardLayout)(cards.getLayout());
cl.show(cards, "title");
frame.setVisible(true);
}
Example...
This is a basic example which sets up two panels, one with preferredSize
of 200x200
and one with 400x400
When you run it, you will find the window will be at least 400x400
and both panels will be the same size
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
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();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
CardLayout cardLayout = new CardLayout();
JPanel base = new JPanel(cardLayout);
base.add(makePanel(200, 200, Color.RED), "red");
base.add(makePanel(400, 400, Color.BLUE), "blue");
frame.add(base);
JButton blue = new JButton("Blue");
blue.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
cardLayout.show(base, "blue");
}
});
JButton red = new JButton("red");
red.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
cardLayout.show(base, "red");
}
});
JPanel buttons = new JPanel();
buttons.add(red);
buttons.add(blue);
frame.add(buttons, BorderLayout.SOUTH);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public TestPane makePanel(int width, int height, Color background) {
TestPane pane = new TestPane(new Dimension(width, height));
pane.setBackground(background);
return pane;
}
public class TestPane extends JPanel {
private JLabel label;
private Dimension preferredSize;
public TestPane(Dimension size) {
label = new JLabel("...");
setLayout(new GridBagLayout());
add(label);
this.preferredSize = size;
}
@Override
public Dimension getPreferredSize() {
return preferredSize;
}
@Override
public void invalidate() {
super.invalidate();
label.setText(getWidth() + "x" + getHeight());
}
}
}