switching of showing 2 panels
Hi. My game has a menu panel with passive rendering (swing) and
animation panel with active rendering. I want to be able switch between
2 of them, so only one is shown at the time. For example, when I click
start game in menu panel then menu panel should hide and animation panel
become visible for user to play. Same thing if I want from game back to
main menu. How is this done with 2 panels?
I've tried adding both panels to JFrame container and just seting
visible one of them at the time, but dosen't work, see example code
below. Can somebody help me with this?
Here's the example code, it adds 2 panels both with JTextField to show
text that identifies what panel is displayed. Clicking on left side
should show panel2 and clicking right side should show panel1, but for
right side it dosen't work.
--------------------------------------------------------
import java.awt.event.*;
import javax.swing.*;
public class TFrame extends JFrame implements MouseListener {
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JTextField tf1 = new JTextField("Test 1");
JTextField tf2 = new JTextField("Test 2");
public TFrame() {
super("TEST");
setDefaultCloseOperation(EXIT_ON_CLOSE);
panel1.add(tf1);
panel2.add(tf2);
panel1.setVisible(false);
panel2.setVisible(false);
add(panel1);
add(panel2);
setBounds(0,0,200,100);
setVisible(true);
addMouseListener(this);
}
public static void main(String[] args) {
new TFrame();
}
public void mouseClicked(MouseEvent e) {}
public void mousePressed(MouseEvent e) {
if (e.getX() < 99) {
System.out.println("tf2 should appear");
panel1.setVisible(false);
panel2.setVisible(true);
repaint();
} else {
System.out.println("tf1 should appear");
panel2.setVisible(false);
panel1.setVisible(true);
repaint();
}
}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
} // end of class
--
Kova