0

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);


 }
 }
  • Avoid using `null` layouts, pixel perfect layouts are an illusion within modern ui design. There are too many factors which affect the individual size of components, none of which you can control. Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify – MadProgrammer May 20 '15 at 02:24
  • You may find that a modal dialog is more suited to the job then another `JFrame`. See [How to Make Dialogs](http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html) for more details – MadProgrammer May 20 '15 at 02:25
  • final String username = userText.getText(); final String password = passwordText.getText(); continueButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { if ((username.equals(USERNAME)) && (password.equals(PASSWORD))) { JOptionPane.showMessageDialog(null,"Access Granted"); } else { JOptionPane.showMessageDialog(null,"Access Denied"); } } }); – Magnus Fyhr May 20 '15 at 05:13
  • also how do you suggest i layout the panel – Magnus Fyhr May 20 '15 at 05:19
  • If you had attempted to debug the code or placed `System.out.println` statements in to test the actual values of your variables, you would find that `username` and `password` were blank every time the `ActionListener` was triggered. Instead you should inspect the values of the fields only when the `ActionListener` is actually called, because when you assign the values to `username` and `password`, the fields are still blank and haven't been filled out by the user yet... – MadProgrammer May 20 '15 at 05:19
  • Personally, I'd use a `GridBagLayout`, but you might find that a little intimidating. It's complex, but powerful – MadProgrammer May 20 '15 at 05:21
  • Thank you so much for the help. i was able to keep the "pixel perfect" layout by creating a boolean and some string along with .contains(); in order to check the password. – Magnus Fyhr May 20 '15 at 17:58
  • *"I was able to keep the pixel perfect layout"* right up until you run it on a different machine/os – MadProgrammer May 20 '15 at 21:55
  • So, if use "some user" and "some pass" it would be pass your condition check? – MadProgrammer May 20 '15 at 21:56

0 Answers0