如何从颜色数组中随机设置jbutton颜色?

时间:2016-03-15 02:56:09

标签: java arrays random jframe

我有以下jframe的代码,即25个按钮,我正在尝试使用我的颜色数组,使每个按钮成为随机颜色之一。

import java.awt.event.*; // Needed for ActionListener and ActionEvent
import javax.swing.*; // Needed for JFrame and JButton
import java.awt.Color;
import java.awt.Graphics;

public class ColorToggleGui extends JFrame implements ActionListener {

  // This stores all buttons
  JButton[][] buttons;
  //Stores colors
  Color[] colors;

  public ColorToggleGui(String title) {
    super(title);
    setLayout(null);

    //Allocate the size of the array
    colors = new Color[4];

        //Initialize the values of the array
    colors[0] = Color.red;
    colors[1] = Color.blue;
    colors[2] = Color.yellow;
    colors[3] = Color.green;


    buttons = new JButton[5][5];
    String[] buttonLabels = { "", "", "", "", "", "", "", "", "", "", "","","","","","","","","","","","","","","" };
    for(int row=0; row<5; row++) {
      for (int col=0; col<5; col++) {
        buttons[row][col] = new JButton(buttonLabels[row*3+col]);
        buttons[row][col].setLocation(10+col*55, 10+row*55);
        buttons[row][col].setSize(50,50);
        buttons[row][col].addActionListener(this);
        add(buttons[row][col]);
      }
    }
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setSize(300,450);
  }


  // This is the single event handler for all the buttons
  public void actionPerformed(ActionEvent e) {
    System.out.println("Button " + e.getActionCommand() + " was pressed." );
  }

  public static void main(String args[]) {
    ColorToggleGui frame = new ColorToggleGui("Julian's Colour Toggle");
    frame.setVisible(true);
  }

}

如何从我制作的颜色数组中随机制作每个按钮的颜色?

1 个答案:

答案 0 :(得分:0)

你必须生成0到3之间的随机数,这样你就可以在红色,蓝色,黄色和绿色之间进行映射

您的构造函数可能如下所示:

public ColorToggleGui(String title) {
        super(title);
        setLayout(null);

        // Allocate the size of the array
        colors = new Color[4];

        // Initialize the values of the array
        colors[0] = Color.red;
        colors[1] = Color.blue;
        colors[2] = Color.yellow;
        colors[3] = Color.green;


        buttons = new JButton[5][5];
        final String[] buttonLabels = { "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
                "" };

        // Here random color logic

        final Random r = new Random();
        int rInt = r.nextInt(4);
        for (int row = 0; row < 5; row++) {
            for (int col = 0; col < 5; col++) {
                rInt = r.nextInt(4); // here generate the random integer
                System.out.println(rInt);
                buttons[row][col] = new JButton(buttonLabels[row * 3 + col]);
                buttons[row][col].setLocation(10 + col * 55, 10 + row * 55);
                buttons[row][col].setSize(50, 50);
                buttons[row][col].addActionListener(this);
                buttons[row][col].setBackground(colors[rInt]); // here set the background color
                add(buttons[row][col]);
            }
        }
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(300, 450);
    }

其余的都一样......

输出可以是:

enter image description here