【问题标题】:Creating array of colors and then using that for Jbuttons?创建颜色数组,然后将其用于 Jbuttons?
【发布时间】:2016-03-14 13:21:47
【问题描述】:

我有以下代码,它是一个用于 25 个按钮的 JFrame,每个按钮需要从颜色数组中随机分配一种颜色。我刚刚输入了一个颜色数组,但出现了错误。

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] = new Color(Color.red);
    colors[1] = new Color(Color.blue);
    colors[2] = new Color(Color.yellow);
    colors[3] = new Color(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("Color Toggle");
    frame.setVisible(true);
  }

}

带有颜色数组的部分出现此错误:

Error: incompatible types: java.awt.Color cannot be converted to int

为什么会发生错误?我还需要一种使用颜色数组为每个按钮随机分配颜色数组中颜色的方法的帮助?有什么办法可以做到吗?

【问题讨论】:

  • Colors[0] = new Color(Color.red)) 省略新的 Color 构造函数(即 Colors[0] = Color.red)

标签: java arrays swing jframe


【解决方案1】:

带有单个参数的 Java Color 的构造函数是:

Color (int rgb)

但是,你在这里做了什么:

colors[0] = new Color(Color.red);

您提供的是Color object 而不是int,因此收到错误java.awt.Color cannot be converted to int


你可以这样做:

colors[0] = Color.RED;

注意:最好使用 Color.RED 而不是 Color.red。

【讨论】:

    【解决方案2】:

    使用Color.red 而不使用new Color()

    colors[0] = Color.red;
    

    如果您想从 rgb 值中生成颜色,请使用新的构造函数,例如

    colors[1] = new Color(100,100,100);
    

    颜色构造函数有一个重载的构造函数,它接受一个整数作为参数。

    public Color(int i) {
            // compiled code
    }
    

    这就是你得到错误的原因

    Error: incompatible types: java.awt.Color cannot be converted to int

    【讨论】:

      【解决方案3】:

      正如你之前问过的here,没有人关闭它:

      我给你一个很好的答案这两个问题,这里是: https://stackoverflow.com/a/36004026/982161

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-06-02
        • 2013-10-08
        • 2016-02-21
        • 2014-02-01
        • 2020-06-14
        • 2012-12-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多