【问题标题】:Java GridBag Layout not working properlyJava GridBag 布局无法正常工作
【发布时间】:2012-06-28 20:15:14
【问题描述】:

我有两张桌子和两个按钮。我希望 table-1 位于第一列并且宽度为 3 个单位,然后我希望表格右侧的两个按钮更窄(宽度为 1 个单位),我希望按钮一个在顶部,按钮 2 在底端。现在在这些按钮的右侧,我希望另一个表(表 2)再次宽 3 个单位。为此,我使用了以下约束:

        //table1
        c = new GridBagConstraints(0, 0, 3, 2, 1, 1,
                GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
                new Insets(0, 0, 0, 0), 0, 0);
        c.fill = GridBagConstraints.BOTH;
        outerPanel.add(all_app_scrollpane, c);

        //button1
        c = new GridBagConstraints(3, 0, 1, 1, 1, 1,
                GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
                new Insets(0, 0, 0, 0), 0, 0);
        outerPanel.add(addButton, c);

        //button2
        c = new GridBagConstraints(3, 1, 1, 1, 1, 1,
                GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
                new Insets(0, 0, 0, 0), 0, 0);
        outerPanel.add(removeButton, c);

        //table2
        c = new GridBagConstraints(4, 0, 3, 2, 1, 1,
                GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
                new Insets(0, 0, 0, 0), 0, 0);
        c.fill = GridBagConstraints.BOTH;
        outerPanel.add(curr_app_scrollpane, c);

我们看一下table1的GridBagCONstraints。据我所知,第一个参数声明它将从 0. 列开始,并且是 3 列宽(第三个参数)。 然后 button1 将从第三列开始,宽度为一列。但我想我知道错了,因为结果与我预期的非常不同。

我希望这些按钮更窄,表格更宽。我怎样才能做到这一点?我想使用 GridBag 来完成这个,因为这个小面板只是一个非常复杂的 gui 的一小部分,而整个 gui 是使用 gridbag 设计的。所以我不想改变编码风格。

【问题讨论】:

    标签: java swing user-interface gridbaglayout


    【解决方案1】:

    在 button1 和 button2 上,将它们的 weightx 降低到 0.0。像这样的:

    import java.awt.Component;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.util.Vector;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.SwingUtilities;
    import javax.swing.table.DefaultTableModel;
    
    public class Test4 {
    
        protected void initUI() {
            JFrame frame = new JFrame("test");
            JPanel panel = new JPanel(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.fill = GridBagConstraints.BOTH;
            gbc.weightx = 0.5;
            gbc.weighty = 1.0;
            gbc.gridheight = 2;
            panel.add(getTable(), gbc);
            gbc.gridx = 2;
            panel.add(getTable(), gbc);
    
            GridBagConstraints gbc2 = new GridBagConstraints();
            gbc2.fill = GridBagConstraints.NONE;
            gbc2.anchor = GridBagConstraints.CENTER;
            gbc2.gridx = 1;
            gbc2.weighty = 1.0;
            panel.add(new JButton("Button 1"), gbc2);
            gbc2.gridy = 1;
            panel.add(new JButton("Button 2"), gbc2);
            frame.add(panel);
            frame.pack();
            frame.setVisible(true);
        }
    
        private Component getTable() {
            Vector<Vector<String>> data = new Vector<Vector<String>>();
            for (int i = 0; i < 10; i++) {
                Vector<String> row = new Vector<String>();
                for (int j = 0; j < 3; j++) {
                    row.add("Cell (" + i + "," + j + ")");
                }
                data.add(row);
            }
            Vector<String> columns = new Vector<String>();
            columns.add("Column 1");
            columns.add("Column 2");
            columns.add("Column 3");
            DefaultTableModel model = new DefaultTableModel(data, columns);
            JTable table = new JTable(model);
            JScrollPane scroll = new JScrollPane(table);
            scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
            scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
            return scroll;
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    new Test4().initUI();
                }
            });
        }
    }
    

    对不起,约束重用,不利于可读性。

    【讨论】:

      【解决方案2】:

      您必须设置布局的增长设置(weightx),以便只有左右列在增长。

      像这样:

          contentPane.setLayout(new GridBagLayout());
          ((GridBagLayout)contentPane.getLayout()).columnWidths = new int[] {0, 0, 0, 0};
          ((GridBagLayout)contentPane.getLayout()).rowHeights = new int[] {0, 0, 10, 0, 0, 0};
          ((GridBagLayout)contentPane.getLayout()).columnWeights = new double[] {1.0, 0.0, 1.0, 1.0E-4};
          ((GridBagLayout)contentPane.getLayout()).rowWeights = new double[] {1.0, 0.0, 0.0, 0.0, 1.0, 1.0E-4};
      
          //---- leftBtn ----
          leftBtn.setText("left Button");
          contentPane.add(leftBtn, new GridBagConstraints(0, 0, 1, 5, 0.0, 0.0,
              GridBagConstraints.CENTER, GridBagConstraints.BOTH,
              new Insets(0, 0, 0, 0), 0, 0));
      
          //---- rightBtn ----
          rightBtn.setText("right Button");
          contentPane.add(rightBtn, new GridBagConstraints(2, 0, 1, 5, 0.0, 0.0,
              GridBagConstraints.CENTER, GridBagConstraints.BOTH,
              new Insets(0, 0, 0, 0), 0, 0));
      
          //---- addBtn ----
          addBtn.setText("add Button");
          contentPane.add(addBtn, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0,
              GridBagConstraints.CENTER, GridBagConstraints.BOTH,
              new Insets(0, 0, 0, 0), 0, 0));
      
          //---- remBtn ----
          remBtn.setText("rem Button");
          contentPane.add(remBtn, new GridBagConstraints(1, 3, 1, 1, 0.0, 0.0,
              GridBagConstraints.CENTER, GridBagConstraints.BOTH,
              new Insets(0, 0, 0, 0), 0, 0));
      

      所以它给出了:

      问候弗洛里安

      编辑:这里是 IDE 的屏幕截图,按钮之间有额外的空间:

      【讨论】:

      • 您的代码产生了非常不同的结果,并且与我想要的相差甚远。 GridBagConstraints构造函数的第一个参数是gridx,第三个是gridwidth。在您的代码中,左按钮位于 gridx 0 上,宽度为 1。add 和 rem 按钮位于 gridx1 上,宽度为 1。右按钮位于 gridx 2 上,宽度为 1。结果我没有达到我想要的效果。跨度>
      • 这里是 IDE 的截图,你可以看到我添加了一些行来使按钮居中并在它们之间创建一个间隙imgur.com/DHEMD
      • 在您的屏幕截图中,所有按钮的宽度都相同。我想要的是使中间的按钮更窄(我的意思是不那么宽)
      • 不,我看错了。对不起。我将再次查看您的代码。感谢您的帮助。我错过了
      • 你可以像我在行上做的一样。像这样添加一些额外的列:imgur.com/8kFcY 也将按钮设置为GridBagConstraints.CENTER
      猜你喜欢
      • 1970-01-01
      • 2012-10-10
      • 1970-01-01
      • 2018-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-13
      相关资源
      最近更新 更多