1 year ago
#221616
kalana ekanayake
Java Card Layout only shows the first panel
Hi sorry if this has already been posted but my code doesn't work as it should. I am new to java programming and would like someone to point me in the right direction on how I could change to desired panels by pressing a button. In my code it only shows the first panel and when I press the button to show the next panel it gives me Wrong parent for CardLayout error. Also this is my first question, I hope you are clear what I'm trying to ask.
// **class Containing the cardLayout**
import java.awt.CardLayout;
import java.awt.Container;
import java.util.HashMap;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Frame extends JFrame{
IDandPassword idandPasswords = new IDandPassword();
JPanel login = new LoginPage(idandPasswords.getLoginInfo());
JPanel newUser = new NewUser();
Encryption enc = new Encryption();
Decryption dec = new Decryption();
CardLayout cl = new CardLayout();
Container panel = new Container();
void setGui() {
this.setSize(420,420);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setResizable(false);
this.setContentPane(panel);
System.out.println("Frame");
panel.setLayout(cl);
panel.add(login,"1");
panel.add(newUser,"2");
panel.add(enc,"3");
panel.add(dec,"4");
}
// **Class containing the 1st panel to show
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
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 LoginPage extends JPanel implements ActionListener{
JButton loginButton = new JButton("Login");
JButton resetButton = new JButton("Reset");
JButton newUserButton = new JButton("Add User");
JTextField UserIdField = new JTextField();
JPasswordField UserPasswordField = new JPasswordField();
JLabel userIdLabel = new JLabel("User ID");
JLabel userPasswordLabel = new JLabel("Password");
JLabel messageLabel = new JLabel();
HashMap <String,String> logininfo = new HashMap<String,String>();
LoginPage(HashMap<String,String> loginInfoOriginal) {
System.out.println("Login page constructor");
this.setSize(420,420);
this.setLayout(null);
this.setFocusable(true);
logininfo = loginInfoOriginal;
userIdLabel.setBounds(50,100,75,25);
userPasswordLabel.setBounds(50,150,75,25);
messageLabel.setBounds(125,303,250,35);
messageLabel.setFont(new Font(null,Font.BOLD,25));
UserIdField.setBounds(125,100,200,25);
UserPasswordField.setBounds(125,150,200,25);
loginButton.setBounds(125,200,100,25);
loginButton.setFocusable(false);
loginButton.addActionListener(this);
resetButton.setBounds(225,200,100,25);
resetButton.setFocusable(false);
resetButton.addActionListener(this);
newUserButton.setBounds(125,250,100,25);
newUserButton.setFocusable(false);
newUserButton.addActionListener(this);
this.add(userIdLabel);
this.add(userPasswordLabel);
this.add(messageLabel);
this.add(UserIdField);
this.add(UserPasswordField);
this.add(loginButton);
this.add(resetButton);
this.add(newUserButton);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == resetButton) {
UserIdField.setText("");
UserPasswordField.setText("");
messageLabel.setText("");
}
if(e.getSource() == loginButton) {
String userId = UserIdField.getText();
String password = String.valueOf(UserPasswordField.getPassword());
if(logininfo.containsKey(userId)) {
if(logininfo.get(userId).equals(password)) {
messageLabel.setForeground(Color.green);
messageLabel.setText("Login Successful");
}
else {
messageLabel.setForeground(Color.red);
messageLabel.setText("Incorrect Password");
}
}else {
messageLabel.setForeground(Color.red);
messageLabel.setText("Invalid Username");
}
}
if(e.getSource() == newUserButton) {
Frame f= new Frame();
f.cl.show(f.getContentPane(), "2");
}
}
}
//**class Containing 2nd panel to show
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.JTextField;
public class NewUser extends JPanel implements ActionListener{
JButton addButton = new JButton("Add");
JButton resetButton = new JButton("Reset");
JTextField newUserIdField = new JTextField();
JTextField newUserPasswordField = new JTextField();
JLabel newUserIdLabel = new JLabel("New User ID");
JLabel newuserPasswordLabel = new JLabel("New Password");
JLabel messageLabel = new JLabel();
JLabel titleLabel = new JLabel("Add a new User");
JButton backButton = new JButton("Back");
NewUser() {
this.setSize(420,420);
this.setLayout(null);
this.setFocusable(true);
newUserIdLabel.setBounds(22,100,75,25);
newuserPasswordLabel.setBounds(22,150,89,25);
messageLabel.setBounds(125,250,250,35);
messageLabel.setFont(new Font(null,Font.BOLD,25));
titleLabel.setFont(new Font("Tahoma", Font.BOLD, 14));
titleLabel.setBounds(125,43,108,35);
newUserIdField.setBounds(125,100,200,25);
newUserPasswordField.setBounds(125,150,200,25);
addButton.setBounds(125,200,100,25);
addButton.setFocusable(false);
addButton.addActionListener(this);
resetButton.setBounds(225,200,100,25);
resetButton.setFocusable(false);
resetButton.addActionListener(this);
backButton.setBounds(263,302,100,25);
backButton.addActionListener(this);
backButton.setFocusable(false);
this.add(newUserIdLabel);
this.add(newuserPasswordLabel);
this.add(messageLabel);
this.add(newUserIdField);
this.add(newUserPasswordField);
this.add(addButton);
this.add(resetButton);
this.add(addButton);
this.add(titleLabel);
this.add(backButton);
}
public void addUser() {
IDandPassword idandPasswords = new IDandPassword();
idandPasswords.setLoginInfo(newUserIdField.getText(),newUserPasswordField.getText());
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == backButton) {
IDandPassword idandPasswords = new IDandPassword();
Frame frame =new Frame();
frame.setContentPane(new LoginPage(idandPasswords.getLoginInfo()));
}
if(e.getSource() == addButton) {
IDandPassword idandPasswords = new IDandPassword();
if(idandPasswords.logininfo.containsKey(newUserIdField.getText())) {
System.out.println("User already exists");
}
else {
if(newUserIdField.getText() != "" && newUserPasswordField.getText() != "") {
addUser();
System.out.println("User added");
}
}
}
}
}
// **Main Class**
public class Main {
public static void main(String[] args) {
Frame f=new Frame();
f.setGui();
}
}
java
swing
layout-manager
cardlayout
0 Answers
Your Answer