I am working on a JPanel that has a username textbox and a password textbox. I felt like the code i have should check if the input equals the preset user and pass but it keeps saying I am wrong. Could someone please help fix this. Also, do not just link another question I have searched for hours upon hours and i cannot seem to get this to work. Furthermore, i would like when the continue button is pressed and inputs are right for a new JFrame to come up and dispose of this panel(totally optional). Please consider I am new to programming so there might be stuff below that can be simplified or just unnecessary.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.awt.*;
import java.util.Scanner;
public class EnterButtonListener implements ActionListener {
String USERNAME = "user";
String PASSWORD = "pass";
String userText, passwordText;
public JFrame authFrame;
public void actionPerformed(ActionEvent arg0) {
JFrame authFrame = new JFrame();
authFrame.setSize(400, 300);
authFrame.setLocationRelativeTo(null);
authFrame.setDefaultCloseOperation(authFrame.DISPOSE_ON_CLOSE);
JPanel panel = new JPanel();
authFrame.add(panel);
placeComponents(panel);
authFrame.setVisible(true);
//make new JFrame appear
}
public void placeComponents(JPanel panel) {
panel.setLayout(null);
JLabel atlasIcon = new JLabel();
atlasIcon.setIcon(new ImageIcon("/users/mfyhr/Pictures/Atlas2.png"));
atlasIcon.setBounds(50, 5, 400, 100);
panel.add(atlasIcon);
JLabel authLabel = new JLabel("Authentication Required");
authLabel.setBounds(90, 85, 400, 25);
authLabel.setFont(new Font("Bank Gothic", Font.TRUETYPE_FONT, 16));
panel.add(authLabel);
JLabel infoLabel = new JLabel("Please enter your credentials to continue.");
infoLabel.setBounds(27, 112, 400, 25);
infoLabel.setFont(new Font("Bank Gothic", Font.TRUETYPE_FONT, 14));
panel.add(infoLabel);
JLabel userLabel = new JLabel("Username");
userLabel.setBounds(80, 150, 100, 25);
userLabel.setFont(new Font("Bank Gothic", Font.TRUETYPE_FONT, 12));
panel.add(userLabel);
JTextField userText = new JTextField(20);
userText.setBounds(170, 150, 150, 25);
userText.setFont(new Font("Bank Gothic", Font.TRUETYPE_FONT, 12));
panel.add(userText);
JLabel passwordLabel = new JLabel("Password");
passwordLabel.setBounds(80, 180, 100, 25);
passwordLabel.setFont(new Font("Bank Gothic", Font.TRUETYPE_FONT, 12));
panel.add(passwordLabel);
JPasswordField passwordText = new JPasswordField(20);
passwordText.setBounds(170, 180, 150, 25);
passwordText.setFont(new Font("Bank Gothic", Font.TRUETYPE_FONT, 12));
panel.add(passwordText);
JButton continueButton = new JButton("Continue");
continueButton.setBounds(185, 223, 85, 33);
continueButton.setFont(new Font("Bank Gothic", Font.TRUETYPE_FONT, 12));
panel.add(continueButton);
JButton cancelButton = new JButton("Cancel");
cancelButton.setBounds(290, 223, 85, 33);
cancelButton.setFont(new Font("Bank Gothic", Font.TRUETYPE_FONT, 12));
panel.add(cancelButton);
final String username = userText.getText();
final String password = passwordText.getText();
continueButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae)
{
if ((username == USERNAME) & (password == PASSWORD))
{
JOptionPane.showMessageDialog(null,"Access Granted");
}
else
{
JOptionPane.showMessageDialog(null,"Access Denied");
}
}
});
//continueButton.addActionListener(new ActionListener(){
//public void actionPerformed(ActionEvent ae)
//{
//check user and password textboxes
//if both = myuser and pass
//hide Menu and close enterbuttonlistener
//open new JFrame
// }
//ActionListener continueButtonListener = new ContinueButtonListener();
//continueButton.addActionListener(continueButtonListener);
//ActionListener cancelButtonListener = new CancelButtonListener();
//cancelButton.addActionListener(cancelButtonListener);
}
}