我按照教程創建了一個Messenger。我正確地鍵入了代碼,並且對所有事情都做了基本的瞭解。Java - 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方法。我用錯了嗎?如果我用一個簡單的system.out.println()替換invokeLater方法,那麼項目完全按照計劃運行。
對不起,我有點缺乏經驗,所以它可能是一件非常簡單的事情。非常感謝你提前。
(學分thenewboston製作這些教程)
你的主要方法在哪裏? –
爲什麼不將chatWindow JTextArea添加到JScrollPane?您的GUI似乎無緣無故地顯示空的JScrollPane。如果你有,添加(新的JScrollPane(chatWindow));'?我會做出答案,因爲這是一個關鍵問題。 –
謝謝!是的,這解決了問題:) – Arcthor