Цвет шрифта в JComboBox [дубликат]

Привет, друзья, очень просто

<html>
<body>
<p>Click the button to calculate x.</p>
<button onclick="myFunction()">Try it</button>
<br/>
<br/>Enter first number:
<input type="text" id="txt1" name="text1">Enter second number:
<input type="text" id="txt2" name="text2">
<p id="demo"></p>
<script>
  function myFunction() {
    var y = document.getElementById("txt1").value;
    var z = document.getElementById("txt2").value;
    var x = +y + +z;
    document.getElementById("demo").innerHTML = x;
  }
</script>

11
задан Marek Ceglowski 8 June 2012 в 16:08
поделиться

1 ответ

Это вы имеете в виду?

TestComboColor [/g0]

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;
    }
}
14
ответ дан Andrew Thompson 21 August 2018 в 20:46
поделиться
Другие вопросы по тегам:

Похожие вопросы: