-3

我正在为我的应用程序使用 GridBagLayout,因为我认为设置组件的位置更容易。但我有宽度的问题。查看确定按钮

查看确定按钮

和代码:

public LoginForm1 (){
    getContentPane().setLayout(new BorderLayout(0,0));
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    this.setExtendedState(MAXIMIZED_BOTH);
    this.setBounds(100, 100, 800, 600);

    JPanel MainPanel = new JPanel();    
    getContentPane().add(MainPanel, BorderLayout.CENTER);
    GridBagLayout GridPanel = new GridBagLayout();
    GridPanel.columnWidths = new int[] {0,0,0,0};
    GridPanel.rowHeights = new int[] {0,0,0,0,0};
    GridPanel.columnWeights = new double[] {0.0, 1.0, 1.0, 0.0};
    GridPanel.rowWeights = new double[] {1.0, 0.1, 0.1, 1.0, 0.1};
    MainPanel.setLayout(GridPanel);

    JLabel userLabel = new JLabel("Username : ");
    userLabel.setFont(new Font("Tahoma", Font.BOLD, 20));
    GridBagConstraints userLabelGrid = new GridBagConstraints();
    userLabelGrid.anchor = GridBagConstraints.EAST;
    userLabelGrid.insets = new Insets(5, 5, 5, 5);
    userLabelGrid.gridx = 1;
    userLabelGrid.gridy = 1;
    MainPanel.add(userLabel, userLabelGrid);

    JLabel passLabel = new JLabel("Password : ");
    passLabel.setFont(new Font("Tahoma", Font.BOLD, 20));
    GridBagConstraints passLabelGrid = new GridBagConstraints();
    passLabelGrid.anchor = GridBagConstraints.EAST;
    passLabelGrid.insets = new Insets(5, 5, 5, 5);
    passLabelGrid.gridx = 1;
    passLabelGrid.gridy = 2;
    MainPanel.add(passLabel, passLabelGrid);

    JTextField userField = new JTextField();
    userField.setFont(UIManager.getFont("TextField.font"));
    GridBagConstraints userFieldGrid = new GridBagConstraints();
    userFieldGrid.fill = GridBagConstraints.VERTICAL;
    userFieldGrid.insets = new Insets(5, 5, 0, 0);
    userFieldGrid.anchor = GridBagConstraints.WEST;
    userFieldGrid.gridx = 2;
    userFieldGrid.gridy = 1;
    userField.setColumns(30);
    MainPanel.add(userField, userFieldGrid);

    JTextField passField = new JTextField();
    passField.setFont(UIManager.getFont("PasswordField.font"));
    GridBagConstraints passFieldGrid = new GridBagConstraints();
    passFieldGrid.fill = GridBagConstraints.VERTICAL;
    passFieldGrid.insets = new Insets(5, 5, 0, 0);
    passFieldGrid.anchor = GridBagConstraints.WEST;
    passFieldGrid.gridx = 2;
    passFieldGrid.gridy = 2;
    passField.setColumns(30);
    MainPanel.add(passField, passFieldGrid);

    JButton okButton = new JButton();
    GridBagConstraints okButtonGrid = new GridBagConstraints();
    okButtonGrid.fill = GridBagConstraints.BOTH;
    okButtonGrid.insets = new Insets(5, 5, 0, 0);
    okButtonGrid.gridx = 1;
    okButtonGrid.gridy = 4;
    okButton.setText("OK");
    MainPanel.add(okButton, okButtonGrid);

    JButton cancelButton = new JButton();
    GridBagConstraints cancelButtonGrid = new GridBagConstraints();
    cancelButtonGrid.fill = GridBagConstraints.BOTH;
    cancelButtonGrid.insets = new Insets(5, 5, 0, 5);
    cancelButtonGrid.gridx = 2;
    cancelButtonGrid.gridy = 4;
    cancelButton.setText("CANCEL");
    MainPanel.add(cancelButton, cancelButtonGrid);
}

我错过了什么??请帮助,在此先感谢:)

4

2 回答 2

2

您可以删除.fill = GridBagConstraints.BOTH两个按钮的,这将阻止按钮想要扩展以填充单元格的整个宽度(和高度)......

在此处输入图像描述

我也会看看你的GridPanel.columnWeights,并GridPanel.rowWeights确保它们满足你的需求......

由于 的性质GridBagLayout,您实际上最好使用一系列面板分别在单个元素(字段和按钮)中进行布局,这将使您更好地控制布局......

在此处输入图像描述

public class LoginPane extends JPanel {

    public LoginPane() {
        setLayout(new BorderLayout(0, 0));
        JPanel mainPanel = new JPanel();

        add(mainPanel, BorderLayout.CENTER);

        mainPanel.setLayout(new GridBagLayout());
        JLabel userLabel = new JLabel("Username : ");

        userLabel.setFont(new Font("Tahoma", Font.BOLD, 20));
        GridBagConstraints userLabelGrid = new GridBagConstraints();
        userLabelGrid.anchor = GridBagConstraints.EAST;
        userLabelGrid.insets = new Insets(5, 5, 5, 5);
        userLabelGrid.gridx = 1;
        userLabelGrid.gridy = 1;

        mainPanel.add(userLabel, userLabelGrid);
        JLabel passLabel = new JLabel("Password : ");

        passLabel.setFont(
                        new Font("Tahoma", Font.BOLD, 20));
        GridBagConstraints passLabelGrid = new GridBagConstraints();
        passLabelGrid.anchor = GridBagConstraints.EAST;
        passLabelGrid.insets = new Insets(5, 5, 5, 5);
        passLabelGrid.gridx = 1;
        passLabelGrid.gridy = 2;

        mainPanel.add(passLabel, passLabelGrid);
        JTextField userField = new JTextField();

        userField.setFont(UIManager.getFont("TextField.font"));
        GridBagConstraints userFieldGrid = new GridBagConstraints();
        userFieldGrid.fill = GridBagConstraints.VERTICAL;
        userFieldGrid.insets = new Insets(5, 5, 0, 0);
        userFieldGrid.anchor = GridBagConstraints.WEST;
        userFieldGrid.gridx = 2;
        userFieldGrid.gridy = 1;

        userField.setColumns(
                        30);
        mainPanel.add(userField, userFieldGrid);
        JTextField passField = new JTextField();

        passField.setFont(UIManager.getFont("PasswordField.font"));
        GridBagConstraints passFieldGrid = new GridBagConstraints();
        passFieldGrid.fill = GridBagConstraints.VERTICAL;
        passFieldGrid.insets = new Insets(5, 5, 0, 0);
        passFieldGrid.anchor = GridBagConstraints.WEST;
        passFieldGrid.gridx = 2;
        passFieldGrid.gridy = 2;

        passField.setColumns(30);
        mainPanel.add(passField, passFieldGrid);
        JButton okButton = new JButton();

        JPanel buttonPane = new JPanel(new GridBagLayout());

        GridBagConstraints okButtonGrid = new GridBagConstraints();
        okButtonGrid.insets = new Insets(5, 5, 0, 0);
        okButtonGrid.gridx = 0;
        okButtonGrid.gridy = 0;

        okButton.setText("OK");
        buttonPane.add(okButton, okButtonGrid);

        JButton cancelButton = new JButton();
        GridBagConstraints cancelButtonGrid = new GridBagConstraints();
        cancelButtonGrid.insets = new Insets(5, 5, 0, 5);
        cancelButtonGrid.gridx = 1;
        cancelButtonGrid.gridy = 0;

        cancelButton.setText("CANCEL");
        buttonPane.add(cancelButton, cancelButtonGrid);

        add(buttonPane, BorderLayout.SOUTH);

    }
}

ps

“查看 OK 按钮”没有任何意义...按钮的布局与您所说的完全一样...

于 2013-04-27T21:25:51.037 回答
0
 cancelButtonGrid.fill = GridBagConstraints.BOTH; 

GridBagConstraints.BOTH将水平和垂直拉伸组件以填充其单元格的整个宽度和整个高度

于 2013-04-27T21:11:39.050 回答