Question

I'm currently writing an interface where a have a JFrame class and two JPanel classes. When the script is first executed, Panel A is shown. I have a JButton in Panel A which I would like, when clicked, to display Panel B instead of Panel A.

Is there any way I could do this?

Was it helpful?

Solution

Read tutorial for that.

You can use next() method of CardLayout for showing next card,

or you can use show(...); for showing specific card.

Simple example:

import java.awt.BorderLayout;
import java.awt.CardLayout;
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;

public class Example {

    public static void main(String[] args){
        JFrame frame = new JFrame();

        final JPanel panel = new JPanel(new CardLayout());

        JLabel l1 = new JLabel("1");
        JLabel l2 = new JLabel("2");
        JLabel l3 = new JLabel("3");

        panel.add(l1,"l1");
        panel.add(l2,"l2");
        panel.add(l3,"l3");

        JButton btn = new JButton("next");
        btn.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                CardLayout layout = (CardLayout) panel.getLayout();
                layout.next(panel);
            }
        });

        JButton btnSpec = new JButton("l3");
        btnSpec.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                CardLayout layout = (CardLayout) panel.getLayout();
                layout.show(panel, "l3");
            }
        });

        frame.add(panel);
        frame.add(btn,BorderLayout.SOUTH);
        frame.add(btnSpec,BorderLayout.NORTH);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top