GridLayout列数错误

时间:2013-06-20 17:55:48

标签: java swing jpanel grid-layout

我正在尝试使用GridLayout(7,2)

创建面板
membersPanel = new JPanel(new GridLayout(7,2));

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

Image

我尝试将列数更改为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); 

2 个答案:

答案 0 :(得分:5)

  • 由GridLayout(7,2)分层的JComponent数量与JComponents不对应到一个容器中,

  • 必须将超过21个JComponents放入一个容器中

super(new GridLayout(8, 8));

的输出

enter image description here

super(new GridLayout(N, N));

的输出

enter image description here

来自代码

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();
            }
        });
    }
}

答案 1 :(得分:5)

如果添加的组件数超过指定的原始数,

Swing会调整用于GridLayout的列数。使用0指定可调整的行数:

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

这将使任何未来的重新分解更容易。