• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

What is wrong with my code?

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When I enter the correct username and password into the textfields it still displays "Wrong username/password!" Could someone please tell what I am doing wrong in my code! Thanks!



import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class LoginFrame extends JFrame {
private JFrame loginFrame;
private JPanel northPanel, southPanel;
private JLabel username, password;
private JTextField user, pass;
private JButton login;

public LoginFrame() {
super("Login...");

loginFrame = new JFrame();

northPanel = new JPanel();
northPanel.setLayout(new GridLayout(2, 2));

username = new JLabel("Username:", username.CENTER);
password = new JLabel("Password:", password.CENTER);

user = new JTextField(15);
pass = new JTextField(15);

northPanel.add(username);
northPanel.add(user);
northPanel.add(password);
northPanel.add(pass);

southPanel = new JPanel();

ButtonHandler buttonHandler = new ButtonHandler();

login = new JButton("Login");
login.addActionListener(buttonHandler);

southPanel.add(login);

getContentPane().add("North", northPanel);
getContentPane().add("South", southPanel);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(175, 110);
setVisible(true);
setResizable(false);
}

public static void main(String[] args) {
LoginFrame login = new LoginFrame();
}

private class ButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent event) {

if(user.getText() == "username" && pass.getText() == "password")
JOptionPane.showMessageDialog(null, "Welcome!");
else
JOptionPane.showMessageDialog(null, "Wrong username/password!");

}
}

}
 
Ranch Hand
Posts: 2108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Instead of

if(user.getText() == .............

you can use

user.getText().equals(.......) ..............

== and .equals work differently

you can look here
 
Kev So
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the help! The .equals() worked perfectly!
 
Been there. Done that. Went back for more. But this time, I took this tiny ad with me:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic