2017-04-03 37 views
1

輸入用戶的用戶名和密碼後,單擊按鈕將流發送到服務器以確定是否應該對用戶進行身份驗證。該按鈕停止工作,就像它已被禁用。爲什麼JButton被點擊後變爲非活動狀態?

public class EmployeeSignIn extends JFrame implements ActionListener { 
    private JLabel lblUsername, lblPassword; 
    private JTextField txtUsername; 
    private JPasswordField jpfPassword; 
    private JButton btnSubmit; 
    private JPanel pnlButton, pnlLogo, pnlComponents1, pnlComponents2; 
    private Socket socket; 
    private ObjectOutputStream oos; 
    private ObjectInputStream ois; 

    private void initializeStreamComponents() { 
     try { 
      socket = new Socket("localhost", 4200); 
      oos = new ObjectOutputStream(socket.getOutputStream()); 
      ois = new ObjectInputStream(socket.getInputStream()); 
     } catch (IOException e) { 
      System.err.println(e.getMessage()); 
     } 
    } 

    private void initComponent() { 
     initializeStreamComponents(); 

     lblUsername = new JLabel("Username"); 
     lblPassword = new JLabel("Password"); 

     txtUsername = new JTextField(); 

     jpfPassword = new JPasswordField(); 

     btnSubmit = new JButton("Sign In"); 

     pnlButton = new JPanel(); 
     pnlLogo = new JPanel(new GridLayout(1, 2)); 
     pnlComponents1 = new JPanel(new GridLayout(1, 2, 3, 3)); 
     pnlComponents2 = new JPanel(new GridLayout(1, 2, 3, 3)); 
    } 

    private void addComponentsToPanel() { 

     pnlLogo.add(new JLabel(new ImageIcon("img/app.png"))); 

     pnlComponents1.add(lblUsername); 
     pnlComponents1.add(txtUsername); 

     pnlComponents2.add(lblPassword); 
     pnlComponents2.add(jpfPassword); 

     pnlButton.add(btnSubmit); 
    } 

    private void addPanelsToWindow() { 
     Container container = getContentPane(); 
     container.add(pnlLogo); 
     container.add(pnlComponents1); 
     container.add(pnlComponents2); 
     container.add(pnlButton); 
    } 

    private void registerListeners() { 
     btnSubmit.addActionListener(this); 
    } 

    private void setWindowProperties() { 
     this.setTitle("ARD-Employee"); 
     this.setLayout(new GridLayout(4, 2)); 
     this.setVisible(true); 
     this.setSize(450, 200); 
     this.setResizable(false); 
     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     this.setLocationRelativeTo(null); 
    } 

    public EmployeeSignIn() { 
     this.initComponent(); 
     this.addComponentsToPanel(); 
     this.addPanelsToWindow(); 
     this.registerListeners(); 
     this.setWindowProperties(); 
    } 

    private boolean validateFields() { 
     if (!textFieldCheck(txtUsername)) { 
      txtUsername.grabFocus(); 
      System.out.println(1); 
      System.out.println("Please enter your username"); 
      System.out.println(2); 
      return false; 
     } 

     if (!textFieldCharacterCheck(txtUsername)) { 
      txtUsername.grabFocus(); 
      System.out.println("No Special Characters Allowed in Username!"); 
      return false; 
     } 

     if (!passwordFieldCheck(jpfPassword)) { 
      jpfPassword.grabFocus(); 
      System.out.println("Please Enter your Password!"); 
      return false; 
     } 
     return true; 
    } 

    private boolean textFieldCheck(JTextField field) { 
     if (field.getText().trim().isEmpty()) { 
      field.grabFocus(); 
      return false; 
     } 
     return true; 
    } 

    private boolean passwordFieldCheck(JPasswordField field) { 
     String password = new String(field.getPassword()); 
     if (password.trim().isEmpty()) { 
      return false; 
     } 
     return true; 
    } 

    private boolean textFieldCharacterCheck(JTextField field) { 
     return field.getText().matches("^\\d+$"); 
    } 

    public String getUsername() { 
     return txtUsername.getText(); 
    } 

    public String getPassword() { 
     return new String(jpfPassword.getPassword()); 
    } 

    @Override 
    public void actionPerformed(ActionEvent e) { 
     JButton src = (JButton) e.getSource(); 

     new Thread() { 
      @Override 
      public void run() { 
       if (src.equals(btnSubmit)) { 
        if (validateFields()) { 
         sendStreamsToServer(); 
        } 
       } 
      } 
     }.start(); 
    } 

    private void sendStreamsToServer() { 
     try { 

      String userType = "employee"; 
      oos.writeObject(userType); 

      oos.writeObject(this.getUsername()); 
      System.out.println("USERNAME: " + this.getUsername()); 
      oos.writeObject(this.getPassword()); 
      System.out.println("PASSWORD: " + this.getPassword()); 

      String readObj = (String) ois.readObject(); 
      System.out.println("READ OBJECT: " + readObj); 

      boolean validationCheck = (boolean) ois.readObject(); 
      System.out.println("Employee/VALIDATION CHECK: " + validationCheck); 

     } catch (IOException e1) { 
      System.err.println(e1.getMessage()); 
     } catch (ClassNotFoundException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 

    private void closeConnection() { 
     try { 
      oos.close(); 
      ois.close(); 
     } catch (IOException e) { 
      System.err.println(e.getMessage()); 
     } 
    } 

    public static void main(String[] args) { 
     new EmployeeSignIn(); 
    } 
} 

回答

4

ActionListener被連接到Socket。 A Socket阻止等待輸入。由於此代碼在Event Dispatch Thread (EDT)上執行,因此GUI會凍結。

解決方案是在單獨的Thread中連接到您的Socket

有關EDT的更多信息,請參閱Concurrency上的Swing教程。您可能需要考慮使用SwingWorker作爲您的Thread。在需要時,使用SwingWorker API可以更輕鬆地更新GUI。

+0

我對actionPerformed方法進行了更改。現在GUI在被點擊後不會凍結。但是,服務器僅在點擊按鈕後纔會響應一次。在隨後的點擊中,沒有任何內容從服務器發送到客戶端。你能告訴我什麼可能導致這種情況發生? – KMuir

+0

@KMuir要使用套接字,您需要一個「while循環」來持續等待來自套接字的數據。看看[Java教程](http://docs.oracle.com/javase/tutorial/index.html)。有'套接字'部分讓你開始。 – camickr

相關問題