I am trying to create a simple Java program with GUI, using Swing as the primary tool. I've trying to use CardLayout to handle switching "screens" (panels) within the same Frame and while there are tutorials online for that, most if not all of them assume the button lies outside of the panel I want to switch itself, while I'm trying to do the opposite where the button used to switch panels lie inside the panel themselves.So I've been trying to do just that, but I've been rather lost on how to even after looking for hours on the internet.
I've tried making a seperate method in Main to handle the panel switching and referring to it in Login, but that didn't work as I couldn't get it to pass the right parameters and other than that it keeps either throwing an error of returning null or not work entirely.
Here are the codes for the important files:
Main
package gui;
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Container;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Main extends JFrame {
private static final long serialVersionUID = 1L;
/**
* Create the frame.
*/
public Main() {
JFrame frame = new JFrame("Swing Frame");
Login loginpanel = new Login();
HomeMenu homemenu = new HomeMenu();
JPanel cards = new JPanel(new CardLayout());
cards.add(loginpanel, "Panel 1");
cards.add(homemenu, "Panel 2");
Container pane = frame.getContentPane();
pane.add(cards, BorderLayout.CENTER);
// Set the size of the frame
frame.setSize(900, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
Main frame = new Main();
frame.setVisible(false);
}
});
}
}
Login
package gui;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
public class Login extends JPanel {
private static final long serialVersionUID = 1L;
private JTextField textField;
private JPasswordField passwordField;
public Login getPanel() {
return this;
}
public Login() {
setBounds(100, 100, 900, 600);
setBorder(new EmptyBorder(5, 5, 5, 5));
setLayout(null);
JLabel lblNewLabel = new JLabel("BOOKSTORE MANAGEMENT SYS 1.0");
lblNewLabel.setBounds(12, 10, 878, 81);
lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
add(lblNewLabel);
lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 38));
JLabel lblNewLabel_1 = new JLabel("Enter Username:");
lblNewLabel_1.setBounds(217, 164, 180, 27);
lblNewLabel_1.setFont(new Font("Tahoma", Font.PLAIN, 23));
add(lblNewLabel_1);
textField = new JTextField();
textField.setBounds(433, 163, 255, 28);
add(textField);
textField.setColumns(10);
JLabel lblNewLabel_1_1 = new JLabel("Enter Password:");
lblNewLabel_1_1.setFont(new Font("Tahoma", Font.PLAIN, 23));
lblNewLabel_1_1.setBounds(217, 248, 180, 27);
add(lblNewLabel_1_1);
passwordField = new JPasswordField();
passwordField.setBounds(433, 253, 255, 28);
add(passwordField);
JButton btnNewButton = new JButton("Log in");
btnNewButton.setForeground(new Color(0, 0, 0));
btnNewButton.setFont(new Font("Tahoma", Font.PLAIN, 23));
btnNewButton.setBounds(347, 369, 176, 46);
add(btnNewButton);
btnNewButton.addActionListener((ActionListener) new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
}
});
}
}