一天晚上我在做一些编程,但遇到了一个问题。似乎我的按钮的动作侦听器中的 if 语句没有被拾取。我对编程很陌生,所以我希望这不是太明显有什么建议吗?这是代码:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.*;
import java.util.*;
import javax.swing.*;
public class Login extends JFrame implements ActionListener {
JTextField tfUsername, tfPassword;
JLabel jlUser, jlPass;
JTextArea tarea;
JButton bLogin;
public Login(){
JFrame frame = new JFrame("Login Test");
JPanel panel = new JPanel();
jlUser = new JLabel("Username");
tfUsername = new JTextField(20);
jlPass = new JLabel("Password");
tfPassword = new JTextField(20);
bLogin = new JButton("Login");
tarea = new JTextArea();
frame.setSize(335,150);
frame.add(panel);
panel.add(jlUser);
panel.add(tfUsername);
panel.add(jlPass);
panel.add(tfPassword);
panel.add(bLogin);
panel.add(tarea);
bLogin.addActionListener(this);
frame.setResizable(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args) {
new Login();
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource() == bLogin){
String user = tfUsername.getText();
String pass = tfPassword.getText();
String be = "hi";
if(pass == be){
tarea.setText("Successful");
}
}
}