代码如下:
import java.awt.Dimension;
import java.awt.Rectangle;import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingConstants;import adminDB.QueryTerm;public class AdminLogin extends JFrame {    private static JLabel title = null;
private static JFrame loginFrame = null;  //  @jve:decl-index=0:visual-constraint="57,36"
private static JPanel loginContentPane = null;
private static JLabel userLabel = null;
private static JTextField userTextField = null; 
private static JLabel passwordLabel = null;
private static JPasswordField jPasswordField = null;
private static JButton loginButton = null;
private static JButton cancelButton = null; /**
 * This method initializes loginFrame
 * 
 * @return javax.swing.JFrame
 */
private static JFrame getLoginFrame() {
if (loginFrame == null) {
loginFrame = new JFrame();
loginFrame.setSize(new Dimension(368, 228));
loginFrame.setTitle("成功学院");
loginFrame.setContentPane(getLoginContentPane());
loginFrame.setVisible(true);
}
return loginFrame;
} /**
 * This method initializes loginContentPane
 * 
 * @return javax.swing.JPanel
 */
private static JPanel getLoginContentPane() {
if (loginContentPane == null) {
passwordLabel = new JLabel();
passwordLabel.setBounds(new Rectangle(80, 85, 49, 18));
passwordLabel.setHorizontalAlignment(SwingConstants.CENTER);
passwordLabel.setText("密码");
userLabel = new JLabel();
userLabel.setBounds(new Rectangle(80, 50, 50, 18));
userLabel.setHorizontalAlignment(SwingConstants.CENTER);
userLabel.setText("用户名");
title = new JLabel();
title.setBounds(new Rectangle(150,10,80,18));
title.setText("管理员登陆");
loginContentPane = new JPanel();
loginContentPane.setLayout(null);
loginContentPane.add(title, null);
loginContentPane.add(userLabel, null);
loginContentPane.add(getUserTextField(), null);
loginContentPane.add(passwordLabel, null);
loginContentPane.add(getJPasswordField(), null);
loginContentPane.add(getLoginButton(), null);
loginContentPane.add(getCancelButton(), null);
}
return loginContentPane;
} /**
 * This method initializes userTextField
 * 
 * @return javax.swing.JTextField
 */
private static JTextField getUserTextField() {
if (userTextField == null) {
userTextField = new JTextField();
userTextField.setBounds(new Rectangle(170, 50, 85, 22));
}
return userTextField;
} /**
 * This method initializes jTextArea
 * 
 * @return javax.swing.JTextArea
 */
private static JPasswordField getJPasswordField() {
if (jPasswordField == null) {
jPasswordField = new JPasswordField();
jPasswordField.setBounds(new Rectangle(170, 85, 85, 22));
}
return jPasswordField;
} /**
 * This method initializes loginButton
 * 
 * @return javax.swing.JButton
 */
private static JButton getLoginButton() {
if (loginButton == null) {
loginButton = new JButton();
loginButton.setBounds(new Rectangle(90, 122, 70, 30));
loginButton.setText("登陆");
loginButton.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent e) {
if (userTextField.getText() == "mayouzhong" && jPasswordField.getText() == "liuyanyan") 
{
JOptionPane.showMessageDialog(null, "登陆成功!");
//QueryTerm queryTerm = new QueryTerm();
//queryTerm.setVisible(true);

else 
{
if (userTextField.getText() != "mayouzhong") 
{
JOptionPane.showMessageDialog(null, "用户名错误,请重新输入!");

else 
{
JOptionPane.showMessageDialog(null, "密码错误,请重新输入!");
}
}
}
});
}
return loginButton;
} /**
 * This method initializes cancelButton
 * 
 * @return javax.swing.JButton
 */
private static JButton getCancelButton() {
if (cancelButton == null) {
cancelButton = new JButton();
cancelButton.setBounds(new Rectangle(175, 122, 70, 30));
cancelButton.setText("取消");
}
return cancelButton;
}


public static void main(String []args) {

getLoginFrame();
getLoginContentPane();
getUserTextField();
getJPasswordField();
getLoginButton();
getCancelButton(); }} 为什么输入“mayouzhong”后总是出现“用户名错误,请重新输入”?
请高手指教!

解决方案 »

  1.   

    if (userTextField.getText().equals("mayouzhong")) 
      

  2.   

    if (userTextField.getText() == "mayouzhong" && jPasswordField.getText() == "liuyanyan") 
    字符串比较用equals 
    if (userTextField.getText().equals("mayouzhong") && jPasswordField.getText().equals("liuyanyan")) 
      

  3.   

    因为你用的 ==
    字符串比较要用equals
      

  4.   

    也可以用str.equalsIgnoreCase(anotherString),该方法忽略大小写。
      

  5.   

    if (userTextField.getText() != "mayouzhong") 改为 if("mayouzhong".equals(userTextField.getText()))