gpt4 book ai didi

java - 无法将复选框添加到 JList

转载 作者:行者123 更新时间:2023-11-29 09:41:12 25 4
gpt4 key购买 nike

我是编程新手,无法将JCheckbox 添加到JList。没有错误,但没有任何显示。

JFrame f=new JFrame();
String[] labels={"a","b","c","d","e"};
JCheckBox[] ch=new JCheckBox[labels.length];

JList list=new JList();

for (int i = 0; i < labels.length; i++) {
ch[i]=new JCheckBox("CheckBox"+i);
list.add(ch[i]);
}

JScrollPane pane=new JScrollPane(list);
f.add(pane);
f.setVisible(true);

最佳答案

作为 trashgod 发布的 JTable 解决方案的替代方案,您还可以通过以下方式在 JList 中创建 JCheckBoxes 的外观:

  1. 为您的 JList 使用自定义渲染器,将每个项目显示为 JCheckBox
  2. 在您的 JList 中使用一个自定义对象来保持它的 boolean “选中”状态
  3. 将 MouseListener 添加到 JList 以设置/取消设置每个项目的选中状态。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

public class JCheckBoxListDemo implements Runnable
{
private JList jlist;

public static void main(String args[])
{
SwingUtilities.invokeLater(new JCheckBoxListDemo());
}

public void run()
{
Object[] items = new CheckListItem[] {
new CheckListItem("Apple"),
new CheckListItem("Banana"),
new CheckListItem("Carrot"),
new CheckListItem("Date"),
new CheckListItem("Eggplant"),
new CheckListItem("Fig"),
new CheckListItem("Guava"),
};

jlist = new JList(items);
jlist.setCellRenderer(new CheckBoxListRenderer());
jlist.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
jlist.setVisibleRowCount(5);
jlist.addMouseListener(new MouseAdapter()
{
@Override
public void mouseClicked(MouseEvent event)
{
selectItem(event.getPoint());
}
});

KeyStroke keyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0);
Object mapKey = keyStroke.toString();
jlist.getInputMap().put(keyStroke, mapKey);
jlist.getActionMap().put(mapKey, new AbstractAction()
{
public void actionPerformed(ActionEvent event)
{
toggleSelectedItem();
}
});

JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new JScrollPane(jlist));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

private void selectItem(Point point)
{
int index = jlist.locationToIndex(point);

if (index >= 0)
{
CheckListItem item = (CheckListItem)jlist.getModel().getElementAt(index);
item.setSelected(!item.isSelected());
jlist.repaint(jlist.getCellBounds(index, index));
}
}

private void toggleSelectedItem()
{
int index = jlist.getSelectedIndex();

if (index >= 0)
{
CheckListItem item = (CheckListItem)jlist.getModel().getElementAt(index);
item.setSelected(!item.isSelected());
jlist.repaint(jlist.getCellBounds(index, index));
}
}

private class CheckListItem
{
private Object item;
private boolean selected;

public CheckListItem(Object item)
{
this.item = item;
}

@SuppressWarnings("unused")
public Object getItem()
{
return item;
}

public boolean isSelected()
{
return selected;
}

public void setSelected(boolean isSelected)
{
this.selected = isSelected;
}

@Override
public String toString()
{
return item.toString();
}
}

private class CheckBoxListRenderer extends JCheckBox
implements ListCellRenderer
{
public Component getListCellRendererComponent(JList comp, Object value,
int index, boolean isSelected, boolean hasFocus)
{
setEnabled(comp.isEnabled());
setSelected(((CheckListItem) value).isSelected());
setFont(comp.getFont());
setText(value.toString());

if (isSelected)
{
setBackground(comp.getSelectionBackground());
setForeground(comp.getSelectionForeground());
}
else
{
setBackground(comp.getBackground());
setForeground(comp.getForeground());
}

return this;
}
}
}

关于java - 无法将复选框添加到 JList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17951886/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com