I am new to using cardlayout and I have a few questions on how to implement it. I first would like to know the best way to implement cardlayout so that I can switch from panel to panel. My main question is how to use buttons inside the constructors of my panels to switch from panel to panel. I just started working on this project today so you will see some code that is not finished or that does not make sense. I first tried to make all my classes to extend JFrame but that led to multiple unwanted windows. If I can get some sort of example on how to effectively use cardlayout and how to switch panels using the buttons within my other panel constructors. My attempts on using cardlayout only worked for buttons that I created in my gui class however that is not the result I want. Please help if you can and thanks in advance.
This is my main class.
public class controller{
public static void main(String[] args){
new Gui();
}
}
This is my gui class.
import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class Gui extends JFrame{
homePage home = new homePage();
public JPanel loginPanel = new textEmailLog(),
registerPanel = new Register(),
homePanel = new homePage(), viewPanel = new emailView(),
loadPanel = new loadEmail(), contentPanel = new JPanel(new CardLayout());
CardLayout cardLayout = new CardLayout();
public Gui(){
super("Email Database Program");
setSize(700,700);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contentPanel.setLayout(cardLayout);
contentPanel.add(loginPanel, "Login");
contentPanel.add(registerPanel, "Register");
contentPanel.add(homePanel, "Home");
contentPanel.add(viewPanel, "View");
contentPanel.add(loadPanel, "Load");
this.setContentPane(contentPanel);
cardLayout.show(contentPanel, "Login");
setVisible(true);
}
My Login Panel class
import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
//panel has been added
public class textEmailLog extends JPanel{
JPanel logGui = null;
JButton registerBut, logBut;
JTextField Login;
public textEmailLog(){
//default characteristics
setSize(700,700);
setName("Email Database");
//setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Labels for username and password
JLabel userLab = new JLabel("Username");
JLabel pwLab = new JLabel("Password");
//textfields for username and password
Login = new JTextField("", 15);
JPasswordField Password = new JPasswordField("", 15);
Password.setEchoChar('*');
//login button that confirms username and password
logBut = new JButton("Login");
logBut.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
//needs regex checks prior to logging in
if(e.getSource() == logBut){
/* Change the panel to the homepage
if(Login.getText().equals() && Password.getText().equals()){
}
*/
new homePage();
}
}
});
registerBut = new JButton("Register");
registerBut.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource() == registerBut){
//Change the panel to the register panel
}
}
});
//Box layout for organization of jcomponents
Box verticalContainer = Box.createVerticalBox();
Box horizontalContainer = Box.createHorizontalBox();
Box mergerContainer = Box.createVerticalBox();
//add to panel
verticalContainer.add(Box.createVerticalStrut(300));//begin relative to the center
verticalContainer.add(userLab);//Login label and text
verticalContainer.add(Login);
verticalContainer.add(Box.createVerticalStrut(5));
verticalContainer.add(pwLab);//password label and text
verticalContainer.add(Password);
verticalContainer.add(Box.createVerticalStrut(5));
horizontalContainer.add(registerBut);//register button
horizontalContainer.add(Box.createHorizontalStrut(5));
horizontalContainer.add(logBut);//login button
//combines both the vertical and horizontal container
mergerContainer.add(verticalContainer);
mergerContainer.add(horizontalContainer);
add(mergerContainer);
//this.add(logGui);
setVisible(true);
}
}
My Register panel class
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
//No Panel addition
public class Register extends JPanel{
JButton submit, previous;
JLabel firstLab, lastLab, userLab, passwordLab, repassLab, address, emailAdd, errorLabel;
JTextField firstText, lastText, userText, addText, emailAddText;
JPasswordField passwordText, repassText;
public Register(){
setSize(700,700);
//setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//register values and their labels and textfields
firstLab = new JLabel("First Name: ");
firstText = new JTextField("", 25);
lastLab = new JLabel("Last Name: ");
lastText = new JTextField("", 40);
userLab = new JLabel("Username: ");
userText = new JTextField("", 25);
passwordLab = new JLabel("Password: ");
passwordText = new JPasswordField("", 25);
passwordText.setEchoChar('*');
repassLab = new JLabel("Confirm Password");
repassText = new JPasswordField("", 25);
repassText.setEchoChar('*');
address = new JLabel("Address: ");
addText = new JTextField("", 50);
emailAdd = new JLabel("Email Address: ");
emailAddText = new JTextField("", 50);
//error message for reigstration issues
errorLabel = new JLabel();
errorLabel.setForeground(Color.red);
submit = new JButton("Submit");
submit.addActionListener(new ActionListener(){
@SuppressWarnings("deprecation")
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource() == submit){
//confirm with regex methods and if statements that have been created and need to be created
if(firstText.getText().length() == 0){
errorLabel.setText("*You have not entered a first name!");
}else
if(lastText.getText().length() == 0){
errorLabel.setText("*You have not entered a last name!");
}else
if(userText.getText().length() == 0){
errorLabel.setText("*You have not entered a username!");
}else
if(passwordText.toString().length() == 0){
errorLabel.setText("*You have not entered a Password!");
}else
if(repassText.toString().length() == 0){
errorLabel.setText("*You have not matched your password!");
}
if(addText.getText().length() == 0){
errorLabel.setText("*You have not entered an address!");
}else
if(emailAddText.getText().length() == 0){
errorLabel.setText("*You have not entered an Email address!");
}else
if(userText.getText().length() < 8){
errorLabel.setText("*Username is too short!");
}
else
if(!(passwordText.toString().matches(repassText.toString()))){
errorLabel.setText("Passwords do not match!");
}
else
if(!isValidEmailAddress(emailAddText.getText())){
errorLabel.setText("*Invalid Email!");
}
else
if(!isValidAddress(addText.getText())){
errorLabel.setText("*Invalid Address!");
}
}
}
});
//returns to the previous page
previous = new JButton("Previous Page");
previous.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource() == previous){
//card.show(login panel);
}
}
});
Box verticalContainer = Box.createVerticalBox();
Box horizontalContainer = Box.createHorizontalBox();
Box mergerContainer = Box.createVerticalBox();
verticalContainer.add(Box.createVerticalStrut(150));
verticalContainer.add(firstLab);
verticalContainer.add(firstText);
verticalContainer.add(Box.createVerticalStrut(5));
verticalContainer.add(lastLab);
verticalContainer.add(lastText);
verticalContainer.add(Box.createVerticalStrut(5));
verticalContainer.add(userLab);
verticalContainer.add(userText);
verticalContainer.add(Box.createVerticalStrut(5));
verticalContainer.add(passwordLab);
verticalContainer.add(passwordText);
verticalContainer.add(Box.createVerticalStrut(5));
verticalContainer.add(repassLab);
verticalContainer.add(repassText);
verticalContainer.add(Box.createVerticalStrut(5));
verticalContainer.add(address);
verticalContainer.add(addText);
verticalContainer.add(Box.createVerticalStrut(5));
verticalContainer.add(emailAdd);
verticalContainer.add(emailAddText);
verticalContainer.add(Box.createVerticalStrut(5));
//horizontal
verticalContainer.add(Box.createVerticalStrut(5));
horizontalContainer.add(submit);
horizontalContainer.