JComboBox中每个项目的多个颜色 [英] Multiple Colors for Each Item in JComboBox

查看:192
本文介绍了JComboBox中每个项目的多个颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为不同的项目使用不同的颜色制作一个ComboBox。我写了一些测试代码,但它似乎不工作。在渲染器中添加会导致程序崩溃,但注释会使框显示在框架中。

I'm trying to make a ComboBox that uses different colors for different items. I wrote out some test code but it doesn't seem to work. Adding in the renderer causes the program to crash but commenting it out makes the box display on in the frame.

有什么我遗漏或我这样做错误的方法?我尝试使用自定义ComboBox呈现器教程作为示例。

Is there anything I'm missing or am I doing this the wrong way? I tried using the custom ComboBox Renderer tutorial as an example.

以下是我的代码:

TestComboColor.java

import java.awt.Color;

import javax.swing.JComboBox;
import javax.swing.JFrame;


public class TestComboColor {

    static Color[] colors = {Color.BLUE, Color.GRAY, Color.RED};
    static String[] strings = {"Test1", "Test2", "Test3"};

    public static void main(String[] args)
    {
        JFrame frame = new JFrame("JAVA");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JComboBox cmb = new JComboBox();
        ComboBoxRenderer renderer = new ComboBoxRenderer(cmb);

        renderer.setColors(colors);
        renderer.setStrings(strings);

        cmb.setRenderer(renderer);

        frame.add(cmb);
        frame.pack();
        frame.setVisible(true);
    }
}

ComboBoxRenderer.java / p>

ComboBoxRenderer.java

import java.awt.Color;
import java.awt.Component;

import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListCellRenderer;


public final class ComboBoxRenderer extends JPanel implements ListCellRenderer
{

    private static final long serialVersionUID = -1L;
    private Color[] colors;
    private String[] strings;

    JPanel textPanel;
    JLabel text;

    public ComboBoxRenderer(JComboBox combo) {

        textPanel = new JPanel();
        textPanel.add(this);
        text = new JLabel();
        text.setOpaque(true);
        text.setFont(combo.getFont());
        textPanel.add(text);
    }

    public void setColors(Color[] col)
    {
        colors = col;
    }

    public void setStrings(String[] str)
    {
        strings = str;
    }

    public Color[] getColors()
    {
        return colors;
    }

    public String[] getStrings()
    {
        return strings;
    }

    @Override
    public Component getListCellRendererComponent(JList list, Object value,
            int index, boolean isSelected, boolean cellHasFocus) {

        if (isSelected)
        {
            setBackground(list.getSelectionBackground());
        }
        else
        {
        }

        if (colors.length != strings.length)
        {
            System.out.println("colors.length does not equal strings.length");
            return this;
        }
        else if (colors == null)
        {
            System.out.println("use setColors first.");
            return this;
        }
        else if (strings == null)
        {
            System.out.println("use setStrings first.");
            return this;
        }

        text.setText(strings[index]);
        text.setForeground(colors[index]);
        text.setBackground(getBackground());
        return text;


    }

}

谢谢!

推荐答案

这是什么意思?

img src =https://i.stack.imgur.com/FQUud.pngalt =TestComboColor>

import java.awt.Color;
import java.awt.Component;
import javax.swing.*;

public class TestComboColor {

    static Color[] colors = {Color.BLUE, Color.GRAY, Color.RED};
    static String[] strings = {"Test1", "Test2", "Test3"};

    public static void main(String[] args)
    {
        JFrame frame = new JFrame("JAVA");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JComboBox cmb = new JComboBox(strings);
        ComboBoxRenderer renderer = new ComboBoxRenderer(cmb);

        renderer.setColors(colors);
        renderer.setStrings(strings);

        cmb.setRenderer(renderer);

        frame.add(cmb);
        frame.pack();
        frame.setVisible(true);
    }
}

class ComboBoxRenderer extends JPanel implements ListCellRenderer
{

    private static final long serialVersionUID = -1L;
    private Color[] colors;
    private String[] strings;

    JPanel textPanel;
    JLabel text;

    public ComboBoxRenderer(JComboBox combo) {

        textPanel = new JPanel();
        textPanel.add(this);
        text = new JLabel();
        text.setOpaque(true);
        text.setFont(combo.getFont());
        textPanel.add(text);
    }

    public void setColors(Color[] col)
    {
        colors = col;
    }

    public void setStrings(String[] str)
    {
        strings = str;
    }

    public Color[] getColors()
    {
        return colors;
    }

    public String[] getStrings()
    {
        return strings;
    }

    @Override
    public Component getListCellRendererComponent(JList list, Object value,
            int index, boolean isSelected, boolean cellHasFocus) {

        if (isSelected)
        {
            setBackground(list.getSelectionBackground());
        }
        else
        {
            setBackground(Color.WHITE);
        }

        if (colors.length != strings.length)
        {
            System.out.println("colors.length does not equal strings.length");
            return this;
        }
        else if (colors == null)
        {
            System.out.println("use setColors first.");
            return this;
        }
        else if (strings == null)
        {
            System.out.println("use setStrings first.");
            return this;
        }

        text.setBackground(getBackground());

        text.setText(value.toString());
        if (index>-1) {
            text.setForeground(colors[index]);
        }
        return text;
    }
}

这篇关于JComboBox中每个项目的多个颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆