获得JColorChooser的颜色名称 [英] get a color name of JColorChooser
问题描述
我正在使用JFrame.在设计视图中,我放置了一个JBUtton,两个JTextField和一个摆动窗口JColorChooser.而不是我在两个JTextField中为此十六进制值和颜色名称编写代码的过程.如果我单击JButton,它将打开JColorChooser调色板,此后,我单击JColorChooser选择一种颜色,它会在其中显示相应的颜色十六进制值一个JtextField,颜色Name在另一个JTextField中.但是我可以得到十六进制值,但是我不知道如何获得颜色名称.
i am working with JFrame. In design View i put One JBUtton , two JTextField and swing windows JColorChooser. than i write a code for this getting hex value and color name in a two JTextField.the process is if i click on JButton, it will open JColorChooser palette, After that i click on JColorChooser for select a color it show corresponding color hex value in one JtextField and color Name in another JTextField. but i can get hex value but i don't know how i can get color name.
import javax.swing.*;
import java.awt.*;
import javax.swing.event.*;
import java.util.Set;
import javax.accessibility.*;
import javax.swing.colorchooser.ColorSelectionModel;7.
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;
chooser = new JColorChooser();
frame= new JFrame();
JPanel panel = new JPanel();
button = new JButton("Show color Palette");
text1 = new JTextField(20);
text2 = new JTextField(20);
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)
{
Main m1 = new Main();
m1.process();
}
}
</pre></code>
推荐答案
大多数颜色都没有名称.您的映射只能从另一端进行.
Most colors don't have something like a name. Your mapping would only work from the other side.
这篇关于获得JColorChooser的颜色名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!