简体   繁体   English

使用线程时不显示Swing GUI

[英]Swing GUI doesn't show when using thread

I'm programming an IRC client for fun, and this is the basic outline of the program threads: 我正在为一个有趣的IRC客户端编程,这是程序线程的基本概要:

  • Main Swing thread 主摆动螺纹
  • Thread for output from server 用于从服务器输出的线程
  • (Not done) Thread for input from server (未完成)用于从服务器输入的线程

The problem is, when I start the thread using Thread.start(), it doesn't show my swing gui. 问题是,当我使用Thread.start()启动线程时,它不显示我的swing gui。 I still see the debug messages, but no components on the JFrame. 我仍然看到调试消息,但JFrame上没有组件。 If it helps, here's my code for the thread (I'm using the O'Reilly java hack code: 如果它有帮助,这是我的线程代码(我正在使用O'Reilly java hack代码:

try{
        // Connect directly to the IRC server.
        Socket socket = new Socket(server, 6667);
        final BufferedWriter writer = new BufferedWriter(
                new OutputStreamWriter(socket.getOutputStream( )));
        BufferedReader reader = new BufferedReader(
                new InputStreamReader(socket.getInputStream( )));

        //pause for a second so that the gui can do its thing
        this.sleep(500);

        // Log on to the server.
        writer.write("NICK " + nick + "\r\n");
        writer.write("USER " + login + " 8 * : IRC Custom Client-" + "\r\n");
        writer.flush( );

        System.out.println("hi this is to verify bla.");

        appendTo.append("Sent login request.");

        // Read lines from the server until it tells us we have connected.
        String line = null;
        while ((line = reader.readLine( )) != null) {
            appendTo.append(line);
            if (line.indexOf("004") >= 0) {
                appendTo.append("You are now logged in!");
            }
            else if (line.indexOf("433") >= 0) {
                appendTo.append("Nickname is already in use.");
                return;
            }
            else if (line.contains("PING")) {
                // We must respond to PINGs to avoid being disconnected.
                writer.write("PONG " + line.substring(5) + "\r\n");
                writer.write("PRIVMSG " + channel + " : Pinged!!\r\n");
                writer.flush( );
            }
            //after we read the line, sleep.
            sleep(10);
        }

        // Join the channel. 
        writer.write("JOIN " + channel + "\r\n");
        writer.flush( );


        // Keep reading lines from the server.
        while ((line = reader.readLine( )) != null) {
            appendTo.append(line);
            if (line.contains("PING")) {
                // We must respond to PINGs to avoid being disconnected.
                writer.write("PONG " + line.substring(5) + "\r\n");
                writer.write("PRIVMSG " + channel + " :I got pinged!\r\n");
                writer.flush( );
            }
            else {
                // Print the raw line received by the bot.
                appendTo.append(line);
            }
            sleep(10);
        }

        socket.close();
        writer.close();
        reader.close();
    }
    catch (Exception e){

    }

And here's how I start the thread: 以下是我启动线程的方法:

new Thread(){

        public void run(){
            try {
                connectToIRC(nickname, login, server, channel);
            } catch (UnknownHostException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }.start();

You should not update your swing UI from threads other then main thread. 您不应该从主线程以外的线程更新swing UI。 To update your UI from other threads use SwingWorker class. 要从其他线程更新UI,请使用SwingWorker类。 Check this article. 看看这篇文章。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM