我正在制作一个非常基本的登录提示GUI,用于下周我的Java考试的修订(这个练习可能会出现在考试中,所以这就是为什么我要先试试它)。基本上当你输入某个名字时,在这种情况下'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();
}
}
答案 0 :(得分:0)
使用string.equals比较字符串operator ==检查对字符串的引用是否相等