Answer the question
In order to leave comments, you need to log in
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();
}
}
public class Main extends CardLayout {
public Main() {
}
}
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"));
}
}
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"));
}
}
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question