简体   繁体   English

我无法按特定顺序将JButtons添加到JList

[英]I can't add JButtons to a JList in a specific order

I try to add JButtons in the JList to be like a board game. 我尝试在JList中添加JButton就像一个棋盘游戏。

Here's my code: 这是我的代码:

public class Board {

public Board() {
    JList list = new JList();
    list.setLayout(new GridLayout(10, 10));
    list.setDragEnabled(true);

    Container container = new Container();
    JFrame frame = new JFrame();
    JPanel panel1 = new JPanel();
    JPanel panel2 = new JPanel();
    frame.add(container);
    container.add(panel1);

    for (int j = 0; j < 99; j++) {
        list.add(createButton());
    }

    panel2.add(list);
    container.add(panel2);
    panel2.setLayout(new GridLayout());
    panel1.setBounds(50, 150, 150, 150);
    panel1.setBackground(Color.YELLOW);
    panel2.setBounds(650, 150, 500, 500);
    panel2.setBackground(Color.RED);

    frame.setSize(1366, 768);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
}

public JButton createButton() {
    JButton button = new JButton();
    button.setBackground(Color.BLUE);
    return button;
}
}

If I put 100 repetitions I get this: 如果我重复100次,我会得到这个: 在此输入图像描述

If I put 99 repetitions i get this: 如果我重复99次,我会得到这个:

在此输入图像描述

So my question is how can I fill the above board? 所以我的问题是如何填写上面的板?

The reason it's happening is because you're adding all the buttons to a JList, which isn't really how you're meant to use a JList (usually you modify the model backing the list in order to add/remove items from the list). 发生这种情况的原因是因为你要将所有按钮添加到JList中,这实际上并不是你打算如何使用JList(通常你修改支持列表的模型以便从列表中添加/删除项目)。 The list is internally doing something that takes up the first slot. 该列表在内部执行占用第一个插槽的内容。

If you change this line: 如果您更改此行:

JList list = new JList();

to: 至:

JPanel list = new JPanel();

your layout will work (you have to remove the setDragEnabled line as well, and change 99 to 100). 您的布局将起作用(您还必须删除setDragEnabled行,并将99更改为100)。 Whether there's some capability of JList that you want I'm not sure, but that's why your layout isn't working properly. 是否有一些你想要的JList功能我不确定,但这就是你的布局无法正常工作的原因。

You should create your JList : 你应该创建你的JList:

DefaultListModel dataModel = new DefaultListModel();
JList list = new JList(dataModel);
list.setLayoutOrientation(JList.HORIZONTAL_WRAP);

Set a ListCellRenderer as follows: 设置ListCellRenderer如下:

 final JButton button = createButton();
    list.setCellRenderer(new ListCellRenderer() {
             @Override
             public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
                  return button;
             }
    });

Add your items as follows: // doesn't matter what value are you adding, you will decide what information do you need when rendering an item 按如下方式添加项目://无论您添加什么值,您将决定在渲染项目时需要哪些信息

    for (int j = 0; j < 99; j++) {
        dataModel.add(j, j); 
    }

And an example using this: How To Create custom JList 并使用此示例: 如何创建自定义JList

Edit : some other useful references: Drag and drop Drag and drop inside JList 编辑 :一些其他有用的参考: 拖放 在JList中拖放

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM