2014-07-12 1 views
1

메신저를 만드는 방법을 따라했습니다. 코드를 올바르게 입력 했으므로 모든 것이 무엇인지에 대해 기본적인 이해를했습니다.자바 - invokeLater가 실행되지 않습니다

비록 동일한 결과로 끝나지는 않지만. 여기에 코드입니다 :


public class Server extends JFrame{ 
    private JTextField userText; 
    private JTextArea chatWindow; 
    private ObjectOutputStream output; 
    private ObjectInputStream input; 
    private ServerSocket server; 
    private Socket connection; 

    //constructor 
    public Server(){ 
     super("Coffee Messenger"); 
     userText = new JTextField(); 
     userText.setEditable(false); 
     userText.addActionListener(
      new ActionListener(){ 
       public void actionPerformed(ActionEvent event){ 
        sendMessage(event.getActionCommand()); 
        userText.setText(""); 
       } 
      } 
     ); 
     add(userText, BorderLayout.SOUTH); 
     chatWindow = new JTextArea(); 
     add(new JScrollPane()); 
     setSize(300,150); 
     setVisible(true); 
    } 
    //set up and run the server 
    public void startRunning(){ 
     try{ 
      server = new ServerSocket(6789, 100); 
      while(true){ 
       try{ 
        //connect and have conversation 
        waitForConnection(); 
        setupStreams(); 
        whileChatting(); 
       }catch(EOFException eofException){ 
        showMessage("\n Server ended the connection!"); 
       }finally{ 
        closeCrap(); 
       } 
      } 
     }catch(IOException ioException){ 
      ioException.printStackTrace(); 
     } 
    } 
    //wait for connection, then display connection information 
    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 conversation 
      try{ 
       message = (String) input.readObject(); 
       showMessage("\n " + message); 
      }catch(ClassNotFoundException classNotFoundException){ 
       showMessage("\n idk wtf that user sent"); 
      } 
     }while(!message.equals("CLIENT - END")); 
    } 
    //close streams and sockets (application) 
    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 client 
    private void sendMessage(String message){ 
     try{ 
      output.writeObject("SERVER - " + message); 
      output.flush(); 
      showMessage("\nSERVER - " + message); 

     }catch(IOException ioException){ 
      chatWindow.append("\n ERROR: Cannot send message."); 
     } 
    } 
    //updates chatWindow 
    private void showMessage(final String text){ 
     SwingUtilities.invokeLater(
      new Runnable(){ 
       public void run(){ 
        chatWindow.append(text); 
       } 
      } 
     ); 
    } 
    //sets the ability to edit the textfield 
    private void ableToType(final boolean tof){ 
     SwingUtilities.invokeLater(
      new Runnable(){ 
       public void run(){ 
        userText.setEditable(tof); 
       } 
      } 
     ); 
    } 
} 
__ 

내 주요 방법에서 응용 프로그램을 시작하면, 문자열합니다 (waitForConnection 방법에서) "누군가가 연결을 기다리는 중"는 표시되지 않습니다. 문제는 showMessage 메서드 내에 있다고 생각합니다. 나는 그것을 잘못 사용하고 있는가? invokeLater 메소드를 간단한 system.out.println()으로 대체하면 프로젝트가 계획대로 정확하게 실행됩니다.

미안하지만, 나는 약간 경험이 없어서 정말 간단 할 수도 있습니다. 대단히 감사드립니다.

여기

+0

주요 방법은 어디입니까? –

+1

그리고 chatWindow JTextArea를 JScrollPane에 추가하지 않는 이유는 무엇입니까? GUI가 아무 이유없이 빈 JScrollPane을 표시하는 것 같습니다. 만약 당신이''(새로운 JScrollPane (chatWindow)) '를 추가했다면 더 좋지 않을까요? 그것이 핵심 문제이기 때문에 나는 대답 할 것이다. –

+0

감사합니다! 그래, 그 문제가 해결 된 :) – Arcthor

답변

2

(크레딧이 자습서를 만들기위한 thenewboston합니다) :

chatWindow = new JTextArea(); 
add(new JScrollPane()); 

당신의 GUI는 JTextArea에, chatWindow를 생성하지만, 표시 아무것도에 추가하고, 대신 GUI가 표시된다 empty JScrollPane. 당신이 가지고 있었다면 훨씬 나아질 것입니다.

chatWindow = new JTextArea(); 
add(new JScrollPane(chatWindow)); 

이렇게하면 JTextArea로 보낸이 텍스트가 표시 될 수 있습니다.

+0

정말 고마워, 그 부분을 놓친 것 같아요. 정말 감사합니다! – Arcthor

관련 문제