0

私はサイモンのようなゲームを作ろうとしています: http://www.freegames.ws/games/kidsgames/simon/simon.htm#

ボタンが2つだけの小さめのスケールを作っています。ボタン 1 とボタン 2 の 2 つのボタンの色を切り替えます。これが起こっている間にボタンをクリックする必要があるため、これはスレッドにあります。プログラムを開くと、ボタンの色はそのままです。

事前に助けてくれてありがとう!

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;

import javax.swing.*;

public class TestFrame extends JFrame{

    public JButton button1; 
    public JButton button2;

    boolean isTrue = true;
    boolean switchColor = true;

    TestFrame(){
        super("Simon");
        initialize();

        this.setSize(200, 400);
        this.setVisible(true);
    }

    private void initialize() {
         this.setLayout(new BorderLayout()); 

        button1 = new JButton();
        button1.setBackground(Color.green);
        button1.setSize(200,200);

        button2 = new JButton();
        button2.setSize(200, 200);
        button2.setBackground(Color.blue);

        this.add(button1, BorderLayout.NORTH);
        this.add(button2, BorderLayout.SOUTH);
        Thread t = new Thread(r1);
        t.start();

    }
    Runnable r1 = new Runnable() {
        public void run() {
            while(isTrue){
            if(switchColor = true){
                button1.setBackground(Color.blue);
                button2.setBackground(Color.green);
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                refresh();
                switchColor = false;
            } else {
                button1.setBackground(Color.green);
                button2.setBackground(Color.blue);
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                refresh();

                switchColor = true;
                }
            }

        }
    };

    public void refresh(){
        this.invalidate();
        this.validate();
        this.repaint();
    }

}
4

2 に答える 2

2

いくつかの問題が際立っています (shazin がその 1 つに対処しました)。もう 1 つは、Swing のシングル スレッド要件に違反していることです。UI に対するすべての変更は、イベント ディスパッチ スレッドのコンテキスト内から行う必要があります。

を使用する代わりに、 を使用するThread必要がありますjavax.swing.Timer。これにより、更新を EDT に再同期する必要がなくなります。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
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 FlashyButtons {

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

    public FlashyButtons() {
        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 TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private JButton btn1;
        private JButton btn2;
        private int count = 0;

        public TestPane() {
            setLayout(new GridBagLayout());
            btn1 = new FlashButton();
            btn2 = new FlashButton();
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            add(btn1, gbc);
            add(btn2, gbc);

            btn1.setBackground(Color.GREEN);
            btn2.setBackground(Color.BLUE);

            Timer timer = new Timer(500, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    count++;
                    if (count % 2 == 0) {
                        btn1.setBackground(Color.BLUE);
                        btn2.setBackground(Color.GREEN);
                    } else {
                        btn1.setBackground(Color.GREEN);
                        btn2.setBackground(Color.BLUE);
                    }
                }
            });
            timer.start();
        }

    }

    public class FlashButton extends JButton {

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

    }

}

詳細については、Swing での同時実行をご覧ください。

于 2013-08-07T01:36:30.857 に答える