Changing Panel
I have started making a gui, but am unable to work out how to do what I want:
Basically the main frame that loads when you run the program has two buttons, what I want to do is, if the person presses buttonA then the panel changes, the buttons on the main frame disappear and the content of the new panel is displayed on the frame (and the new panel would have a button that would take you back to the main menu where the two jbuttons are visible again).
Here is the code for the main frame, where the two jbuttons are when the program first loads:
package gui;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
publicclass MyFrameextends JPanel{
/**
*
*/
privatestaticfinallong serialVersionUID = 1;
publicint WIDTH = 600;
publicint HEIGHT = 500;
publicstaticvoid main(String[] args){
MyFrame bw =new MyFrame();
bw.createWindow();
}
publicvoid createWindow(){
//Create and set up window
JFrame frame =new JFrame();
frame.setBounds(200,100,WIDTH,HEIGHT);
//Add items to the frame
JPanel jp =new JPanel();
jp.setPreferredSize(new Dimension(WIDTH,HEIGHT));
JButton buttonA =new JButton("Button A");
JButton buttonB =new JButton("Button B");
BorderLayout borderLayout =new BorderLayout();
this.setLayout(borderLayout);
this.setLayout(new GridLayout(1,1));
jp.add(buttonA);
jp.add(buttonB);
frame.getContentPane().add(jp);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.pack();
}
}
And here is the code for the panel I want to be displayed when the user chooses buttonA (buttonB has the same code as buttonA)
package gui;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
publicclass PanelAextends JPanel{
/**
*
*/
privatestaticfinallong serialVersionUID = 1;
publicint WIDTH = 600;
publicint HEIGHT = 500;
public JLabel labelA =new JLabel("Panel A");
public JButton buttonA =new JButton("Back to main page");
PanelA(){
// Set Size and background Colour
this.setBackground(Color.RED);
this.setPreferredSize(new Dimension(WIDTH,HEIGHT));
// Grid Layout
BorderLayout borderLayout =new BorderLayout();
this.setLayout(borderLayout);
this.setLayout(new FlowLayout(1));
this.add(labelA);
this.add(buttonA);
}
}
I hope this is clear, if not let me know and I'll try and clarify things further.

