Java JComboBox中每个项目的多种颜色
我正在尝试制作一个组合框,为不同的项目使用不同的颜色。我写了一些测试代码,但似乎不起作用。添加渲染器会导致程序崩溃,但对其进行注释会使框显示在帧中 我有没有遗漏什么,或者我做得不对?我试着以教程为例 这是我的密码: TestComboColor.javaJava JComboBox中每个项目的多种颜色,java,swing,combobox,render,Java,Swing,Combobox,Render,我正在尝试制作一个组合框,为不同的项目使用不同的颜色。我写了一些测试代码,但似乎不起作用。添加渲染器会导致程序崩溃,但对其进行注释会使框显示在帧中 我有没有遗漏什么,或者我做得不对?我试着以教程为例 这是我的密码: TestComboColor.java import java.awt.Color; import javax.swing.JComboBox; import javax.swing.JFrame; public class TestComboColor { stat
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);
}
}
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;
}
}
ComboBoxRenderer.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);
}
}
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;
}
}
谢谢 这就是你的意思吗
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;
}
}
这种颜色数组可以对安全颜色严格,对任何颜色都严格,或者与JColorChooser结合使用??颜色数组应该能够输入任何颜色,包括(新颜色(#,#,#,#)),尽管我刚刚使用安全颜色(color.BLUE等)进行测试。另外,我根本没有使用JColorChooser。+1我不知道,使用ListCellRenderer有这么简单:-)@nIcEcOw渲染器几乎和布局管理器一样出色。:)