Hello,
I've recently started using swing for creating User Interfaces and here is a difficulty I cannot overcome. My application has a main class and four JPanel forms.
The main class has a function that creates a JFrame and loads in it an instance of the first "JPanel" class. This has three radio buttons in a group and a JButton with the below function:
private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
if (jRadioButton1.isSelected()) {
JOptionPane.showMessageDialog(null, "Beginner");
this.selection = 1;
} else if (jRadioButton2.isSelected()) {
JOptionPane.showMessageDialog(null, "Intermediate");
this.selection = 2;
} else if (jRadioButton3.isSelected()) {
JOptionPane.showMessageDialog(null, "Advanced");
this.selection = 3;
} else {
JOptionPane.showMessageDialog(null, "Make a selection!");
}
}
selection is a public int member of
NewJPanel (fst JPanel class)
Here is the Main class:
public Main() {
}
public void PlayAGame() {
JFrame myFrame = new JFrame("Difficulty selection:");
NewJPanel panel = new NewJPanel();
myFrame.setSize(256,256);
myFrame.setDefaultCloseOperation(myFrame.EXIT_ON_CLOSE);
myFrame.getContentPane().add(panel);
myFrame.pack();
myFrame.setVisible(true);
switch (panel.selection) {
case 1: {
NewJPanel1 panel1 = new NewJPanel1();
myFrame.getContentPane().remove(panel);
myFrame.getContentPane().add(panel1);
break;
}
case 2: {
NewJPanel2 panel2 = new NewJPanel2();
myFrame.getContentPane().remove(panel);
myFrame.getContentPane().add(panel2);
break;
}
case 3: {
NewJPanel3 panel3 = new NewJPanel3();
myFrame.getContentPane().remove(panel);
myFrame.getContentPane().add(panel3);
break;
}
default: {
break;
}
}
}
public static void main(String ar[]){
Main myMain = new Main();
myMain.PlayAGame();
}
Here is what I want the code to do:
Load a
NewJPanel in the frame, the user makes a choice and clicks the button, then PlayAGame() (using the switch) unloads the NewJPanel and loads the suitable Panel in the frame.
Here is what it does:
NewJPanel is loaded, I make a choice, click the button and nothing happens; NewJPanel is still in the frame
I need help, please!
Regards,
Vassil Zorev