0

クライアント プログラムから取得した文字列を追加しようとしています。ただし、append ステートメントが while ループになるようにしました。ただし、メッセージ REALTIME が JTextArea に送信されないため、これはプログラムに影響します。

while ループや for ループを使用せず、クライアントからのメッセージを引き続き受け取り、リアルタイムでテキストエリアに出力するようにプログラムを作成するにはどうすればよいですか? おそらく単なるif文でしょうか?

whileループエリア

while ((inputLine = in.readLine()) != null) 

のような地域の場合

   tfFIXMsg.append( inputLine + "\n\n\n");

       tfCSVLine.append(outputLine+"\n\n\n");

while ループが完了するまで、textArea にメッセージは表示されません。クライアントからメッセージを受信し続け、リアルタイムで textArea に出力できるようにするにはどうすればよいですか

try {
    while ((inputLine = in.readLine()) != null) 
          { 
           System.out.println ("Server: " + inputLine); 


           tfFIXMsg.append( inputLine + "\n\n\n");


           int pos = inputLine.indexOf(inputLine, 0);
           h.addHighlight(50, 60, DefaultHighlighter.DefaultPainter);



           if (inputLine.trim().equals("Bye.")) {
               System.out.println("Exit program"); 
               break;
               } 

           Scanner input1 = new Scanner(new File(csvName));
           Scanner input2 = new Scanner(new File(csvName));
           Scanner input3 = new Scanner(new File(csvName));
           Scanner input4 = new Scanner(new File(csvName));


           String csvline = getCsvLineVal (getLocation34CSV(getTag34Value(Tag34Location(getTagCSV( parseFixMsg(inputLine ,inputLine))), getValueCSV( parseFixMsg(inputLine ,inputLine))), getVal34(input1,  input2)), getCSVLine( input3,  input4) );
           outputLine = compareClientFixCSV( getTagCSV( parseFixMsg(inputLine ,inputLine)), getValueCSV(parseFixMsg(inputLine ,inputLine)), getCSVTag(csvline), getCSVValue(csvline));

           out.println(outputLine);
           tfCSVLine.append(outputLine+"\n\n\n");

           input1.close();
           input2.close();
           input3.close();
           input4.close();



          }
} catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
4

2 に答える 2

2

あなたのコードは UI スレッドで処理されていると思われます。UI スレッドは、画面を更新してレンダリングするものです。UI スレッドで while ループが実行されているため、UI を更新できません。while ループの実行で忙しい。

答えは、別のスレッドを開始して処理を行うことです。次に、SwingWorkerを使用して UI スレッドに戻り、テキスト ボックスを更新します。UI スレッドから UI オブジェクトを更新してはならないため、これが必要です。

開始するためのチュートリアルを次に示します。これをより詳しく説明します。

于 2013-08-07T18:21:45.497 に答える