2

私は、swingworker で印刷を行うコードを実行していました。プリントアウトが得られなかったので使用SwingUtilities.invokeLaterしましたが、今では機能しています。こんな結果になるとは思っていませんでしたが、どうしてこうなったのですか?System.out.println私はEDTの外で実行できると思っていたでしょう.

4

4 に答える 4

2

それはテストするのがかなり簡単だったでしょう(言うまでもなく、これをテストするためにすべてのコードを入力することでさえ、ここに投稿するよりも手間がかかりません):

import java.awt.EventQueue;
public class HelloWorld {
  public static void main( String[] args ) {
    System.out.println("Hello world");
    System.out.println( EventQueue.isDispatchThread());
  }
}

結果は

Hello world
false

コンソール上。

そうですSystem.out.println、EDTの外で使用できます

于 2012-06-28T21:03:30.833 に答える
1

私はthoughtsystem.out.printlnがedtの外で実行できると思います。

それは本当です。これをテストするには、ループとプリントアウトを配置して自分の目で確かめるスレッドを作成します:)

于 2012-06-28T20:30:18.877 に答える
0

System.out.println は edt の外で実行されます。スウィングワーカーを使って走らせたら、中に走らせました。理論的には、常にコード ブロックから直接結果を出力できるはずです。私は提案します:

Runnable runnable = new Runnable() {

        public void run() {

        }
    };
SwingUtilities.invokeLater(runnable);
于 2012-06-28T20:35:41.560 に答える
0

可能ですが、デバッグ以外のアプリケーションはほとんどありません。

代替出力の例は、JOptionPane です。

JOptionPane.showMessageDialog(frame/* sets up the message, can also be replaced with null to remove formatting*/,
"Eggs are not supposed to be green."/* this is your main message*/,
"A plain message"/* this is what shows in the title spot (first parameter must not be null)*/,
JOptionPane.PLAIN_MESSAGE/*shows no icon, also replacable with WARNING_MESSAGE, ERROR_MESSAGE, INFORMATION_MESSAGE*/);

すべてが1行になりますが、ここで1行のバージョンをフォーマットするために分割しました:

JOptionPane.showMessageDialog(frame/* sets up the message, can also be replaced with null to remove formatting*/, "Eggs are not supposed to be green."/* this is your main message*/, "A plain message"/* this is what shows in the title spot (first parameter must not be null)*/, JOptionPane.PLAIN_MESSAGE/*shows no icon, also replacable with WARNING_MESSAGE, ERROR_MESSAGE, INFORMATION_MESSAGE*/);

コメントを省く:

JOptionPane.showMessageDialog(frame, "Eggs are not supposed to be green.", "A plain message", JOptionPane.PLAIN_MESSAGE);

プログラムで Dr. Seuss の名言を使って教師を荒らすのは楽しいことです。

于 2012-06-28T21:56:28.933 に答える