3

我有两张桌子和两个按钮。我希望 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 设计的。所以我不想改变编码风格。

4

2 回答 2

3

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

很抱歉约束重用,它不利于可读性。

于 2012-06-27T12:07:23.633 回答
3

您必须设置布局的增长设置 (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 的屏幕截图,按钮之间有额外的空间:演示2

于 2012-06-27T12:13:37.450 回答