2015-09-07 84 views
0

如果用戶名和密碼正確,則進行了登錄,可以打開圖片。我把它當做一個圖形用戶界面,但問題是,在我編寫用戶名和密碼並單擊登錄後,沒有任何反應。該項目工程在控制檯(沒有GUI)需要幫助才能在框架中打開新框架

import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.net.URL; 

import javax.swing.ImageIcon; 
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; 

public class LoginView { 
private static final String IMG_FILE_PATH = "index.jpg"; 
private static final String USERNAME = "Hudhud"; 
private static final String PASSWORD = "123"; 

public static void main(String[] args) { 
    JFrame frame = new JFrame("Login"); 
    frame.setSize(300, 160); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    JPanel panel = new JPanel(); 
    frame.add(panel); 
    placeComponents(panel); 

    frame.setVisible(true); 
} 

private static void placeComponents(JPanel panel) { 

    panel.setLayout(null); 

    JLabel userLabel = new JLabel("User"); 
    userLabel.setBounds(10, 10, 80, 25); 
    panel.add(userLabel); 

    final JTextField userText = new JTextField(20); 
    userText.setBounds(100, 10, 160, 25); 
    panel.add(userText); 

    JLabel passwordLabel = new JLabel("Password"); 
    passwordLabel.setBounds(10, 40, 80, 25); 
    panel.add(passwordLabel); 

    final JPasswordField passwordText = new JPasswordField(20); 
    passwordText.setBounds(100, 40, 160, 25); 
    panel.add(passwordText); 

    JButton loginButton = new JButton("login"); 
    loginButton.setBounds(10, 80, 80, 25); 
    panel.add(loginButton); 

    loginButton.addActionListener(new ActionListener() { 

     public void actionPerformed(ActionEvent e) { 
      if (userText.equals(USERNAME)&& passwordText.equals(PASSWORD)) { 
       URL url = LoginView.class.getResource(IMG_FILE_PATH); 
       ImageIcon icon = new ImageIcon(url); 
       JFrame f = new JFrame(); 
       f.setSize(300, 300); 
       JLabel label = new JLabel(icon); 
       f.add(label); 
       f.setVisible(true); 
       f.setDefaultCloseOperation(f.EXIT_ON_CLOSE); 
      } else { 



      } 
     } 
    }); 
} 

} 

編輯:

import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.net.URL; 
import java.util.Arrays; 

import javax.swing.ImageIcon; 
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; 

public class LoginView 
{ 
private static final String IMG_FILE_PATH = "index.jpg"; 
private static final String USERNAME = "Hudhud"; 
private static final String PASSWORD = "123"; 

public static void main(String[] args) 
{ 
    JFrame frame = new JFrame("Login"); 
    frame.setSize(300, 160); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    JPanel panel = new JPanel(); 
    frame.add(panel); 
    placeComponents(panel); 

    frame.setVisible(true); 
    } 

    private static void placeComponents(JPanel panel) 
    { 

    panel.setLayout(null); 

    JLabel userLabel = new JLabel("User"); 
    userLabel.setBounds(10, 10, 80, 25); 
    panel.add(userLabel); 

    final JTextField userText = new JTextField(20); 
    userText.setBounds(100, 10, 160, 25); 
    panel.add(userText); 

    JLabel passwordLabel = new JLabel("Password"); 
    passwordLabel.setBounds(10, 40, 80, 25); 
    panel.add(passwordLabel); 

    final JPasswordField passwordText = new JPasswordField(20); 
    passwordText.setBounds(100, 40, 160, 25); 
    panel.add(passwordText); 

    JButton loginButton = new JButton("login"); 
    loginButton.setBounds(10, 80, 80, 25); 
    panel.add(loginButton); 

    loginButton.addActionListener(new ActionListener() 
    { 

     public void actionPerformed(ActionEvent e) 
     { 

      if ((userText.getText()).equals(USERNAME)&& (passwordText.getPassword().toString()).equals(PASSWORD)) 
      { 
       URL url = LoginView.class.getResource(IMG_FILE_PATH); 
       ImageIcon icon = new ImageIcon(url); 
       JFrame frame = new JFrame(); 
       frame.setSize(300, 300); 
       JLabel label = new JLabel(icon); 
       frame.add(label); 
       frame.setVisible(true); 
       frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE); 
      } 

      else { 
       JOptionPane.showMessageDialog(null, "You ain't the boss"); 
      } 
     } 
    }); 
} 

}

我得到這個錯誤,但我不知道如何解決它。當我寫用戶名和密碼時,它會跳過if語句,並給我else語句,即使用戶名和密碼是正確的。

+2

您正在調用圖形組件上的equals方法,該方法使用Object的標準equals方法。您需要將「JTextField#getText」或「JPasswordField#getPassword」與實際用戶名或密碼進行比較 – SomeJavaGuy

回答

0

如果你看看文檔,你會直接equals()方法看到ObjectJPasswordField繼承,其中規定:

的等於Object類的方法實現對象上最挑剔的 可能等價關係;也就是說,對於任何非空 引用值x和y,當且僅當x 和y引用同一對象(x == y的值爲true)時,此方法返回true。

這顯然不是JPasswordFieldString的情況。因此,您必須將JPasswordField的內容與給定的String進行比較。

如果再次查看文檔,您會看到通常用於輸入文本的某個組件的文本的獲取方式,出於安全原因,已棄用getText()。確切的原因與事實String是一個對象,它會留在內存中,直到垃圾清理器啓動,這不是我們想要的。一旦我們不需要它,我們就想從內存中移除密碼,所以正確的方法是使用char數組來控制我們的位置。正確的方法是:

char[] passwordInput = passwordText.getPassword(); 
if ((userText.getText()).equals(USERNAME)&& 
    (Arrays.equals(password.toCharArray(), passwordInput)) { 
    //do something 
} else { 
    //do something else, e. g. inform the user about the error 
} 
Arrays.fill(passwordInput, '0'); //clearing out password from memory 
+0

'Arrays.fill(passwordInput,'0');''之後爲什麼不能寫一個else語句 – user4476151

+0

其他語句與'if'綁定,所以它應該立即跟着它,在它之後留下Arrays.fill。我已經更新了我的答案。 – Luke

1

試試這個

if ((userText.getText()).equals(USERNAME)&& (passwordText.getPassword().toString()).equals(PASSWORD)) { 

您應該使用的getText()方法用於比較值。在你的代碼中,你使用Object類的equals方法而不是String類。

+1

「JPasswordField#getText」已棄用且不應使用。 – SomeJavaGuy

+0

@KevinEsche Thanks Now代碼更新 –

+0

有兩個問題:'char []'是一個數組,而'toString()'會返回垃圾,其次,如果我理解了它的話,那麼你就會重新實現同樣的弱點。 'getText()'已被棄用。看到我的答案。 – Luke