简体   繁体   English

如何更改 JComboBox 中的颜色格式?

[英]How to change format of color in JComboBox?

I am training a GUI and am facing a string formatting problem.我正在训练 GUI 并面临字符串格式问题。 Don't understand how to display the colors in the list in a user-readable format?不明白如何以用户可读的格式显示列表中的颜色?

  public static void main(String[] args) {

        JFrame jframe = getFrame();

        jframe.setTitle("Background color");

        Toolkit toolkit = Toolkit.getDefaultToolkit();

        Dimension dimension = toolkit.getScreenSize();

        jframe.setBounds(dimension.width/2-250, dimension.height/2-150, 500, 300);

        JPanel jpanel = new JPanel();

        JButton setColor = new JButton("Set Color");

        Color colors[] = {Color.red, Color.green, Color.blue, Color.black};
        
        JComboBox<Color> selector = new JComboBox<>(colors);

        setColor.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                jpanel.setBackground((Color)selector.getSelectedItem());
            }
        });

        jpanel.add(selector);

        jpanel.add(setColor);

        jframe.add(jpanel);
        
      }

      public static JFrame getFrame() {
        JFrame jframe = new JFrame();
        jframe.setVisible(true);
        jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        return jframe;
      }
  }

And here my output:这里是我的输出:

So, I want the name of the color to be displayed instead of java.awt.Color[.....]所以,我希望显示颜色的名称而不是 java.awt.Color[.....]

https://i.stack.imgur.com/UyO9B.png

Thanks in advance!提前致谢!

By default, JComboBox displays the value returned by the toString method of the objects in its list.默认情况下, JComboBox显示其列表中对象的toString方法返回的值。 Since your JComboBox contains Color objects, you see the value returned by method toString of class java.awt.Color .由于您的JComboBox包含Color对象,您会看到类java.awt.Color方法toString返回的值。

If you want to display a color name, then you need to create a custom class that stores both the name of the color, as a String and the Color object.如果要显示颜色名称,则需要创建一个自定义类,将颜色名称存储为StringColor对象。 Then you need to override the toString method of the custom class to return just the name of the color.然后您需要覆盖自定义类的toString方法以仅返回颜色的名称。 In the below code, the custom class is named NamedColor .在下面的代码中,自定义类被命名为NamedColor

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class ColorSet {

    public static void main(String[] args) {
        JFrame jframe = new JFrame("Background color");
        jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Toolkit toolkit = Toolkit.getDefaultToolkit();
        Dimension dimension = toolkit.getScreenSize();
        jframe.setBounds(dimension.width / 2 - 250, dimension.height / 2 - 150, 500, 300);
        JPanel jpanel = new JPanel();
        JButton setColor = new JButton("Set Color");
        NamedColor colors[] = {new NamedColor(Color.red, "RED"),
                               new NamedColor(Color.green, "GREEN"),
                               new NamedColor(Color.blue, "BLUE"),
                               new NamedColor(Color.black, "BLACK")};
        JComboBox<NamedColor> selector = new JComboBox<>(colors);
        setColor.addActionListener(
                  e -> jpanel.setBackground(((NamedColor) selector.getSelectedItem()).getColor()));
        jpanel.add(selector);
        jpanel.add(setColor);
        jframe.add(jpanel);
        jframe.setVisible(true);
    }
}

class NamedColor {
    private Color color;
    private String name;

    public NamedColor(Color color, String name) {
        this.color = color;
        this.name = name;
    }

    public Color getColor() {
        return color;
    }

    public String toString() {
        return name;
    }
}

Notes regarding above code.关于上述代码的注意事项。

  • The ActionListener interface is implemented using a lambda expression ActionListener接口是使用lambda 表达式实现的
  • You should call method setVisible , of class JFrame only after you have added all the components.只有在添加了所有组件之后,才应该调用JFrame类的setVisible方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM