1 回答
TA贡献1862条经验 获得超6个赞
您可以像任何其他数组一样将 an 的值添加enum到 a 中。JList例如:
public class Example implements Runnable {
private JPanel jPanel;
private JFrame jFrame;
private JList<Algorithm> diagnosisList;
private DefaultListModel<Algorithm> model;
public static void main(String[] args) {
EventQueue.invokeLater(new Example());
}
@Override
public void run() {
jFrame = new JFrame();
jPanel = new JPanel();
jPanel.setLayout(new FlowLayout());
jFrame.getContentPane().add(jPanel);
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
jPanel.setPreferredSize(new Dimension(500, 300));
model = new DefaultListModel<>();
diagnosisList = new JList<Algorithm>(model);
for (Algorithm a : Algorithm.values())
model.addElement(a);
jPanel.add(diagnosisList);
jFrame.pack();
jFrame.setVisible(true);
}
public static enum Algorithm {
BFS, DFS, A_STAR
}
}
如果您不喜欢它的渲染方式,您可以随时添加自己的ListCellRenderer:
diagnosisList.setCellRenderer(new DefaultListCellRenderer() {
private static final long serialVersionUID = 1L;
@Override
public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected,
boolean cellHasFocus) {
JLabel renderer = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
renderer.setText(renderer.getText().toLowerCase()); //something
return renderer;
}
});
添加回答
举报