【问题标题】:GridLayout wrong number of columnsGridLayout 列数错误
【发布时间】:2013-06-20 17:55:48
【问题描述】:

我正在尝试使用 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); 

【问题讨论】:

  • 你能提供更多代码吗?
  • 给你,我刚刚编辑了帖子
  • 您尝试在面板中添加超过 14 个组件,即 7 x 2 = 14。
  • 尝试做类似的事情:GridLayout(0,2); 我相信这会给你任何你想要的行和 2 列。来源:docs.oracle.com/javase/tutorial/uiswing/layout/grid.html

标签: java swing jpanel grid-layout


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:
    • 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();
                }
            });
        }
    }
    

    【讨论】:

    • 另外,我相信他可以简单地做GridLayout(0,2),这样他就不需要指定行数。 编辑不错的答案!来源和示例 +1!
    • 谢谢,但它对我不起作用,我不知道为什么。我用过GridLayout(0,2),非常完美。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-14
    相关资源
    最近更新 更多