1

我正在制作战舰游戏,我正在尝试找出一种方法来控制窗格中的按钮,以便我可以拖放它们并使用默认列表模型跟踪它们的索引。如果我添加字符串或 ImageIcons 它工作正常但有了按钮,我得到了不同的东西。

这是我的代码:

public class ListModelExample extends JPanel {

  JList list;
  DefaultListModel model;
  int counter = 15;

  public ListModelExample() {
    setLayout(new BorderLayout());
    model = new DefaultListModel();
    list = new JList(model);
    JScrollPane pane = new JScrollPane(list);
    JButton addButton = new JButton("Add Element");
    JButton removeButton = new JButton("Remove Element");
    final JButton button = new JButton("button");

    for (int i = 0; i < 5; i++)
      model.addElement(button);

    addButton.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        model.addElement(button);
        counter++;    

      }
    });
    removeButton.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        if (model.getSize() > 0)
          model.removeElementAt(0);
      }
    });

    add(pane, BorderLayout.NORTH);
    add(addButton, BorderLayout.WEST);
    add(removeButton, BorderLayout.EAST);
  }

  public static void main(String s[]) {
    JFrame frame = new JFrame("List Model Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setContentPane(new ListModelExample());
    frame.setSize(260, 200);
    frame.setVisible(true);

  }
}

如果我添加按钮,我会得到这个结果:

在窗格中添加按钮的结果

所以我的问题是:如何使按钮正常显示而不是默认列表模型中的文本?

4

1 回答 1

3
  • 这只能通过Renderer,只StringDefaultListModel

  • 不要放任何JComponents东西XxxModel

  • 我将使用JPanelwithJButtons而不是容器(与or相比,JList需要更改getScrollableBlockIncrement/getScrollableUnitIncrement以进行自然滚动)JListJTable

关于两种方式的示例

import java.awt.*;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import javax.swing.*;

public class ListButtons extends JFrame {

    private static final long serialVersionUID = 1L;

    public ListButtons() {
        setLayout(new GridLayout(0, 2, 10, 10));
        DefaultListModel model = new DefaultListModel();
        model.addElement(createButtons("one"));
        model.addElement(createButtons("two"));
        model.addElement(createButtons("three"));
        model.addElement(createButtons("four"));
        model.addElement(createButtons("five"));
        model.addElement(createButtons("six"));
        model.addElement(createButtons("seven"));
        model.addElement(createButtons("eight"));
        model.addElement(createButtons("nine"));
        model.addElement(createButtons("ten"));
        model.addElement(createButtons("eleven"));
        model.addElement(createButtons("twelwe"));
        JList list = new JList(model);
        list.setCellRenderer(new PanelRenderer());
        JScrollPane scroll1 = new JScrollPane(list);
        final JScrollBar scrollBar = scroll1.getVerticalScrollBar();
        scrollBar.addAdjustmentListener(new AdjustmentListener() {

            @Override
            public void adjustmentValueChanged(AdjustmentEvent e) {
                System.out.println("JScrollBar's current value = " + scrollBar.getValue());
            }
        });
        add(scroll1);
        JScrollPane scroll2 = new JScrollPane(createPanel());
        add(scroll2);        
        final JScrollBar scrollBar1 = scroll2.getVerticalScrollBar();
        scrollBar1.addAdjustmentListener(new AdjustmentListener() {

            @Override
            public void adjustmentValueChanged(AdjustmentEvent e) {
                System.out.println("JScrollBar's current value = " + scrollBar1.getValue());
            }
        });

    }

    public static JPanel createPanel() {
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(0, 1, 1, 1));
        panel.add(createButtons("one"));
        panel.add(createButtons("two"));
        panel.add(createButtons("three"));
        panel.add(createButtons("four"));
        panel.add(createButtons("five"));
        panel.add(createButtons("six"));
        panel.add(createButtons("seven"));
        panel.add(createButtons("eight"));
        panel.add(createButtons("nine"));
        panel.add(createButtons("ten"));
        panel.add(createButtons("eleven"));
        panel.add(createButtons("twelwe"));
        return panel;
    }

    public static JButton createButtons(String text) {
        JButton button = new JButton(text);
        return button;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                ListButtons frame = new ListButtons();
                frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
                //frame.pack();
                frame.setSize(270, 200);
                frame.setVisible(true);
            }
        });
    }

    class PanelRenderer implements ListCellRenderer {

        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
            JButton renderer = (JButton) value;
            renderer.setBackground(isSelected ? Color.red : list.getBackground());
            return renderer;
        }
    }
}
于 2013-04-07T13:56:07.387 回答