如何随机设定的JButton从颜色组合的颜色? [英] how to randomly set jbutton color from array of colors?

查看:168
本文介绍了如何随机设定的JButton从颜色组合的颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对的JFrame以下code是25按钮,我试图用我的色彩阵列,使每个按钮随机颜色之一。

i have the following code for jframe that is 25 buttons and i am trying to use my array of colors to make each button one of the colors randomly.

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);
  }

}

我会怎么做随机每个按钮从我做颜色的数组颜色?

how would I randomly make each button a color from the array of colors that I made?

推荐答案

您有0和3之间产生随机数,所以你可以映射那些颜色有红色,蓝色,黄色和绿色之间的

You have to generate random numbers between 0 and 3 so you can map those between the colors red, blue, yellow and green

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);
    }

剩下的是一样的...

The rest is the same...

在这里输入的形象描述

这篇关于如何随机设定的JButton从颜色组合的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆