2016-11-25 6 views
0

私は現在、チャットプログラムのログインフォームを開発しており、プログラムがフレームを読み込んでユーザー入力を待つようにしたいと考えています。 プログラムはフレームを開きますが、同時にメインメソッドを再開します。 私に助けてくれるアイデアがありましたら幸いです。JFrame - ユーザーがボタンを押すのを待つ

挨拶

public static void main(String[] args){ 

     boolean running = true; 

     //Starting JFrame 
     chatFrame.loginFrame(); 

      //Processing - Receiving Status from Login method 
      if(getStatus() == 1){ 

       ... 
      } else { 
       System.out.println("An Error occured.."); 
       System.exit(0); 
      } 

     } 

のJFrameクラス:

public class chatFrame{ 

    private static String sLogin; 
    private static String password; 


    public static void loginFrame(){ 
     System.out.println("Launching Frame"); 
     JFrame loginFrame = new JFrame(); 
     loginFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     JTextField user = new JTextField("Username"); 
     JTextField pass = new JTextField("Password"); 
     JButton login = new JButton("Login"); 

     loginFrame.setLayout(new BorderLayout()); 
     loginFrame.add(user, BorderLayout.NORTH); 
     loginFrame.add(pass, BorderLayout.CENTER); 
     loginFrame.add(login, BorderLayout.SOUTH); 

     loginFrame.pack(); 
     loginFrame.setSize(250, 150); 
     loginFrame.setVisible(true); 

      login.addActionListener(new ActionListener(){ 

       public void actionPerformed(ActionEvent e){ 
        System.out.println("Action performed"); 
        String sLogin = user.getText(); 
        String password = pass.getText(); 

        //Calling Login method 
        ClEngine.login(sLogin, password); 
        System.out.println("dataIn:" + dataIn); 
        loginFrame.setVisible(false); 
       } 
      }); 
    } 
} 
+0

を参照してください[複数JFramesの使用、グッド/悪い習慣?](http://stackoverflow.com/q/9554636/418556) –

答えて

0

これはあなたがフレームを開いているので、何が起こっている

chatFrame.loginFrame(); 

が、プログラムは継続を呼び出すことによっては、その後、mainメソッドを終了しますアプリケーションを終了します...

ユーザーイベントを確認するコールバックを使用します。

2

ユーザーがアプリケーションウィンドウでの作業を完了したら、応答を待つ必要があります。これを解決する方法はいくつかあります。一番簡単なのは、あなたのログインウィンドウを作ることです。モーダル JFrameではなくJDialogです。このようにして、ダイアログが表示されている間、呼び出しコードのプログラムフローは停止し、呼び出しプログラムのコードフローが再開され、ダイアログの状態を照会し、データが入力されているかどうかを調べることができます。有効なデータ。

もう1つの選択肢は、外部クラスがそのボタンまたはWindowListenerのActionListenerなどのリスナーをログインウィンドウに追加できるようにすることですが、これはやや複雑です。例えば

import java.awt.Window; 
import java.awt.BorderLayout; 
import java.awt.GridBagConstraints; 
import java.awt.Dialog.ModalityType; 
import java.awt.event.ActionEvent; 
import java.awt.event.KeyEvent; 
import java.awt.GridBagLayout; 
import java.awt.GridLayout; 
import java.awt.Insets; 

import javax.swing.*; 

public class ChatLogin extends JPanel { 
    private static JDialog dialog; 
    private static ChatLogin chatLogin; 
    private static boolean loginValid; 
    private JTextField userNameField = new JTextField(10); 
    private JPasswordField passwordField = new JPasswordField(10); 

    public ChatLogin() { 
     JPanel inputPanel = new JPanel(new GridBagLayout()); 
     GridBagConstraints gbc = new GridBagConstraints(); 
     gbc.gridx = 0; 
     gbc.gridy = 0; 
     gbc.anchor = GridBagConstraints.WEST; 
     gbc.fill = GridBagConstraints.HORIZONTAL; 
     gbc.insets = new Insets(2, 10, 2, 2); 
     gbc.weightx = 1.0; 
     gbc.weighty = 1.0; 
     inputPanel.add(new JLabel("User Name:"), gbc); 

     gbc.gridy = 1; 
     inputPanel.add(new JLabel("Password:"), gbc); 

     gbc.gridx = 1; 
     gbc.gridy = 0; 
     gbc.anchor = GridBagConstraints.EAST; 
     gbc.insets = new Insets(2, 2, 2, 2); 
     inputPanel.add(userNameField, gbc); 

     gbc.gridy = 1; 
     inputPanel.add(passwordField, gbc); 

     JPanel btnPanel = new JPanel(new GridLayout(1, 0, 2, 2)); 
     btnPanel.add(new JButton(new LoginAction())); 

     setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3)); 
     setLayout(new BorderLayout()); 
     add(inputPanel, BorderLayout.CENTER); 
     add(btnPanel, BorderLayout.PAGE_END); 
    } 

    public String getUserName() { 
     return userNameField.getText(); 
    } 

    public char[] getPassword() { 
     return passwordField.getPassword(); 
    } 

    public static boolean isLoginValid() { 
     return loginValid; 
    } 

    private class LoginAction extends AbstractAction { 
     public LoginAction() { 
      super("Login"); 
      putValue(MNEMONIC_KEY, KeyEvent.VK_L); 
     } 

     @Override 
     public void actionPerformed(ActionEvent e) { 
      loginValid = true; 
      Window win = SwingUtilities.getWindowAncestor(ChatLogin.this); 
      win.dispose(); 
     } 
    } 

    public static JDialog getInstance(Window win, String title) { 
     dialog = new JDialog(win, title, ModalityType.APPLICATION_MODAL) { 
      @Override 
      public void setVisible(boolean b) { 
       loginValid = false; 
       super.setVisible(b); 
      } 
     }; 
     chatLogin = new ChatLogin(); 
     dialog.add(chatLogin); 
     dialog.pack(); 
     dialog.setLocationRelativeTo(win); 
     return dialog; 
    } 

    public static JDialog getInstance() { 
     if (dialog == null) { 
      return getInstance(null, "Login"); 
     } else { 
      return dialog; 
     } 
    } 

    public static ChatLogin getChatLoginInstance() { 
     return chatLogin; 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(() -> { 
      ChatLogin.getInstance().setVisible(true); 
      if (ChatLogin.isLoginValid()) { 
       ChatLogin chatLogin = ChatLogin.getChatLoginInstance(); 
       String userName = chatLogin.getUserName(); 
       String password = new String(chatLogin.getPassword()); // not a safe thing to do 

       // here test that user name and password are valid 
       System.out.println("user name: " + userName); 
       System.out.println("Password: " + password); 
      } 
     }); 
    } 

} 
関連する問題