I am not sure what is going on to be honest, I have another card layout system in my code and it works fine.
Summary of what I noticed :
1. there are no errors.
2. when I try to switch from Panel A to Panel B, the whole thing freezed.
3. when I try to switch from B to A, the .show() line has been ran, but A didn't show up and B keep running.
4. println are put all over the place(I removed most of them now) and I am quite sure the .show() command is being excute but nothing happens.
I'll be monitoring this post closely if any of you want addition information.
Core.java (where the main is in, also the Frame and the card layout in question, this is the code that most likely to have something wrong with it)
package TheGame; import java.awt.*; import javax.swing.*; public class Core extends JFrame { public static Core instance; JPanel contentPanel; ResPanel resPanel; OptPanel optPanel; CardLayout cl; JPanel container; public static Core getInstance(){ if(instance == null){ instance = new Core(); } return instance; } private Core() { cl = new CardLayout(); container = new JPanel(); container.setLayout(cl); optPanel = new OptPanel(); contentPanel = new JPanel(); contentPanel.setLayout(new BorderLayout()); resPanel = ResPanel.getInstance(); contentPanel.add(resPanel, BorderLayout.NORTH); container.add(contentPanel, "2"); container.add(optPanel, "3"); container.setVisible(true); add(container); setSize(800, 600); setDefaultCloseOperation(EXIT_ON_CLOSE); } public static void main(String[] args) { Core core = new Core(); core.setLocationRelativeTo(null); core.setVisible(true); core.showContent(); // core.showOption(); } public void showContent(){ cl.show(container, "" + 2); } public void showOption(){ cl.show(container, "" + 3); } }
OptPanel.java (This is the option panel, the card layout .show method is being called at the end of the code in responce to the apply/cancel button being clicked)
package TheGame; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class OptPanel extends JPanel implements ActionListener{ private Core core; private JButton oCancel; private JPanel down; public OptPanel(){ down = new JPanel(); oCancel = new JButton("Cancel"); oCancel.addActionListener(this); setLayout(new BorderLayout()); down.add(oCancel); add(down, BorderLayout.SOUTH); setVisible(true); } public void actionPerformed(ActionEvent e) { JButton s = (JButton)e.getSource(); core = Core.getInstance(); if(s.getText().equals("Cancel")){ core.showContent(); } } }
ResPanel.java (This is my Resource Panel which is where the button to call the option menu is at, it is also located at the very end of the file)
package TheGame; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ResPanel extends JPanel implements ActionListener{ public static ResPanel instance; private JButton rOption; private Core core; public static ResPanel getInstance(){ if(instance == null){ instance = new ResPanel(); } return instance; } private ResPanel() { rOption = new JButton("Options"); rOption.addActionListener(this); setLayout(new FlowLayout()); add(rOption); setVisible(true); } public void actionPerformed(ActionEvent e) { JButton s = (JButton)e.getSource(); core = Core.getInstance(); if(s.getText().equals("Options")){ core.showOption(); } } }