i'm new to Java and JFrame. on my project, i'm learning to make multiple panels on the same JFrame using JPanel. but when i code a function of the button on my 1st JPanel (i want it to go show another JPanel) it gives me an error, here is my full code :
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/GUIForms/JFrame.java to edit this template
*/
package Main;
import GUI.Menu;
import java.awt.*;
/**
*
* @author rizkiaswangga
*/
public class Start extends javax.swing.JFrame {
/**
* Creates new form Start
*/
public Start() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
windowPanel = new javax.swing.JPanel();
jScrollPane1 = new javax.swing.JScrollPane();
jTextPane1 = new javax.swing.JTextPane();
jLayeredPane1 = new javax.swing.JLayeredPane();
Menu = new javax.swing.JPanel();
Browse = new javax.swing.JButton();
Library = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jTextPane1.setEditable(false);
jTextPane1.setFont(new java.awt.Font("sansserif", 0, 24)); // NOI18N
jTextPane1.setText("Game Store");
jScrollPane1.setViewportView(jTextPane1);
javax.swing.GroupLayout windowPanelLayout = new javax.swing.GroupLayout(windowPanel);
windowPanel.setLayout(windowPanelLayout);
windowPanelLayout.setHorizontalGroup(
windowPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(windowPanelLayout.createSequentialGroup()
.addGap(404, 404, 404)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
windowPanelLayout.setVerticalGroup(
windowPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(windowPanelLayout.createSequentialGroup()
.addGap(35, 35, 35)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(50, Short.MAX_VALUE))
);
jLayeredPane1.setLayout(new java.awt.CardLayout());
Browse.setText("Browse");
Browse.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
BrowseActionPerformed(evt);
}
});
javax.swing.GroupLayout MenuLayout = new javax.swing.GroupLayout(Menu);
Menu.setLayout(MenuLayout);
MenuLayout.setHorizontalGroup(
MenuLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, MenuLayout.createSequentialGroup()
.addContainerGap(813, Short.MAX_VALUE)
.addComponent(Browse)
.addGap(146, 146, 146))
);
MenuLayout.setVerticalGroup(
MenuLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, MenuLayout.createSequentialGroup()
.addContainerGap(231, Short.MAX_VALUE)
.addComponent(Browse)
.addGap(187, 187, 187))
);
jLayeredPane1.add(Menu, "card2");
jLabel1.setText("test");
javax.swing.GroupLayout LibraryLayout = new javax.swing.GroupLayout(Library);
Library.setLayout(LibraryLayout);
LibraryLayout.setHorizontalGroup(
LibraryLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(LibraryLayout.createSequentialGroup()
.addGap(188, 188, 188)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(790, Short.MAX_VALUE))
);
LibraryLayout.setVerticalGroup(
LibraryLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(LibraryLayout.createSequentialGroup()
.addGap(124, 124, 124)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(268, Short.MAX_VALUE))
);
jLayeredPane1.add(Library, "card3");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(windowPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addComponent(jLayeredPane1))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(windowPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLayeredPane1))
);
pack();
}// </editor-fold>
private void BrowseActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
CardLayout cardLayout = (CardLayout) getContentPane().getLayout();
cardLayout.show(getContentPane(), "Library");
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Start.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Start.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Start.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Start.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Start().setVisible(true);
}
});
}
public void switchPanels(Menu panel){
}
// Variables declaration - do not modify
private javax.swing.JButton Browse;
private javax.swing.JPanel Library;
private javax.swing.JPanel Menu;
private javax.swing.JLabel jLabel1;
private javax.swing.JLayeredPane jLayeredPane1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextPane jTextPane1;
private javax.swing.JPanel windowPanel;
// End of variables declaration
}
most of the code is generated automatically by Netbeans' JFrame Form Design tab. the only part that i modify is :
private void BrowseActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
CardLayout cardLayout = (CardLayout) getContentPane().getLayout();
cardLayout.show(getContentPane(), "Library");
}
i watched online tutorials on how to show JPanel from another JPanel, but on my cased it failed by giving me a message on the output whenever i click the button :
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: class javax.swing.GroupLayout cannot be cast to class java.awt.CardLayout (javax.swing.GroupLayout and java.awt.CardLayout are in module java.desktop of loader 'bootstrap')
at Main.Start.BrowseActionPerformed(Start.java:139)
at Main.Start$1.actionPerformed(Start.java:70)
at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1972)
at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2313)
at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405)
at java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262)
at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:279)
at java.desktop/java.awt.Component.processMouseEvent(Component.java:6620)
at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3398)
at java.desktop/java.awt.Component.processEvent(Component.java:6385)
at java.desktop/java.awt.Container.processEvent(Container.java:2266)
at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:4995)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2324)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4827)
at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4948)
at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4575)
at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4516)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2310)
at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2780)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4827)
at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:775)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:720)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:714)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:97)
at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:747)
at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:745)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:744)
at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
below is the screenshot of the Navigator tab of my components (at that part is showing the "Menu" panel which has the button that i would want to show the "Library" panel when clicked :
i'm sure something is wrong on my code :(...any help would be appreciated
I tried asking the code from ChatGPT and watching online tutorials