2017-12-04 14 views
0
ublic class TheMedicalResourceManagementSystem 
{ 
    JFrame jf; 
    JPanel jp; 
    JButton b; 
    JTextField t; 
    JTextField t2; 

    public static void main(String[] args) 
    { 
     TheMedicalResourceManagementSystem cc = new TheMedicalResourceManagementSystem(); 

     // SwingUtilities.invokeLater(() -> new GUI("The Medical Resource Management System").setVisible(true)); 
    } 

    public TheMedicalResourceManagementSystem() 
    { 



     jf = new JFrame("Frame"); 
     jp = new JPanel(); 
     jp.setLayout(new FlowLayout()); 
     jf.add(jp); 
     t = new JTextField(15); 
     jp.add(t); 
      t2 = new JTextField(15); 
     jp.add(t); 
     jp.add(t2); 

b= new JButton("Login"); 
jp.add(b); 

     jf.setSize(200,200); 
     jf.setVisible(true); 

     String username = t.getText(); 
     String password = t2.getText(); 

     b.addActionListener((ActionEvent e) -> { 
      if(username.equals("admin") && password.equals("password")) 
      { 
       SwingUtilities.invokeLater(() -> new GUI("Medical Remote Management System").setVisible(true)); 
      } 

      else 
      { 
       JOptionPane.showMessageDialog(null, "Incorrect username or password , Please try again"); 
      }}); 

} 


} 

私のプログラムの中で私はユーザー名とパスワードを入力したいと思っています。私は管理者とパスワードを入力するときにそれが実行されることはできません。JTextFieldとJButtonのログインページ

答えて

1

ほとんどのGUIフレームワークと同様に、Swingはイベント駆動型です。これは、情報が収集された時間に相対的であることを意味します。

上のあなたの例では、(ユーザに提示)UIが完全に実現される前であってもusernamepasswordを取得しているので、それだけで(おそらく空白です)のテキストフィールドの初期値

を返します。

代わりに、あなたはそれが例えば...

b.addActionListener((ActionEvent e) -> { 
    String username = t.getText(); 
    String password = t2.getText(); 
    if(username.equals("admin") && password.equals("password")) { 
     SwingUtilities.invokeLater(() -> new GUI("Medical Remote Management System").setVisible(true)); 
    } else { 
     JOptionPane.showMessageDialog(null, "Incorrect username or password , Please try again"); 
    } 
}); 
+0

が働いて、コードに最も関係だときだとして、ActionEventusernamepasswordを取得する必要があります!このフォームを隠す方法はどうですか?それはif内でのdisposeの使用を伴いますか? – YeBoi

+0

あなたのケースでは、私は "モーダルダイアログ"を使っていくつかの研究を行います。あなたが遭遇しようとしている他の問題は解決しますが、 'dispose'はウィンドウを処分する最も一般的な方法でしょうもっと長く必要な – MadProgrammer

関連する問題