我正在为我的应用程序使用GridBagLayout,因为我认为设置组件的位置更容易。但我有宽度的问题。查看OK按钮
和代码:
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);
}
我错过了什么?请帮助,提前致谢:)
答案 0 :(得分:2)
您可以删除两个按钮的.fill = GridBagConstraints.BOTH
,这会阻止按钮扩展以填充单元格的整个宽度(和高度)...
我还会看一下您的GridPanel.columnWeights
和GridPanel.rowWeights
,以确保它们符合您的需求......
由于GridBagLayout
的性质,你实际上会更好地使用一系列面板来单独布局各个元素(字段和按钮),这样可以更好地控制布局... < / p>
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 强>
“看看确定按钮”没有任何意义......按钮的布局完全符合你的要求......
答案 1 :(得分:0)
cancelButtonGrid.fill = GridBagConstraints.BOTH;
GridBagConstraints.BOTH将水平和垂直拉伸组件以填充整个宽度及其单元格的整个高度