开发者

Coloring a JButton in Java

开发者 https://www.devze.com 2023-04-10 17:21 出处:网络
I am struggling with a problem on coloring an array of JButtons. I made two arrays of JButtons: public JButton Speler1[] = new JButton[140]; //Player1

I am struggling with a problem on coloring an array of JButtons.

I made two arrays of JButtons:

public JButton Speler1[] = new JButton[140]; //Player1
public JButton Speler2[] = new JButton[140]; //Player2

These two arrays of buttons make lane 1 and lane 2 of a racing game. I want the position of player 1 and 2 to be colored on both开发者_JAVA技巧 screens. So player 1 can see where player 2 is and vice versa.

I already made a method which sends the position of both players to eachother.

if (message.contains("Positie")) {
   String posit = message.replaceFirst("Positie", "");
   int positi = Integer.valueOf(posit);
   positie2 = positi;
   kleurHokje kleur = new kleurHokje();
   kleur.hokVerkleur(positi); // positi is the position of each player
}

So when I call the method hokVerkleur(positi), I want to change a button on lane 2.

class kleurHokje{
    public void hokVerkleur(int loc){
        Speler2[loc].setBackground(Color.yellow);
        Speler2[positie2].setBackground(Color.gray);                
    }
}

It just wont work. While i'm doing almost the same for Speler1[positie] except Speler1 doesn't use the network, which works as I want.

Any help is appreciated,

Thanks Jef

Edit: If I place my code in one of the MouseListeners it works fine, but rather have it to be colored automatically instead of having to click each time.

 class Klaar extends MouseAdapter {

    public void mouseClicked(MouseEvent e) {            
        Speler2[positie2].setBackground(Color.gray);
    }
}

Ps. my first language isn't english, I hope you understand my problem.


If I place my code in one of the MouseListeners it works fine:

agreed, if you change Color for JButton from BackGroung Task, then there any changes, you have some issues with Concurency in Swing, your updated to the GUI is out of EDT,

1) then you have to wrap coloring JButtons into invokeLater();

    java.awt.EventQueue.invokeLater(new Runnable() {

        @Override
        public void run() {
            Speler2[loc].setBackground(Color.yellow);
            Speler2[positie2].setBackground(Color.gray);
        }
    });

2) but you have to solve by using regular Swing methods

2a) wrap your GUI rellated code to the javax.swing.Action

2b) initialize your BackGroung Tasks from

  • SwingWorker
  • Runnble#Tread


Made a runnable thread, works as a charm. Thanks for help everybody.

public void actionThread() {
    Thread t = new Thread() {

        public void run() {
            while (!stop) {
                tegenspelerPositie();
                Score();
                eigenOgen();
                try {
                    sleep(100);
                } catch (InterruptedException ex) {
                }
            }
        }
    };
    t.start();
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号