2016-05-23 9 views
0

Ich versuche, eine LAN-Kommunikationssoftware zu erstellen, wo der Prozess so sein sollte, wenn der Benutzer angemeldet ist, dann sollte die Vernetzung zwischen dem Client und Server starten.Methode funktioniert nicht innerhalb addactionlister

Das Problem, dass ich konfrontiert ist, dass, wann immer ich meine startRunning() (das ist die Methode, die die Vernetzung startet) Methode im addactionlistener der Schaltfläche die gesamte Software einfriert aber wenn ich die Methode außerhalb addactionlistener der Vernetzung beginnt gut. Das Problem ist in der btnNewButton.addActionListener. Wenn der Zählerstand 1 ist, sollte es die startrunning() -Methode aufrufen, die es nicht tut und die gesamte Software einfriert.

Jede Methode, die von der startRunning() - Methode bis nach unten läuft, funktioniert einwandfrei. Die Datenbank kann auch prüfen, ob der angegebene Benutzername und das Passwort korrekt sind oder nicht. Diese Software läuft gut, wenn sich die startRunning-Methode im Konstruktor befindet, aber nicht im btnNewButton.addActionListener.

 public class Server extends JFrame { 

private JTextField userText; 
private JTextArea chatWindow; 
private ObjectOutputStream output; 
private ObjectInputStream input; 
private ServerSocket server; 
private Socket connection; 

Connection dbconnection = null; 

private JTextField textField_1; 
private JPasswordField passwordField_1; 



public Server() { 

    super("Communication system"); 

    dbconnection = sqliteConnection.dbConnector(); 

    setupFrame(); 


} 

private void setupFrame() { 
    setBounds(100, 100, 450, 300); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    getContentPane().setLayout(null); 

    JLabel lblNewLabel = new JLabel("UserName"); 
    lblNewLabel.setBounds(10, 11, 81, 14); 
    getContentPane().add(lblNewLabel); 

    JLabel lblNewLabel_1 = new JLabel("Password"); 
    lblNewLabel_1.setBounds(10, 36, 59, 14); 
    getContentPane().add(lblNewLabel_1); 

    JButton btnNewButton = new JButton("Login"); 

    btnNewButton.addActionListener(new ActionListener() { 

     public void actionPerformed(ActionEvent arg0) { 

      try { 
       String query = "SELECT * FROM EmplyeeInfo WHERE Username=? and password=?"; 
       PreparedStatement pst = dbconnection 
         .prepareStatement(query); 

       pst.setString(1, textField_1.getText()); 
       pst.setString(2, passwordField_1.getText()); 

       ResultSet rs = pst.executeQuery(); 

       int count = 0; 
       while (rs.next()) { 
        count++; 
       } 
       if (count == 1) { 

        textField_1.setText(""); 
        passwordField_1.setText(""); 
        JOptionPane.showMessageDialog(null, 
          "Correct Username and Password"); 
        startRunning(); 

       } else { 
        textField_1.setText(""); 
        passwordField_1.setText(""); 
        JOptionPane.showMessageDialog(null, "Wrong try again"); 
       } 
       rs.close(); 
       pst.close(); 
      } catch (Exception e) { 
       // TODO: handle exception 
       JOptionPane.showMessageDialog(null, e); 
      } 
     } 
    }); 

    btnNewButton.setBounds(335, 7, 89, 23); 
    getContentPane().add(btnNewButton); 

    textField_1 = new JTextField(); 
    textField_1.setBounds(101, 8, 224, 20); 
    getContentPane().add(textField_1); 

    passwordField_1 = new JPasswordField(); 
    passwordField_1.setBounds(101, 33, 224, 20); 
    getContentPane().add(passwordField_1); 

    userText = new JTextField(); 
    userText.setBounds(10, 230, 330, 20); 
    userText.setEditable(false); 
    userText.addActionListener(new ActionListener() { 

     @Override 
     public void actionPerformed(ActionEvent event) { 
      // TODO Auto-generated method stub 
      sendMessage(event.getActionCommand()); 
      userText.setText(""); 
     } 
    }); 
    getContentPane().add(userText); 
    userText.setColumns(10); 

    chatWindow = new JTextArea(); 
    chatWindow.setBounds(10, 75, 330, 144); 
    getContentPane().add(chatWindow); 
    setVisible(true); 
} 

// set up and run the server 
public void startRunning() { 
    try { 
     server = new ServerSocket(6789, 100); 

     while (true) { 
      try { 
       waitForConnection(); 
       setupStreams(); 
       whileChatting(); 
      } catch (EOFException eofException) { 
       // TODO: handle exception 
       showMessage("\nServer Ended the conection!"); 
      } finally { 
       closeCrap(); 
      } 
     } 
    } catch (IOException ioException) { 
     ioException.printStackTrace(); 
    } 
} 

// wait for the connection, then display connection 

private void waitForConnection() throws IOException { 
    showMessage("Waiting for someone to connect... \n"); 
    connection = server.accept(); 
    showMessage(" Now connected to " 
      + connection.getInetAddress().getHostName()); 
} 

// get stream to send and receive data 
private void setupStreams() throws IOException { 
    output = new ObjectOutputStream(connection.getOutputStream()); 
    output.flush(); 
    input = new ObjectInputStream(connection.getInputStream()); 
    showMessage("\n Streams are now Setup \n"); 
} 

// during the chat conversation 
private void whileChatting() throws IOException { 
    String message = " You are now connected"; 
    sendMessage(message); 
    ableToType(true); 

    do { 
     // have a conversation 
     try { 
      message = (String) input.readObject(); 
      showMessage("\n" + message); 
     } catch (ClassNotFoundException classNotFoundException) { 
      showMessage("\n I don't know what the user send"); 
     } 

    } while (!message.equals("CLIENT - END")); 
} 

// closing the streams and sockets after done chatting 
private void closeCrap() { 
    showMessage("\n closing connections...\n"); 
    ableToType(false); 
    try { 
     output.close(); 
     input.close(); 
     connection.close(); 
    } catch (IOException ioException) { 
     ioException.printStackTrace(); 
    } 
} 

// send message to a client 
private void sendMessage(String message) { 
    try { 
     output.writeObject("SERVER - " + message); 
     output.flush(); 
     showMessage("\nSERVER -" + message); 
    } catch (IOException ioException) { 
     chatWindow.append("\n Error : can't send the message"); 
    } 
} 

// updates chat window 

private void showMessage(final String text) { 
    SwingUtilities.invokeLater(new Runnable() { 

     @Override 
     public void run() { 
      // TODO Auto-generated method stub 
      chatWindow.append("" + text); 
     } 
    }); 
} 

// allowing user to type stuff in the box 

private void ableToType(final boolean tof) { 
    SwingUtilities.invokeLater(new Runnable() { 

     @Override 
     public void run() { 
      // TODO Auto-generated method stub 
      userText.setEditable(tof); 
     } 
    }); 
} 

}

Antwort

1

Sie müssen es in einem anderen Thread setzen kann - man den Faden lokalisieren kann nur die startRunning Methode einschließen oder was Sie wollen:

public void actionPerformed(ActionEvent arg0) { 

    Thread th=new Thread() { 
     public void run() { 
     try { 
      String query = "SELECT * FROM EmplyeeInfo WHERE Username=? and password=?"; 
      PreparedStatement pst = dbconnection 
        .prepareStatement(query); 

      pst.setString(1, textField_1.getText()); 
      pst.setString(2, passwordField_1.getText()); 

      ResultSet rs = pst.executeQuery(); 

      int count = 0; 
      while (rs.next()) { 
       count++; 
      } 
      if (count == 1) { 

       textField_1.setText(""); 
       passwordField_1.setText(""); 
       JOptionPane.showMessageDialog(null, 
         "Correct Username and Password"); 
       startRunning(); 

      } else { 
       textField_1.setText(""); 
       passwordField_1.setText(""); 
       JOptionPane.showMessageDialog(null, "Wrong try again"); 
      } 
      rs.close(); 
      pst.close(); 
     } catch (Exception e) { 
      // TODO: handle exception 
      JOptionPane.showMessageDialog(null, e); 
     } 
    } 
    }; 
    th.start(); 
}); 
+0

Dank gelöst es mein Problem – Saud

+0

Kannst du mir bitte sagen, warum ich den neuen Thread brauchte und warum meine Software einfriert, als ich die startrunning() Methode ohne den Thread gestartet habe ??? – Saud

Verwandte Themen