Reputation: 71
To keep things short, I know how to switch between different JPanels
using CardLayout
and only using one JFrame
but I want to know how, if it is possible, to have different sized JPanels
occupying the JFrame
. CardLayout
uses the largest of the panels but I was wondering if there was any way to suppress that or override so that I could add different JPanels
to a JFrame
that were different sizes. Here is my code:
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class View {
//Views
private JFrame frame;
private JPanel container;
private JPanel panel1;
private JPanel panel2;
private CardLayout layout;
private final int WIDTH = 1280;
private final int HEIGHT = 720;
public View(){
init();
frame = new JFrame();
frame.setResizable(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.add(container);
frame.pack();
frame.setVisible(true);
}
private void init(){
JButton button = new JButton();
layout = new CardLayout();
container = new JPanel(layout);
panel1 = new JPanel();
panel2 = new JPanel();
panel1.setBackground(Color.WHITE);
panel1.setPreferredSize(new Dimension(500, 500));
panel1.add(button);
panel2.setBackground(Color.BLACK);
panel2.setPreferredSize(new Dimension(500, 500));
container.add(panel1, "panel1");
container.add(panel2, "panel2");
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
layout.show(container, "panel2");
}
});
}
public static void main(String [] args){
View view = new View();
}
}
I put the JButton in the init method because I need it for future use and wanted to make sure now that it could be done.
Upvotes: 1
Views: 129
Reputation: 31612
The only way it seems to work with the cardLayout is by setting the preferred size of the container panel after the button is clicked.
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
layout.show(container, "panel2");
container.setPreferredSize(new Dimension(100, 100));
frame.pack();
}
});
It's not a very great solution but it's pretty simple.
If a login window is what you need I would definitely recommend using 2 frames, just seems more logical.
Upvotes: 1
Reputation: 61
For a quick solution try two separate JFrames, or resize the frame to the size of the panel on button press.
Upvotes: 2
Reputation: 324147
You can't use a CardLayout. So you need to manage the visible panel yourself.
The logic would be something like:
JPanel content = (JPanel)frame.getContentPane();
content.removeAll();
content.add( theOtherPanel );
frame.pack();
So the idea is you only have one panel added to the content pane at a time and you need to swap it by doing the remove and add before you pack the frame so the frame is displayed at the size of each panel.
The question is why do you want to do this? Users don't like to see the size of the frame continually changing.
Upvotes: 1