java Jlist问题
如何让JList的不同列表项显示不同的颜色???比如,让奇数行显示蓝色,偶数行显示黑色??如何设置某行颜色后,使其不因为鼠标的选择而变颜色。。不是setSelection...
如何让JList的不同列表项显示不同的颜色???
比如,让奇数行显示蓝色,偶数行显示黑色??
如何设置某行颜色后,使其不因为鼠标的选择而变颜色。。不是setSelectionBackground()方法!!
给个代码例子。。。给代码!给代码!!
高手解决!! 展开
比如,让奇数行显示蓝色,偶数行显示黑色??
如何设置某行颜色后,使其不因为鼠标的选择而变颜色。。不是setSelectionBackground()方法!!
给个代码例子。。。给代码!给代码!!
高手解决!! 展开
展开全部
package com.akfucc.zhidao;
import java.awt.Color;
import java.awt.Component;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.ListModel;
/**
* @description: http://zhidao.baidu.com/question/124865850.html
* @author akfucc
* @date Nov 9, 2009
*
*/
public class Zhidao extends JFrame {
ListModel model;
JList listTest;
public Zhidao() {
super("JList 测试");
listTest = new JList(new String[] { "Item1", "Item2", "Item3", "Item4",
"Item4" });
// 设置这个东西
listTest.setCellRenderer(new MyRenderer());
this.add(listTest);
// //
this.setSize(200, 400);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
// 重点所在
private static class MyRenderer extends DefaultListCellRenderer {
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
super.getListCellRendererComponent(list, value, index, isSelected,
cellHasFocus);
if (index % 2 == 0) {
setBackground(Color.red);
} else {
setBackground(Color.green);
}
return this;
}
}
public static void main(String[] args) {
new Zhidao();
}
}
import java.awt.Color;
import java.awt.Component;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.ListModel;
/**
* @description: http://zhidao.baidu.com/question/124865850.html
* @author akfucc
* @date Nov 9, 2009
*
*/
public class Zhidao extends JFrame {
ListModel model;
JList listTest;
public Zhidao() {
super("JList 测试");
listTest = new JList(new String[] { "Item1", "Item2", "Item3", "Item4",
"Item4" });
// 设置这个东西
listTest.setCellRenderer(new MyRenderer());
this.add(listTest);
// //
this.setSize(200, 400);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
// 重点所在
private static class MyRenderer extends DefaultListCellRenderer {
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
super.getListCellRendererComponent(list, value, index, isSelected,
cellHasFocus);
if (index % 2 == 0) {
setBackground(Color.red);
} else {
setBackground(Color.green);
}
return this;
}
}
public static void main(String[] args) {
new Zhidao();
}
}
展开全部
1111111111
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
import java.awt.Color;
import java.awt.Component;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;
class MyCellRenderer extends JLabel implements ListCellRenderer {
public MyCellRenderer() {
setOpaque(true);
}
public Component getListCellRendererComponent(JList list,
Object value,
int index,
boolean isSelected,
boolean cellHasFocus) {
this.setForeground(Color.WHITE);
setText(value.toString());
if (index % 2 == 0) {
setBackground(Color.BLACK);
} else {
setBackground(Color.BLUE);
}
return this;
}
}
public class Test {
public static void main(String[] args) {
MyCellRenderer cellRenderer = new MyCellRenderer();
JList list = new JList(new Object[]{1, 2, 3, 4, 5, 6});
list.setCellRenderer(cellRenderer);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(3);
frame.setContentPane(list);
frame.setVisible(true);
}
}
import java.awt.Component;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;
class MyCellRenderer extends JLabel implements ListCellRenderer {
public MyCellRenderer() {
setOpaque(true);
}
public Component getListCellRendererComponent(JList list,
Object value,
int index,
boolean isSelected,
boolean cellHasFocus) {
this.setForeground(Color.WHITE);
setText(value.toString());
if (index % 2 == 0) {
setBackground(Color.BLACK);
} else {
setBackground(Color.BLUE);
}
return this;
}
}
public class Test {
public static void main(String[] args) {
MyCellRenderer cellRenderer = new MyCellRenderer();
JList list = new JList(new Object[]{1, 2, 3, 4, 5, 6});
list.setCellRenderer(cellRenderer);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(3);
frame.setContentPane(list);
frame.setVisible(true);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询