4

我正在尝试使用 GridLayout(7,2) 创建面板

membersPanel = new JPanel(new GridLayout(7,2));

但是,当我添加组件(标签、组合框、文本字段等)时,组件会显示在 3 列中,如下所示:

图片

我尝试将列数更改为 1 甚至 0,但面板保持不变。我能做些什么?

编辑:

这里有更多代码:

 p1 = new JPanel();
membersPanel = new JPanel(new GridLayout(7,0));

resourcesLabel = new JLabel("Resources");
    membersPanel.add(resourcesLabel);       

    emptyLabel5 = new JLabel("");
    membersPanel.add(emptyLabel5);

    emptyLabel6 = new JLabel("");
    membersPanel.add(emptyLabel6);

    comboBoxResource = new JComboBox(configs.XMLreaderDOM4J.readResourceID());
    membersPanel.add(comboBoxResource);

    slider1 = new SliderWithTextField(1,10);
    textSli1 = new TextFieldFromSlider(this, slider1); 
    slider1.setTextField(textSli1);

    slider1.setValue(1);
    membersPanel.add(slider1);

    membersPanel.add(textSli1);

    emptyLabel2 = new JLabel();
    membersPanel.add(emptyLabel2);

    addButton1 = new JButton("Add");
    addButton1.addActionListener(new TrataEvento());
    membersPanel.add(addButton1);



    agregator1Label = new JLabel("Agretagor1");
    membersPanel.add(agregator1Label); 

    comboBoxAgregator1 = new JComboBox(configs.XMLreaderDOM4J.readAgregator1ID());
    membersPanel.add(comboBoxAgregator1);

    slider2 = new SliderWithTextField(1,10);
    textSli2 = new TextFieldFromSlider(this, slider2); 
    slider2.setTextField(textSli2);

    slider2.setValue(1);
    membersPanel.add(slider2);

    membersPanel.add(textSli2);
    addButton2 = new JButton("Add");
    addButton2.addActionListener(new TrataEvento());
    membersPanel.add(addButton2);

    emptyLabel3 = new JLabel();
    membersPanel.add(emptyLabel3);


    agregator0Label = new JLabel("Agregator0");
    membersPanel.add(agregator0Label);     

    comboBoxAgregator0 = new JComboBox(configs.XMLreaderDOM4J.readAgregator0ID());
    membersPanel.add(comboBoxAgregator0);

    slider3 = new SliderWithTextField(1,10);
    textSli3 = new TextFieldFromSlider(this, slider3); 
    slider3.setTextField(textSli3);

    slider3.setValue(1);
    membersPanel.add(slider3);

    membersPanel.add(textSli3);

    addButton3 = new JButton("Add");
    addButton3.addActionListener(new TrataEvento());
    membersPanel.add(addButton3);

    emptyLabel4 = new JLabel();
    membersPanel.add(emptyLabel4);

p1.add(membersPanel); 
4

2 回答 2

6

GridLayout如果添加的组件数量超过指定的原始数量,Swing 会调整用于的列数。用于0指定可调整的行数:

membersPanel = new JPanel(new GridLayout(0, 2));

这将使任何未来的重构变得更容易。

于 2013-06-20T18:08:22.603 回答
5
  • GridLayout(7,2) 分层的 JComponent 数量与 JComponents 不对应到一个容器中,

  • 必须在一个容器中放置超过 21 个 JComponent

输出自super(new GridLayout(8, 8));

在此处输入图像描述

输出自super(new GridLayout(N, N));

在此处输入图像描述

从代码

import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.EventHandler;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

/** based on  @see http://stackoverflow.com/questions/7702697 */
public class GridButtonPanel extends JPanel {

    private static final int N = 4;
    private final List<GridButton> list = new ArrayList<GridButton>();

    public GridButtonPanel() {
        //super(new GridLayout(8, 8));
        super(new GridLayout(N, N));
        for (int i = 0; i < N * N; i++) {
            int row = i / N;
            int col = i % N;
            GridButton gb = new GridButton(row, col);
            gb.addActionListener((ActionListener) EventHandler.create(ActionListener.class, this,
                    "actionName" + row + "A" + col));
            list.add(gb);
            this.add(gb);
        }
    }

    public void actionName0A0() {
        System.out.println(" Grid at Row 0, Column 0 ");
    }

    public void actionName0A1() {
        System.out.println(" Grid at Row 0, Column 1 ");
    }

    public void actionName1A0() {
        System.out.println(" Grid at Row 1, Column 0 ");
    }

    public void actionName1A1() {
        System.out.println(" Grid at Row 1, Column 1 ");
    }

    private GridButton getGridButton(int r, int c) {
        int index = r * N + c;
        return list.get(index);
    }

    private class GridButton extends JButton {

        private int row;
        private int col;

        public GridButton(int row, int col) {
            super("Row - " + row + ",  Col - " + col);
            this.row = row;
            this.col = col;
            this.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    int r = GridButton.this.row;
                    int c = GridButton.this.col;
                    GridButton gb = GridButtonPanel.this.getGridButton(r, c);
                    System.out.println("r" + r + ",c" + c
                            + " " + (GridButton.this == gb)
                            + " " + (GridButton.this.equals(gb)));
                }
            });
        }
    }

    private void display() {
        JFrame f = new JFrame("GridButton");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(this);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

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

            @Override
            public void run() {
                new GridButtonPanel().display();
            }
        });
    }
}
于 2013-06-20T18:07:53.683 回答