我正在为我的应用程序使用 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);
}
我错过了什么??请帮助,在此先感谢:)