2016-07-13 4 views
0

JPasswordFieldを使用してログイン機能のユーザー名とパスワードを取得しています。 .getPasswordはパスワードに対しては機能しますが、ユーザー名には機能しません。私は同じ正確なものを入力した場合、なぜそれはユーザー名のために働いていないすべてのアイデア?ここでJava JPasswordField .getPassword()の問題

は私のコードです:

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 
import java.util.Arrays; 

class pros 
{ 
    JFrame frame; 
    private static void show(){ 
    JFrame frame = new JFrame("LOGIN"); 
    frame.setSize(450,450); 
    frame.setLayout(new GridLayout(0,1)); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    JPanel panel = new JPanel(); 
    panel.setBackground(Color.BLACK); 

    JLabel username = new JLabel("Enter the Username: ",JLabel.LEFT); 
    username.setForeground(Color.WHITE); 
    username.setFont(username.getFont().deriveFont(24f)); 
    panel.add(username); 

    JPasswordField user = new JPasswordField(10); 
    user.setEchoChar('?'); 
    panel.add(user); 

    JLabel password = new JLabel("Enter your Password: ",JLabel.RIGHT); 
    password.setForeground(Color.WHITE); 
    password.setFont(password.getFont().deriveFont(24f)); 
    panel.add(user); 
    panel.add(password); 

    JPasswordField field = new JPasswordField(10); 
    field.setEchoChar('•'); 

    JButton ok = new JButton("OK"); 
    ok.addActionListener(new ActionListener(){ 
     public void actionPerformed(ActionEvent e) 
     { 
      String input = String.valueOf(field.getPassword()); 
      String user = String.valueOf(user.getPassword()); 
      if(input.equals("double" && user.equals("relocating"))) 
      { 
       field.setText(""); 
       pane(); 
     } 
    }}); 
    panel.add(field); 
    panel.add(ok); 
    frame.add(panel); 
    frame.setVisible(true); 
} 

答えて

0

使用 のSystem.out.println(入力+」「+ユーザー) 文は単に変数の値が何であるかを見るためにならば、あなたが知っている前に、問題とあなたが文字列に同じ名前を使用しているので、あなたは

{ 
     String input = String.valueOf(field.getPassword()); 
     String user = String.valueOf(user.getPassword()); 
     System.out.println(input+" "+user);//here print see the consul to know why it's not working 
     if(input.equals("double" && user.equals("relocating"))) 
     { 
      field.setText(""); 
      pane(); 
    } 

であってもよい、それを修正することができますし、フィールド

+0

または、文字列とフィールドに同じ名前を使用している可能性があります。 – Cherif

0

ありますあなたのコードのいくつかの間違い。まず、親パネルに 'user' JPasswordFieldを2回追加します。最初の時間はここで行われている:

JPasswordField user = new JPasswordField(10); 
user.setEchoChar('?'); 
panel.add(user); 

は、コンポーネントと、ここ1、初期化後:第二に

JLabel password = new JLabel("Enter your Password: ",JLabel.RIGHT); 
password.setForeground(Color.WHITE); 
password.setFont(password.getFont().deriveFont(24f)); 
panel.add(user); 
panel.add(password); 

、あなたが一緒に(ボタンの初期化後にパネル「フィールド」にあなたのコンポーネントを追加する理由をリスナーと一緒に)。

第3に、ユーザー名はどのくらいですか? 10文字より長い場合は、サイズが制限されているので動作しません。