0

私は楽しみのために IRC クライアントをプログラミングしています。これはプログラム スレッドの基本的な概要です。

  • メインスイングスレッド
  • サーバーからの出力用スレッド
  • (未処理) サーバーからの入力用スレッド

問題は、Thread.start() を使用してスレッドを開始すると、Swing GUI が表示されないことです。デバッグ メッセージは引き続き表示されますが、JFrame にコンポーネントがありません。それが役立つ場合は、スレッドのコードを次に示します (私は O'Reilly Java ハック コードを使用しています:

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

1 に答える 1

2

メインスレッド以外のスレッドから Swing UI を更新しないでください。UI を他のスレッドから更新するには、SwingWorker クラスを使用します。この記事をチェックしてください。

于 2013-09-19T17:30:37.840 に答える