在标签中显示颜色名称

时间:2016-03-27 19:13:11

标签: java swing colors label awt

有人可以向我解释如何在标签中显示哪种颜色用于某项任务。

不要分享整个代码,这是我基本上想做的事情。

  JColorChooser cc = new JColorChooser();
   Color c = cc.showDialog(rootPane, null, null);      
   L1.setText((String)(c.toString()));`

我有一个按钮,显示Color Chooser对话框,我选择一种颜色,我唯一想要的是将颜色名称打印到Label。 这怎么都行不通,因为我得到" java.awt.Color [r = ... g = ... b = ...]"

也许这是不可能的,我使用默认颜色选择器,我应该创建自己的颜色选择器,并单独定义颜色名称?

1 个答案:

答案 0 :(得分:0)

我不认为有一些内置方式可以做到这一点,但我尝试做一些我希望它会帮助你的事情,你在这里:

public class ColorManager {
    public static void main(String [] args){
  SimpleColor yellow = new SimpleColor(22,32,45);
      ArrayList<SimpleColor> myColors = new ArrayList<SimpleColor>();
     for (SimpleColor color : myColors)
{


    if ((color.getR() == 12) && (color.getG() == 23) && (color.getB() == 23))
    {
        System.out.println("The colour is lets say yellow");
    }
}
}
}


public class SimpleColor {
    int r;
    int g;
    int b;

    public int getR() {
        return r;
    }

    public void setR(int r) {
        this.r = r;
    }

    public int getB() {
        return b;
    }

    public void setB(int b) {
        this.b = b;
    }

    public int getG() {
        return g;
    }

    public void setG(int g) {
        this.g = g;
    }

    public SimpleColor(int r, int g, int b){
        this.r=r;
        this.b=b;
        this.g=g;
    }
}