条件没有被检查。Java,AWT

huangapple go评论81阅读模式
英文:

The condition is not checking. Java, AWT

问题

我正在使用Java的AWT编写一个小型登录表单。在检查登录的用户输入时遇到了一个问题:当用户将“admin”输入为登录名并将“password”输入为密码时,程序会输出“成功!”,但无论如何我都会收到“错误!”消息,即使输入的数据满足条件。

因此,我应该如何调整程序以正确检查条件?

代码:

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

public class Login extends JFrame {
    private JButton button = new JButton("确认");
    private JLabel label1 = new JLabel("登录:");
    private JTextField login = new JTextField("", 8);
    private JLabel label2 = new JLabel("密码:");
    private JTextField pswrd = new JTextField("", 8);

    public Login() {
        super("请登录");
        this.setBounds(100, 100, 250, 100);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Container container = this.getContentPane();
        container.setLayout(new GridLayout(3, 2, 2, 2));
        container.add(label1);
        container.add(label2);
        container.add(login);
        container.add(pswrd);
        button.addActionListener(new ButtonEvent());
        container.add(button);
    }

    class ButtonEvent implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            if (login.getText().equals("admin") && pswrd.getText().equals("password")) {
                JOptionPane.showMessageDialog(null, "成功!", "输出", JOptionPane.PLAIN_MESSAGE);
            } else {
                JOptionPane.showMessageDialog(null, "错误!", "输出", JOptionPane.PLAIN_MESSAGE);
            }
        }
    }

    public static void main(String[] args) {
        Login app = new Login();
        app.setVisible(true);
    }
}

请注意,我只提供了代码的翻译部分,没有包含额外的内容。

英文:

I am writing a small login form at Java using AWT. Faced a problem while checking user input for login: I need, when user inputed "admin" as login and "password" as password, program outputting "Succes!", but anyway I get "Wrong!" message, even if inputed data meet the condition.

条件没有被检查。Java,AWT

So, what should I do so that the program checks the condition correctly?

Code:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Login extends JFrame {
private JButton button = new JButton("Confirm");
private JLabel label1 = new JLabel("Login:");
private JTextField login = new JTextField("", 8);
private JLabel label2 = new JLabel("Password:");
private JTextField pswrd = new JTextField("", 8);
public Login() {
super("Please log in");
this.setBounds(100, 100, 250, 100);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container container = this.getContentPane();
container.setLayout(new GridLayout(3,2,2,2));
container.add(label1);
container.add(label2);
container.add(login);
container.add(pswrd);
button.addActionListener(new ButtonEvent());
container.add(button);
}
class ButtonEvent implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (login.getText() == "admin" && pswrd.getText() == "password")
{
JOptionPane.showMessageDialog(null, "Succes!", "Output", JOptionPane.PLAIN_MESSAGE);
}
else
{
JOptionPane.showMessageDialog(null, "Wrong!", "Output", JOptionPane.PLAIN_MESSAGE);
}
}
}
public static void main(String[] args) {
Login app = new Login();
app.setVisible(true);
}
}

答案1

得分: 2

只需尝试使用.equals(),而不是使用==

英文:

Just try .equals() instead of ==

huangapple
  • 本文由 发表于 2020年5月5日 20:19:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/61612988.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定