日期:2014-05-20  浏览次数:20748 次

实在没想到啥招获取上一个元素的背景色。JList中使用ListCellRenderer为元素附背景色,怎样获取上面元素的背景色啊
崩溃,咋获得上一个元素的背景色呢?
我要的效果是,右侧的Jlist中的元素,若是连续的(如2,3,4,5)那么他们就显示同一背景色,当出现与上面的元素不连续的元素时,其背景色与它上面的那一个元素背景色不同。只使用绿色和黄色2种背景色。我现在做的无法获得上面元素的背景色。附代码,请前辈指点

import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.ListCellRenderer;
import javax.swing.ListModel;
import javax.swing.WindowConstants;

/**
 *
 * @author __USER__
 */
public class MyDemo extends JDialog implements ActionListener {

private JList jList1;
private JButton jButton1;
private JButton jButton2;
private JList jList2;
private DefaultListModel listModel1;
private DefaultListModel listModel2;
private JButton confirmBtn;
private JScrollPane paneA;
private JScrollPane paneB;

{
//Set Look & Feel
try {
javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch(Exception e) {
e.printStackTrace();
}
}


/**
* Auto-generated main method to display this JFrame
*/
public static void main(String[] args) {
MyDemo inst = new MyDemo();
inst.setVisible(true);
}
 
public MyDemo() {
super();
initGUI();
}
 
private void initGUI() {
try {
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
paneA = new JScrollPane();
paneB = new JScrollPane();
getContentPane().setLayout(null);
getContentPane().add(paneA);
getContentPane().add(paneB);
this.setTitle("光缆熔接");
 
{
listModel1 = new DefaultListModel();
listModel1.addElement("1");
listModel1.addElement("2");
listModel1.addElement("3");
listModel1.addElement("4");
listModel1.addElement("5");
listModel1.addElement("6");
listModel1.addElement("7");
listModel1.addElement("8");
jList1 = new JList(listModel1);
 
paneA.setBounds(30, 28, 130, 250);
paneA.setViewportView(jList1);
}
{
listModel2 = new DefaultListModel();
jList2 = new JList(listModel2);
 
jList2.setCellRenderer(new ColorRender());
 
paneB.setBounds(330, 28, 130, 250);
paneB.setViewportView(jList2);
 
}
{
jButton1 = new JButton();
getContentPane().add(jButton1);
jButton1.setText("添加->>");;
jButton1.setBounds(210, 80, 80, 28);
jButton1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
}
{
jButton2 = new JButton();
getContentPane().add(jButton2);
jButton2.setText("<<-移除");
jButton2.setBounds(210, 170, 80, 28);
jButton2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
}
{
confirmBtn = new JButton("确定");
getContentPane().add(confirmBtn);
confirmBtn.setBounds(370, 300, 80, 28);
confirmBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {