获取 JColorChooser 的颜色名称
我正在使用 JFrame。在设计视图中,我放置了一个 JBUtton 、两个 JTextField 和摆动窗口 JColorChooser 。比我编写一个代码来在两个 JTextField 中获取十六进制值和颜色名称。该过程是如果我单击 JButton,它将打开 JColorChooser 调色板,之后我单击 JColorChooser 选择一种颜色,它会在其中显示相应的颜色十六进制值一个 JtextField 和另一个 JTextField 中的颜色名称。但我可以获得十六进制值,但我不知道如何获得颜色名称。
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>
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
大多数颜色没有名称之类的东西。您的映射只能从另一侧起作用。
Most colors don't have something like a name. Your mapping would only work from the other side.