GUI 中的 If 语句出错(总是落入 ELSE 而不是 IF)
Error with an If Statement within a GUI (Always falls into ELSE never IF)
我正在为我下周的 Java 考试做一个非常基本的登录提示 GUI 修改(这个练习可能会出现在考试中所以这就是为什么我试图首先获得它)。基本上,当您输入某个名称时,在本例中 'joe' 会显示已验证,否则会显示未验证。但是,即使我输入 joe,它仍然会说未验证。谁能看到我哪里出错了?谢谢。
package gui.eventHandler;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class EventHandlerExample2_NameConfirmation {
private JFrame frame;
private JPanel panel;
private JButton button1;
private JLabel label;
private JTextField fname;
public EventHandlerExample2_NameConfirmation() {
frame = new JFrame("Log In");
panel = new JPanel();
label = new JLabel("First Name: ");
fname = new JTextField(20);
// Actions for when the OK button is clicked
// Implementing the ActionListener as an anonymous inner class
button1 = new JButton("Submit");
button1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String inputname = fname.getText();
if (inputname == "joe") {
JOptionPane.showMessageDialog(null, "Verified user " + inputname);
} else {
//selects this option regardless of entry
JOptionPane.showMessageDialog(null, "Invalid user " + inputname);
}
}
});
panel.add(label); // add the label
panel.add(fname); // textField
panel.add(button1); // and button to the panel
frame.add(panel); // add panel onto the frame
frame.setVisible(true); // By default its invisible
frame.setSize(600, 70); // 600 width 70 height
frame.setLocation(350, 350); // roughly center screen
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // exit when closed
}
public static void main(String[] args) {
EventHandlerExample2_NameConfirmation app = new EventHandlerExample2_NameConfirmation();
}
}
使用 string.equals 比较字符串运算符 == 检查对字符串的引用是否相等
我正在为我下周的 Java 考试做一个非常基本的登录提示 GUI 修改(这个练习可能会出现在考试中所以这就是为什么我试图首先获得它)。基本上,当您输入某个名称时,在本例中 'joe' 会显示已验证,否则会显示未验证。但是,即使我输入 joe,它仍然会说未验证。谁能看到我哪里出错了?谢谢。
package gui.eventHandler;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class EventHandlerExample2_NameConfirmation {
private JFrame frame;
private JPanel panel;
private JButton button1;
private JLabel label;
private JTextField fname;
public EventHandlerExample2_NameConfirmation() {
frame = new JFrame("Log In");
panel = new JPanel();
label = new JLabel("First Name: ");
fname = new JTextField(20);
// Actions for when the OK button is clicked
// Implementing the ActionListener as an anonymous inner class
button1 = new JButton("Submit");
button1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String inputname = fname.getText();
if (inputname == "joe") {
JOptionPane.showMessageDialog(null, "Verified user " + inputname);
} else {
//selects this option regardless of entry
JOptionPane.showMessageDialog(null, "Invalid user " + inputname);
}
}
});
panel.add(label); // add the label
panel.add(fname); // textField
panel.add(button1); // and button to the panel
frame.add(panel); // add panel onto the frame
frame.setVisible(true); // By default its invisible
frame.setSize(600, 70); // 600 width 70 height
frame.setLocation(350, 350); // roughly center screen
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // exit when closed
}
public static void main(String[] args) {
EventHandlerExample2_NameConfirmation app = new EventHandlerExample2_NameConfirmation();
}
}
使用 string.equals 比较字符串运算符 == 检查对字符串的引用是否相等