0

私は Java の初心者で、GUI テストとしてこのばかげた小さなプログラムを書き始めたところです。12 個のボタンがあり、それらをすべて白に設定し、連続して 3 つのランダムなボタンを黒に設定し、すべてのボタンを再び白に設定し、1 秒待ってから繰り返します。問題は、それを繰り返すことができないように見えることです。ランダム ボタンを黒くするコード セクションに while または for ループを配置しようとするたびに、実行されません。エラーは発生せず、プロセス自体は実行されますが、ウィンドウは表示されません。これは、クラスのコードです (インポート ステートメントを除く)。

public class testingness extends JFrame {

JButton one, two, three, four, five, six, seven, eight, nine, ten, eleven,
        twelve;
JPanel panel;

testingness(String title) {

    super(title);
    this.init();
    this.setSize(800, 800);
    this.setLocationRelativeTo(null);
    this.setVisible(true);
}

void init() {
    panel = new JPanel();
    panel.setLayout(new GridLayout(3, 4));
    one = new JButton();
    one.setBackground(Color.white);
    two = new JButton();
    two.setBackground(Color.white);
    three = new JButton();
    three.setBackground(Color.white);
    four = new JButton();
    four.setBackground(Color.white);
    five = new JButton();
    five.setBackground(Color.white);
    six = new JButton();
    six.setBackground(Color.white);
    seven = new JButton();
    seven.setBackground(Color.white);
    eight = new JButton();
    eight.setBackground(Color.white);
    nine = new JButton();
    nine.setBackground(Color.white);
    ten = new JButton();
    ten.setBackground(Color.white);
    eleven = new JButton();
    eleven.setBackground(Color.white);
    twelve = new JButton();
    twelve.setBackground(Color.white);

    panel.add(one);
    panel.add(two);
    panel.add(three);
    panel.add(four);
    panel.add(five);
    panel.add(six);
    panel.add(seven);
    panel.add(eight);
    panel.add(nine);
    panel.add(ten);
    panel.add(eleven);
    panel.add(twelve);

    this.add(panel);
    while (true) {
        randomness();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

public void randomness() {

    for (int timesdone = 0; timesdone < 4; timesdone++) {
        panel.update(panel.getGraphics());
        Random r = new Random();
        int rand = r.nextInt(12);

        if (rand == 0) {
            one.setBackground(Color.black);
        } else if (rand == 1) {
            two.setBackground(Color.black);
        } else if (rand == 2) {
            three.setBackground(Color.black);
        } else if (rand == 3) {
            four.setBackground(Color.black);
        } else if (rand == 4) {
            five.setBackground(Color.black);
        } else if (rand == 5) {
            six.setBackground(Color.black);
        } else if (rand == 6) {
            seven.setBackground(Color.black);
        } else if (rand == 7) {
            eight.setBackground(Color.black);
        } else if (rand == 8) {
            nine.setBackground(Color.black);
        } else if (rand == 9) {
            ten.setBackground(Color.black);
        } else if (rand == 10) {
            eleven.setBackground(Color.black);
        } else if (rand == 11) {
            twelve.setBackground(Color.black);
        }
        one.setBackground(Color.white);
        two.setBackground(Color.white);
        three.setBackground(Color.white);
        four.setBackground(Color.white);
        five.setBackground(Color.white);
        six.setBackground(Color.white);
        seven.setBackground(Color.white);
        eight.setBackground(Color.white);
        nine.setBackground(Color.white);
        ten.setBackground(Color.white);
        eleven.setBackground(Color.white);
        twelve.setBackground(Color.white);

    }
  }

}

ここで何が間違っていますか?

4

4 に答える 4

4

Swing はシングルスレッド環境です。UI に対するすべての対話と変更は、イベント ディスパッチ スレッドのコンテキスト内で実行する必要があります。

EDT は、とりわけ、再描画要求と着信イベントの処理を担当します。

EDT をブロックするアクションまたは操作は、これらのイベントの処理と画面の更新を妨げます。

あなたのコードでは、これを行っています...

while (true) {
    randomness();
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

これにより、EDT がブロックされ、画面の更新が妨げられている可能性があります。

同様に、あなたもこれをやっています...

for (int timesdone = 0; timesdone < 4; timesdone++) {
   /*...*/
}

同様に、EDT をブロックし、画面が更新されないようにします...

また、これpanel.update(panel.getGraphics());は非常に悪い考えです。Swing では、ペイント プロセスを制御することはできませんRepaintManager。更新/再描画をリクエストすることはできますが、RepaintManager何をいつ行うかは によって決定されます。これらのリクエストは EDT に投稿されます...ブロックされた場合、何もしません。

getGraphicsを返すこともできますがnull、これは決してきれいではありません...

問題を解決するには、「待機」時間をある種のバックグラウンド プロセスにオフロードする必要があります。準備ができたら、それに応じて状態を変更するように UI に指示する必要があります...

を使用することもできますがThread、これは、すべての呼び出しを EDT に再同期する必要があることを意味します。これは面倒で、オーバーヘッドのメリットはほとんどありません。

を使用できSwingWorkerますが、手元にあるタスクにはあまり適していません

より良い解決策は、EDT のコンテキスト内で実行されるように、指定された遅延の後にイベントを繰り返しjavax.swing.Timerトリガーするを使用することです。actionPerformed

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Flashy01 {

    public static void main(String[] args) {
        new Flashy01();
    }

    public Flashy01() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new FlashPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class FlashPane extends JPanel {

        private final BigButton one, two, three, four, five, six, seven, eight, nine, ten, eleven,
                twelve;
        private final JPanel panel;
        private final Random random;
        private int timesDone;

        public FlashPane() {
            panel = new JPanel();
            panel.setLayout(new GridLayout(3, 4));
            one = new BigButton();
            one.setBackground(Color.white);
            two = new BigButton();
            two.setBackground(Color.white);
            three = new BigButton();
            three.setBackground(Color.white);
            four = new BigButton();
            four.setBackground(Color.white);
            five = new BigButton();
            five.setBackground(Color.white);
            six = new BigButton();
            six.setBackground(Color.white);
            seven = new BigButton();
            seven.setBackground(Color.white);
            eight = new BigButton();
            eight.setBackground(Color.white);
            nine = new BigButton();
            nine.setBackground(Color.white);
            ten = new BigButton();
            ten.setBackground(Color.white);
            eleven = new BigButton();
            eleven.setBackground(Color.white);
            twelve = new BigButton();
            twelve.setBackground(Color.white);

            panel.add(one);
            panel.add(two);
            panel.add(three);
            panel.add(four);
            panel.add(five);
            panel.add(six);
            panel.add(seven);
            panel.add(eight);
            panel.add(nine);
            panel.add(ten);
            panel.add(eleven);
            panel.add(twelve);

            this.add(panel);
            random = new Random();

            Timer timer = new Timer(1000, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    randomness();
                }
            });
            timer.start();
        }

        public void randomness() {

            int rand = random.nextInt(12);
            timesDone++;

            if (timesDone % 5 != 0) {

                if (rand == 0) {
                    one.setBackground(Color.black);
                } else if (rand == 1) {
                    two.setBackground(Color.black);
                } else if (rand == 2) {
                    three.setBackground(Color.black);
                } else if (rand == 3) {
                    four.setBackground(Color.black);
                } else if (rand == 4) {
                    five.setBackground(Color.black);
                } else if (rand == 5) {
                    six.setBackground(Color.black);
                } else if (rand == 6) {
                    seven.setBackground(Color.black);
                } else if (rand == 7) {
                    eight.setBackground(Color.black);
                } else if (rand == 8) {
                    nine.setBackground(Color.black);
                } else if (rand == 9) {
                    ten.setBackground(Color.black);
                } else if (rand == 10) {
                    eleven.setBackground(Color.black);
                } else if (rand == 11) {
                    twelve.setBackground(Color.black);
                }

            } else {

                one.setBackground(Color.white);
                two.setBackground(Color.white);
                three.setBackground(Color.white);
                four.setBackground(Color.white);
                five.setBackground(Color.white);
                six.setBackground(Color.white);
                seven.setBackground(Color.white);
                eight.setBackground(Color.white);
                nine.setBackground(Color.white);
                ten.setBackground(Color.white);
                eleven.setBackground(Color.white);
                twelve.setBackground(Color.white);
            }
        }
    }

    public class BigButton extends JButton {

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(20, 20);
        }

    }
}

を見てみましょう...

詳細については...

于 2013-08-19T01:10:25.337 に答える
2

randomness() 関数の最後で、すべてのボタンを白に設定しています。それを後ではなく、すべてのif-elseの上に移動する必要があります。

于 2013-08-18T23:37:08.410 に答える
0

主な問題は、コードが実行されることですが、ループが終了しないため、コンポーネントをペイントすることができません。ループ内のメソッドの最後で repaint() メソッドを呼び出してみてください。

別のポイント: 複数のボタンがある場合は、(プログラムによって) ボタンのみをコンテナーに追加し、このコンテナーのコンテンツをループする方がよいでしょう。

于 2013-08-18T23:39:13.820 に答える