L
leider Anfänger
Gast
Hallo, vielleicht kann mir jemand helfen, wäre super!
Ich erzeuge mehrere Buttons mit verschiedenen Farben, nun möchte ich wenn ich auf Start klicke, das er die Buttons mit neuen Farben belegt. Kann mir jemand sagen, was ich in meinen "public void actionPerformed" eintragen muss. Danke
Ich erzeuge mehrere Buttons mit verschiedenen Farben, nun möchte ich wenn ich auf Start klicke, das er die Buttons mit neuen Farben belegt. Kann mir jemand sagen, was ich in meinen "public void actionPerformed" eintragen muss. Danke
Code:
public static void main(String[] args) {
double zufall;
Button[][] kaestchen = new Button[24][20];
int[] xpos = new int[24];
int[] ypos = new int[20];
for (int i = 0; i <24; ++i){
xpos[i] = 30*i+10;
}
for (int i = 0; i <20; ++i){
ypos[i] = 30*i+40;
}
Frame frame = new Frame("Hallo");
frame.setLayout(null);
frame.setSize(900,650);
Button start = new Button("Start");
start.setBounds(750,500,100,30);
frame.add(start);
for (int x = 0; x <24; ++x) {
for (int y = 0; y <20; ++y)
{
kaestchen[x][y] = new Button();
kaestchen[x][y].setBounds(xpos[x],ypos[y],30,30);
frame.add(kaestchen[x][y]);
zufall = Math.random();
if (zufall < 0.2 & zufall >= 0.0) kaestchen[x][y].setBackground(Color.BLUE);
if (zufall >= 0.2 & zufall < 0.4) kaestchen[x][y].setBackground(Color.MAGENTA);
if (zufall >= 0.4 & zufall < 0.6) kaestchen[x][y].setBackground(Color.RED);
if (zufall >= 0.6 & zufall < 0.8) kaestchen[x][y].setBackground(Color.YELLOW);
if (zufall >= 0.8 & zufall <= 1.0) kaestchen[x][y].setBackground(Color.GREEN);
}
frame.setVisible(true);
frame.setBackground(Color.GRAY);
frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
start.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
System.out.println("button gedrückt");
}});