I have a color selector app that uses JSlider to show the RGB value. I would like to have it also show the hex value. Can someone please point me in the right direction? I would like to have the hex value show under the colorbox that appears on the right side of the panel.

/*
* Program: ColorSlider.java
* Descrip: Using JSlider and BoxLayout
*/
package colorslider;

import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;

public class ColorSlider
{
    static JPanel controls, colorPanel;
    static JSlider rSlider, gSlider, bSlider;
    static JLabel rLabel, gLabel, bLabel;

    public static void main(String[] args)
    {
       JFrame win = new JFrame("Color Chooser");
       win.setDefaultCloseOperation(3);
       win.add(new SlideColorPanel());
       win.pack();
       win.setResizable(false);
       win.setVisible(true);
    }

    static class SlideColorPanel extends JPanel
    {
        public SlideColorPanel()
        {
            rSlider = new JSlider(JSlider.HORIZONTAL, 0, 255, 0);
            rSlider.setMajorTickSpacing(50);
            rSlider.setMinorTickSpacing(10);
            rSlider.setPaintTicks(true);
            rSlider.setPaintLabels(true);
            rSlider.setAlignmentX(LEFT_ALIGNMENT);

            gSlider = new JSlider(JSlider.HORIZONTAL, 0, 255, 0);
            gSlider.setMajorTickSpacing(50);
            gSlider.setMinorTickSpacing(10);
            gSlider.setPaintTicks(true);
            gSlider.setPaintLabels(true);
            gSlider.setAlignmentX(LEFT_ALIGNMENT);

            bSlider = new JSlider(JSlider.HORIZONTAL, 0, 255, 0);
            bSlider.setMajorTickSpacing(50);
            bSlider.setMinorTickSpacing(10);
            bSlider.setPaintTicks(true);
            bSlider.setPaintLabels(true);
            bSlider.setAlignmentX(LEFT_ALIGNMENT);

            SliderListener listener = new SliderListener();

            rSlider.addChangeListener(listener);
            gSlider.addChangeListener(listener);
            bSlider.addChangeListener(listener);

            rLabel = new JLabel("Red: 0");
            rLabel.setAlignmentX(LEFT_ALIGNMENT);
            gLabel = new JLabel("Green: 0");
            gLabel.setAlignmentX(LEFT_ALIGNMENT);
            bLabel = new JLabel("Blue: 0");
            bLabel.setAlignmentX(LEFT_ALIGNMENT);

            controls = new JPanel();

            BoxLayout layout = new BoxLayout(controls, BoxLayout.Y_AXIS);
            controls.setLayout(layout);

            controls.add(rLabel);
            controls.add(rSlider);
            controls.add(Box.createRigidArea(new Dimension(0,20)));

            controls.add(gLabel);
            controls.add(gSlider);
            controls.add(Box.createRigidArea(new Dimension(0,20)));

            controls.add(bLabel);
            controls.add(bSlider);
            controls.add(Box.createRigidArea(new Dimension(0,20)));

            colorPanel = new JPanel();
            colorPanel.setPreferredSize(new Dimension(100,100));
            colorPanel.setBackground(Color.black);

            add(controls);
            add(colorPanel);
        }
    }
    static class SliderListener implements ChangeListener
    {
        private int red, green, blue;

        @Override
        public void stateChanged(ChangeEvent e)
        {
            red = rSlider.getValue();
            green = gSlider.getValue();
            blue = bSlider.getValue();

            rLabel.setText("Red: " + red);
            gLabel.setText("Green: " + green);
            bLabel.setText("Blue: " + blue);

            colorPanel.setBackground(new Color(red, green, blue));
        }

    }
}
有帮助吗?

解决方案

You can use the following to convert an int value to a hex value:

String hex = Integer.toHexString(...);

You can do that for each individual value.

Or, if you just want one hex value for the entire Color then you will need to get the int value of the Color object by using the getRGB(...) method. The hex value will include the alpha value in this case so you may want to ignore the first 2 characters.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top