首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >invokeLater将不会运行

invokeLater将不会运行
EN

Stack Overflow用户
提问于 2014-07-12 22:23:35
回答 1查看 66关注 0票数 1

我遵循了一个教程来创建一个信使。我正确地输入了代码,并且我对每件事都有相当基本的理解。

虽然,我并没有得到同样的结果。下面是代码:

代码语言:javascript
复制
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();,则该项目完全按计划运行。

对不起,我有点缺乏经验,所以可能是很简单的事情。先谢谢你。

(这些教程的制作归功于当时的韦伯斯顿)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-07-12 22:28:40

在此:

代码语言:javascript
复制
chatWindow = new JTextArea();
add(new JScrollPane());

您的图形用户界面正在创建一个JTextArea,chatWindow,但是将它添加到不显示它的任何内容中,而是将您的GUI显示为一个空的JScrollPane。如果你有更好的,

代码语言:javascript
复制
chatWindow = new JTextArea();
add(new JScrollPane(chatWindow));

这样,发送给JTextArea的文本就有可能被显示出来。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24717853

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档