【发布时间】:2012-01-22 14:16:31
【问题描述】:
我有一个问题 - 在我选择了一种颜色(使用 JColorChooser)后,我有一个十六进制值存储在一个文本字段中。我想做的是在另一个文本字段中显示颜色的 name,就在具有十六进制值的文本字段旁边,但我不确定如何获取颜色名称?我正在包含我的代码,也许有人可以给我一些有用的提示:
public class Main extends JComponent implements Accessible {
public ColorSelectionModel selectionModel;
public static final String SELECTION_MODEL_PROPERTY = "selectionModel";
public JColorChooser chooser;
public Color color;
public void process() {
JFrame frame;
JButton button;
final JTextField text1, text2;
// Initialize variables
chooser = new JColorChooser();
frame = new JFrame();
JPanel panel = new JPanel();
button = new JButton("Show color Palette");
text1 = new JTextField(20);
text2 = new JTextField(20);
// Setup UI
frame.add(panel);
panel.add(button);
panel.add(text1);
panel.add(text2);
panel.add(chooser)
chooser.setVisible(false);
button.setLocation(800, 600);
button.setActionCommand("");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
color = chooser.showDialog(chooser, "SHOW THE COLOR",
chooser.getColor());
{
if (color != null) {
String hex = Integer.toHexString(color.getRGB() & 0xffffff);
hex = "#" + hex;
text1.setText(hex);
}
}
}
});
frame.setVisible(true);
frame.setSize(1000, 800);
}
public static void main(String[] argv) {
// Run the code
Main m1 = new Main();
m1.process();
}
}
【问题讨论】:
-
"..don't know how to get the name of that selected color" 您是否希望 16,777,216 种可能的颜色中的每一种都有不同的名称,例如“红色”?
-
@AndrewThompson - 我的妻子知道其中的 16,777,200 种颜色。特别是在提到鞋子时。现在,如果我们能弄清楚如何在 Java Swing 中构建一个妻子模块......
-
@Perception 好吧,只有 digital 鞋子仅限于 256 种 R、G 和 B 色调。在现实世界中(即鞋店),全部 赌注取消。 ;)
-
如果我没记错的话,来自 OTN 的 Swings Gurus 曾经使用过这个r0k.us/graphics/SIHwheel.html,
-
@Perception 如果这样的模型是可能的,由于它的不确定性,它无论如何都是无用的:)
标签: java swing jcolorchooser