Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
196 views
in Technique[技术] by (71.8m points)

java - get a color name of JColorChooser

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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...