2

所以我现在已经为此工作了 45 分钟,我似乎无法弄清楚为什么这么简单的事情不起作用。我有一个非常复杂的 GUI,并且已经按照我想要的方式在布局中得到了几乎所有的东西,但是当我尝试制作它JListJCheckBoxes完全没有用。所以我在这里将我的问题简化为这个测试类,也许我正在做一些非常愚蠢的事情,但这是我的代码:

import java.awt.BorderLayout;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class TestGUI extends JFrame{
    public TestGUI(){
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setVisible(true);
        setTitle("TEST");
        JPanel outside = new JPanel();
        add(outside);
        outside.setLayout(new BorderLayout());
        JList<JCheckBox> checks = new JList<JCheckBox>();
        JCheckBox c = new JCheckBox("Check");
        checks.add(c);
        JScrollPane t = new JScrollPane(checks);
        add(t, BorderLayout.CENTER);
        pack();
    }
    public static void main(String[] args) {
        new TestGUI();
    }
}

当我运行 GUI 时,什么都没有出现...只是空白,没有复选框...

4

1 回答 1

2
  1. 不要JFrame不必要地扩展课程。
  2. JFrame#setVisible(true)在添加所有组件之前不要调用。
  3. 对 UI 组件使用 Event-Dispatch-Thread/Intial Thread
  4. 你在JPanel外面添加,然后JScrollPane在上面添加我不认为那是你想要做的?

我能够为您整理一个简短的示例,该示例使用自定义ListCellRenderer呈现JCheckBoxJList. 它还用于MouseAdapter允许检查和取消检查JCheckBox

在此处输入图像描述

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.ListCellRenderer;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

public class JListTest {

    public JListTest() {
        JFrame frame = new JFrame();
        frame.setTitle("JList Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        initComponents(frame);

        frame.pack();
        frame.setVisible(true);
    }

    private void initComponents(JFrame frame) {
        String[] strs = {"swing", "home", "basic", "metal"};

        final JList checkBoxesJList = new JList(createData(strs));
        checkBoxesJList.setCellRenderer(new CheckListRenderer());
        checkBoxesJList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        checkBoxesJList.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                int index = checkBoxesJList.locationToIndex(e.getPoint());
                CheckableItem item = (CheckableItem) checkBoxesJList.getModel().getElementAt(index);
                item.setSelected(!item.isSelected());
                Rectangle rect = checkBoxesJList.getCellBounds(index, index);
                checkBoxesJList.repaint(rect);
            }
        });

        JScrollPane scrollPane = new JScrollPane(checkBoxesJList);
        frame.add(scrollPane, BorderLayout.CENTER);
    }

    private CheckableItem[] createData(String[] strs) {
        int n = strs.length;
        CheckableItem[] items = new CheckableItem[n];
        for (int i = 0; i < n; i++) {
            items[i] = new CheckableItem(strs[i]);
        }
        return items;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new JListTest();
            }
        });
    }
}

class CheckListRenderer extends JCheckBox implements ListCellRenderer {

    public CheckListRenderer() {
        setBackground(UIManager.getColor("List.textBackground"));
        setForeground(UIManager.getColor("List.textForeground"));
    }

    @Override
    public Component getListCellRendererComponent(JList list, Object value,
            int index, boolean isSelected, boolean hasFocus) {
        setEnabled(list.isEnabled());
        setSelected(((CheckableItem) value).isSelected());
        setFont(list.getFont());
        setText(value.toString());
        return this;
    }
}

class CheckableItem {

    private String str;
    private boolean isSelected;

    public CheckableItem(String str) {
        this.str = str;
        isSelected = false;
    }

    public void setSelected(boolean b) {
        isSelected = b;
    }

    public boolean isSelected() {
        return isSelected;
    }

    @Override
    public String toString() {
        return str;
    }
}

参考:

于 2012-10-18T06:59:22.270 回答