简体   繁体   English

用户选择颜色后在jLabel中设置文本

[英]Setting text in jLabel after user selectes color

I am creating small application where user will select color by pressing button. 我正在创建一个小型应用程序,用户可以通过按按钮选择颜色。 I would like to when user selects the color from JColorChooser dialog and setup the color of application as background also to put name of the color in jLabel . JColorChooser用户从JColorChooser对话框中选择颜色并将应用程序的颜色设置为背景时,还将颜色的名称放入jLabel

So far I have create following code: 到目前为止,我已经创建了以下代码:

private void btnChooseColorActionPerformed(java.awt.event.ActionEvent evt) {                                               
    Color color = JColorChooser.showDialog(getContentPane(), "Choose color", Color.yellow);
    this.getContentPane().setBackground(color);
    lblColorSelected.setText("Color: " + /* here I would like to append code that will display name of the color what user have selected */);
} 

I have successfully created that user selects the color from dialog and color goes on application as background but only problem is that I don't know how to get the name of the colors selected by the user. 我已经成功创建了用户从对话框中选择颜色并将颜色作为背景进行应用的方法,但是唯一的问题是我不知道如何获取用户选择的颜色的名称。 Do you have any idea? 你有什么主意吗?

Assuming that there is a limited number of possible colors (for example, you only allow red, green, and blue), it might be easiest to simply use a switch statement or a brute-force if else statement to assign the names. 假设可能的颜色数量有限(例如,您仅允许使用红色,绿色和蓝色),则仅使用switch语句或蛮力的if else语句来分配名称可能是最简单的。

String colorName = new String(); 

//My syntax may be off here but you get the idea
if Color.color.equals(c)
{
    colorName = color;
}

If you allow any color to be selected, then MadProgrammer is right. 如果允许选择任何颜色,则MadProgrammer是正确的。 This will let you display the hex values as a string. 将使您将十六进制值显示为字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM