D
D
Don_Conteo2021-01-16 06:07:20
Java
Don_Conteo, 2021-01-16 06:07:20

How to switch between panels from different classes?

I need to implement switching between panels in different classes. How can I implement this?

Everything is shown here

public class Display {

    public Display() {
        JFrame frame = new JFrame("MindLoop");
        frame.add(new Main());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
        frame.setUndecorated(true);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        new Display();
    }
}


Here I somehow need to implement switching between panels
public class Main extends CardLayout {

    public Main() {
    }
}


From this panel, you need to switch to Kitchen when you click the toKitchen button
public class Room extends JPanel{

    Main main = new Main();

    Image back = new ImageIcon("res/images/interior1.jpg").getImage();
    private JButton toKitchen = new JButton(new ImageIcon("res/images/arrowLeftUnpressed.png"));
    private JButton toBathRoom = new JButton(new ImageIcon("res/images/arrowRightUnpressed.png"));

    public void paint(Graphics g) {
        super.paint(g);
        g.drawImage(back, 0, 0, 1920, 1080, null);
        super.paintComponents(g);
    }

    public Room() {
        setLayout(null);
        add(toKitchen);
        toKitchen.setBounds(50, 470, 170, 170);
        toKitchen.setRolloverIcon(new ImageIcon("res/images/ArrowLeftPressed.png"));
        add(toBathRoom);
        toBathRoom.setBounds(1700, 470, 170, 170);
        toBathRoom.setRolloverIcon(new ImageIcon("res/images/arrowRightPressed.png"));
    }
}


From this, respectively, back when you press the toRoom button
public class Kitchen extends JPanel {

    Image back = new ImageIcon("res/images/kitchen.jpg").getImage();
    private JButton toRoom = new JButton(new ImageIcon("res/images/arrowRightUnpressed.png"));

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        g.drawImage(back, 0, 0, 1920, 1080, null);
        super.paintComponents(g);
    }

    public Kitchen() {
        setLayout(null);
        add(toRoom);
        toRoom.setBounds(1700, 470, 170, 170);
        toRoom.setRolloverIcon(new ImageIcon("res/images/arrowRightPressed.png"));
    }
}


To be honest, I'm not at all sure about the correct architecture, so if you correct me and set me on the right path, I will be very grateful

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Erik Mironov, 2021-01-16
@Don_Conteo

Greetings. Perhaps this is what you are looking for. https
://docs.oracle.com/javase/7/docs/api/javax/sw...
the class hierarchy is very small. The only thing I would do is single out common fields and methods among your component classes and put it all into an abstract class that will inherit JPanel, and then inherit from it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question