This week's book giveaway is in the Design forum.
We're giving away four copies of Building Microservices and have Sam Newman on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes What is wrong with my code? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Building Microservices this week in the Design forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What is wrong with my code?" Watch "What is wrong with my code?" New topic

What is wrong with my code?

Kev So

Joined: Jul 08, 2006
Posts: 2
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() {

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


southPanel = new JPanel();

ButtonHandler buttonHandler = new ButtonHandler();

login = new JButton("Login");


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

setSize(175, 110);

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!");
JOptionPane.showMessageDialog(null, "Wrong username/password!");


Jesus Angeles
Ranch Hand

Joined: Feb 26, 2005
Posts: 2066
Instead of

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

you can use

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

== and .equals work differently

you can look here
Kev So

Joined: Jul 08, 2006
Posts: 2
Thanks for the help! The .equals() worked perfectly!
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link:
subject: What is wrong with my code?
It's not a secret anymore!