我在想是否有可能在jlist项目上放置或设置助记符到目前为止我搜索jlist教程并且几乎所有这些都是事件鼠标点击所以我在想是否可以为每个项目放置助记符在列表中,我正在处理此代码
public class CheckList
{
public static void main(String args[])
{
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create a list containing CheckListItem's
JList list = new JList(new CheckListItem[] {
new CheckListItem("apple"),
new CheckListItem("orange"),
new CheckListItem("mango"),
new CheckListItem("paw paw"),
new CheckListItem("banana")});
// Use a CheckListRenderer (see below)
// to renderer list cells
list.setCellRenderer(new CheckListRenderer());
list.setSelectionMode(
ListSelectionModel.SINGLE_SELECTION);
// Add a mouse listener to handle changing selection
list.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent event)
{
JList list = (JList) event.getSource();
// Get index of item clicked
int index = list.locationToIndex(event.getPoint());
CheckListItem item = (CheckListItem)
list.getModel().getElementAt(index);
// Toggle selected state
item.setSelected(! item.isSelected());
// Repaint cell
list.repaint(list.getCellBounds(index, index));
}
});
frame.getContentPane().add(new JScrollPane(list));
frame.pack();
frame.setVisible(true);
}
}
来自link
赞赏任何想法或建议