【问题标题】:Swing GUI doesn't show when using thread使用线程时不显示 Swing GUI
【发布时间】:2013-09-19 17:20:15
【问题描述】:

我在编写一个 IRC 客户端是为了好玩,这是程序线程的基本概述:

  • 主要的 Swing 线程
  • 服务器输出线程
  • (未完成)从服务器输入的线程

问题是,当我使用 Thread.start() 启动线程时,它没有显示我的 swing gui。我仍然看到调试消息,但 JFrame 上没有组件。如果有帮助,这是我的线程代码(我正在使用 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){

    }

下面是我开始讨论的方式:

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();

【问题讨论】:

  • 尝试发布SSCCE 以获得更好的帮助。 Swing 的东西在事件调度线程中运行。如需更多帮助,请阅读Concurrency in Swing
  • @nachokk 的好建议,但作为进一步的一般建议,不要阻止 EDT,并确保在 EDT 上执行所有 GUI 更新。 SwingWorker 是实现这两个目标的好方法。

标签: java multithreading swing irc


【解决方案1】:

您不应从主线程以外的线程更新您的 Swing UI。要从其他线程更新您的 UI,请使用 SwingWorker 类。查看this 文章。

【讨论】:

    猜你喜欢
    • 2022-07-12
    • 1970-01-01
    • 2011-12-26
    • 1970-01-01
    • 2011-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多