我正在尝试添加 ai 猜测您会将其称为列表中每个项目的子列表。我已经构建了一个自定义渲染器,它提供了以下输出。正如您所看到的,这里有些东西不正确,我没有运气找到我的问题的答案。我猜我需要更改面板布局中的某些内容以获得正确的结果,但不知道是什么。
http://i.stack.imgur.com/jCKjJ.jpg
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.GridLayout;
import java.util.Random;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.ListCellRenderer;
import javax.swing.border.EmptyBorder;
public class GAListRendererAdv extends Container implements ListCellRenderer {
private static final long serialVersionUID = 1L;
private JPanel pnl = new JPanel();
private Random rnd = new Random();
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected,
boolean cellHasFocus) {
pnl.setLayout(new BorderLayout(0, 0));
JCheckBox chckbxSomething = new JCheckBox("something");
pnl.add(chckbxSomething, BorderLayout.NORTH);
JSeparator separator = new JSeparator();
pnl.add(separator, BorderLayout.CENTER);
JPanel panel_1 = new JPanel();
panel_1.setBorder(new EmptyBorder(0, 35, 0, 0));
pnl.add(panel_1, BorderLayout.SOUTH);
panel_1.setLayout(new GridLayout(0, 1, 0, 0));
int rndNum = rnd.nextInt(5) + 1;
for (int i = 0; i < rndNum; i ++) {
JLabel lblNewLabel2 = new JLabel("New label");
panel_1.add(lblNewLabel2);
}
return pnl;
}
}