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
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");
}});
1. ändere den Modifier von kaestchen in final, das sieht dann so aus:
Code:
final Button[][] kaestchen = new Button[24][20];
dann schnippeslt du deine actionPerformed raus und setzt diese ein
Code:
public void actionPerformed(ActionEvent e) {
for (int x = 0; x <24; ++x) {
for (int y = 0; y <20; ++y)
{
double 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);
}
}
}
Was macht die? Ganz einfach Wie du siehst hab ich den zufallscode von dir schon benutzt, und laufe auch wie du durch das ButtonArray durch. und setze jedesmal ne neue Farbe. genau wie du. hatteste also alles schon, mussteste nur noch neu zusammenbauen :toll:
Was man jetzt noch machen könnte wäre das zufallsfarbe berechnen in ne eigene klasse basteln, das mach ich auch noch und posts dir in äh 5 minuten
Und nochn Tip. das frame.setVisible() sollteste erst machen nachdem alle Buttons aufgebaut sind. dann geht das fenster auf und alle buttons sind da. andernfalls wenn du das machst wie bei deiner lösung kann man den buttons noch beim aufbauen zuguggen (was ja auch nicht unstylisch iss in dem Fall )