I am trying to change the JPanel in the parent class to the JPanel in the child class once the Jbutton jbtcredits is clicked.I tried implementing cardlayout in the child class but however,instead of the old JFrame changing the old JPanel to the new one,a new JFrame pops out with the new JPanel and the old JFrame with the old JPanel is left unchanged.This is the parent class:
private JButton jbtstart= new JButton("Start Cooking!");
private JButton jbtabout = new JButton("About");
JButton jbtcredits = new JButton("Credits");
private JButton jbtexit = new JButton("Exit");
private JLabel Screen;
JFrame frame = new JFrame("f");
JPanel p1 = new JPanel();
JPanel p = new JPanel();
JLabel back=new JLabel(new ImageIcon("C:\\Users\\Desktop\\background.png"));
//private static int gridSize = 2;
public GUI_interface()
{
super("GUIinterface");
}
public void createAndDisplayGUI()
{
Screen=new JLabel(new ImageIcon("C:\\Users\\Desktop\\Title.png"));
jbtstart.setLayout(new BorderLayout());
//JPanel p = new JPanel();
//p.setLayout(null);
p1.setLayout(new GridLayout(1,1));
p.setLayout(new BorderLayout());
back.setLayout(new FlowLayout());
jbtstart.setPreferredSize(new Dimension(40, 40));
p.add(Screen,BorderLayout.NORTH);
p.add(jbtstart);
p1.add(jbtabout);
p1.add(jbtcredits);
p1.add(jbtexit);
back.add(p);
back.add(p1);
jbtstart.addActionListener(this);
jbtabout.addActionListener(this);
jbtcredits.addActionListener(this);
jbtexit.addActionListener(this);
setContentPane(back);
frame.setTitle("Cooking App");
frame.setLocation(300,250);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(back);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
// find out which button was pressed
if (e.getSource()==jbtstart)
{
JOptionPane.showMessageDialog(null,"to be added");
}
else if (e.getSource() == jbtabout)
{
About files=new About();
//files.setSize(20, 30);
files.createAndDisplayGUI();
}
else if (e.getSource() == jbtcredits)
{
Credits filess=new Credits();
filess.createAndDisplayGUI();
}
else
{
{ Object[] options = {"Yes","No"};
int n = JOptionPane.showOptionDialog(null,
"Are you sure you would like to quit? ","Cooking App",
JOptionPane.PLAIN_MESSAGE,
JOptionPane.QUESTION_MESSAGE,
null,
options,
options[1]);
if (n==0)
{
System.exit(0);
}
}
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new GUI_interface().createAndDisplayGUI();
}
});
}
This is the child class with the new JPanel:
private JButton jbtback = new JButton("back");
JPanel a =new JPanel();
JPanel b=new JPanel();
public void createAndDisplayGUI(){
CardLayout c1= new CardLayout();
a.setLayout(c1);
JLabel back1=new JLabel(new ImageIcon("C:\\Users\\Desktop\\background.png"));
b.add(back1);
b.add(jbtback);
a.add(back,"1");
a.add(b,"2");
c1.show(a,"2");
setContentPane(a);
//a.add(back,"1");
a.add(jbtback,"2");
jbtcredits.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e)
{
// find out which button was pressed
c1.show(a,"2");
}
});
jbtback.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e)
{
// find out which button was pressed
c1.show(a,"1");
}
});
frame.add(a);
frame.setTitle("About us");
frame.setSize(300,250);
frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
}