0

I tried to create a drafts board with Java. For this reason I created 100 JButtons in an Array. While creating these Buttons I wanted to change the Backcolor of every Second Button.

But the Color of the Button doesn't change.

Here's the screenshot: Screenshot of the Board.

I want to Change the Color of the Buttons not the Color behind the Buttons. It would be awesome if you could help me out. Thanks in advice.

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;

public class Brett extends javax.swing.JFrame{

public Brett ()
{
    initComponents();

    getContentPane().removeAll();   

    boolean schwarz = true;

    for (int z = 0; z< feld.length;z++){

        for (int sp = 0; sp< feld[z].length; sp++){

            Feld f = new Feld (this, schwarz );
            feld [z][sp] = f;
            f.addActionListener(fl);

            if (schwarz){
            f.setBackground(new Color(100,216,199));
            f.setOpaque(true);
            //f.setBorderPainted(false);
            }
            else {
            f.setBackground(Color.lightGray);
            f.setOpaque(true);
            //f.setBorderPainted(false);
            }


            if (schwarz){

                if (z<=3){

                f.setStein(new Einfach (f,false));
                f.setForeground(Color.white);
                f.setText("O");

                }else if (z>=6){

                f.setStein(new Einfach (f, true));
                f.setForeground(Color.black);
                f.setText("O");

                }
            }

            jPanel1.add(feld [z][sp]);
            schwarz =! schwarz;
        } 
        schwarz =! schwarz;
    }

    getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);
    pack();
}


//  @SupressWarnings("unchecked")


private void initComponents(){



    jPanel1 = new javax.swing.JPanel();
    jButton1 = new javax.swing.JButton();


          setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    jPanel1.setLayout(new java.awt.GridLayout(10,10));

    //jButton1.setText("jButton1");
    //jPanel1.add(jButton1);

    getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

    pack();
}



public static void main(String [] args){


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

        public void run(){
            new Brett().setVisible(true);


        }
    });

}



private Feld[][] feld = new Feld[10][10];


private class FeldListener implements java.awt.event.ActionListener{

    public void actionPerformed(java.awt.event.ActionEvent evt){
    System.out.println("Feld geklickt!");
    }
}

FeldListener fl = new FeldListener();

private javax.swing.JButton jButton1;
private javax.swing.JPanel jPanel1;

}
Top Sekret
  • 748
  • 5
  • 21

0 Answers0