2016-11-25 107 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(); 

但程序繼續,然後退出的主要方法並結束申請...

使用回調來驗證用戶事件...

2

您的願望是等待用戶完成他們的工作與應用程序窗口的響應,並且有幾種可能的方法來解決這個問題。最簡單的,和一個我建議你讓你的登錄窗口中的模式的JDialog,沒有一個JFrame。這樣,調用代碼的程序流程將在對話框可見時停止,一旦它關閉,調用程序的代碼流程將恢復,然後它可以查詢對話框的狀態並確定是否輸入了數據,以及是否已經輸入有效的數據。

另一種選擇是允許外部類偵聽器添加到登錄窗口,比如一個ActionListener的按鈕或WindowListener的,但是這是一個有點複雜處理。

例如

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); 
      } 
     }); 
    } 

}