Home > Mobile >  Best way to switch Panels in Frame
Best way to switch Panels in Frame

Time:12-03

I have started working with JFrames and am creating a simple programme with 3 panels that are different colours and I was just wondering what would be the best way to switch between panels for my programme. The panels are to be changed when a JMenuItem with the colours name is selected. I was looking at cards and was wondering if they would be a good solution and if it would be possible to implement them into my programme? Any other suggestions would be greatly appreciated & apologies if I have done anything wrong ive only started on stack overflow and am quite a newcomer to Java :)

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Colour extends JFrame
{
CardLayout card;
JPanel childPanel =  new JPanel(), redPanel, bluePanel, greenPanel;

Container c;
public Colour(){

// Get Container
c=getContentPane();

//Create Menu and items
childPanel = new JPanel();

JMenuBar menuBar = new JMenuBar();

JMenu customer = new JMenu("Colour");
customer.setMnemonic(KeyEvent.VK_U);

JMenuItem blue = new JMenuItem("Blue");
JMenuItem red = new JMenuItem("Red");
JMenuItem green = new JMenuItem("Green");

customer.add(blue);
customer.add(red);
customer.add(green);
menuBar.add(customer);

this.setJMenuBar(menuBar);
//Adding panel to container
c.add(childPanel);
childPanel.setBackground(Color.PINK);

//Action Listeners for switching panels
blue.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {

} 
} );

red.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {

} 
} );

green.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {

} 
} );}

//Creating my different panels and adding them to childPanel
public void redC() {
    
    redPanel = new JPanel();
    redPanel.setBackground(Color.RED);
    childPanel.add(redPanel, "Red");
    
    
}

public void blueC() {
    bluePanel = new JPanel();
    bluePanel.setBackground(Color.BLUE);
    childPanel.add(bluePanel, "Blue");
    
}

public void  greenC() {
    greenPanel = new JPanel();
    greenPanel.setBackground(Color.GREEN);
    childPanel.add(greenPanel, "Green");
}
public static void main(String[] args) {
Colour cl=new Colour();
cl.setSize(400,400);
cl.setVisible(true);
cl.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}

CodePudding user response:

You are almost done with you code. Here is the working code. You need to revalidate the childPanel after adding/removing a panel so that it will be repainted:

//Action Listeners for switching panels
        blue.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (childPanel.getComponentCount() >0)
                    childPanel.remove(0);
                blueC();
                childPanel.revalidate();
            }
        });

        red.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (childPanel.getComponentCount() > 0)
                    childPanel.removeAll();
                redC();
                childPanel.revalidate();
            }
        });

        green.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (childPanel.getComponentCount() > 0)
                    childPanel.remove(0);
                greenC();
                childPanel.revalidate();
            }
        });
  • Related