获取 JColorChooser 的颜色名称

发布于 2024-12-28 08:18:50 字数 2100 浏览 2 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

记忆之渊 2025-01-04 08:18:50

大多数颜色没有名称之类的东西。您的映射只能从另一侧起作用。

Most colors don't have something like a name. Your mapping would only work from the other side.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文