Java GridBagLayout-JFormattedTextFields太小?
我正在SWING中编写gui:Java GridBagLayout-JFormattedTextFields太小?,java,eclipse,swing,user-interface,gridbaglayout,Java,Eclipse,Swing,User Interface,Gridbaglayout,我正在SWING中编写gui: JLabel tLabel1 = new JLabel("Name: "); JFormattedTextField tTextField1 = new JFormattedTextField(); textBoxes.add(tTextField1); JLabel tLabel2 = new JLabel("Maximaler Preis: "); JFormattedTextField tTextField2 = ne
JLabel tLabel1 = new JLabel("Name: ");
JFormattedTextField tTextField1 = new JFormattedTextField();
textBoxes.add(tTextField1);
JLabel tLabel2 = new JLabel("Maximaler Preis: ");
JFormattedTextField tTextField2 = new JFormattedTextField();
textBoxes.add(tTextField2);
GridBagLayout tLayout = new GridBagLayout();
mainFrame.getContentPane().setLayout(tLayout);
// tLayout).setAutoCreateGaps(true);
// tLayout.setAutoCreateContainerGaps(true);
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.anchor = GridBagConstraints.WEST;
mainFrame.add(new JLabel("Geben sie bitte die Kriterien für die Suche an:"), gbc);
gbc.gridwidth = 1;
gbc.gridy++;
mainFrame.add(tLabel1, gbc);
gbc.gridy++;
mainFrame.add(tLabel2, gbc);
gbc.gridy++;
mainFrame.add(tLabel3, gbc);
gbc.gridy++;
mainFrame.add(tLabel4, gbc);
gbc.gridx++;
gbc.gridy = 1;
mainFrame.add(tTextField1, gbc);
gbc.gridy++;
mainFrame.add(tTextField2, gbc);
gbc.gridy++;
mainFrame.add(tTextField3, gbc);
gbc.gridy++;
mainFrame.add(tCombo, gbc);
gbc.gridy++;
mainFrame.add(searchButton, gbc);
gbc.gridy++;
gbc.gridx = 0;
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.fill = GridBagConstraints.HORIZONTAL;
mainFrame.add(tTable, gbc);
mainFrame.setSize(800, 500);
mainFrame.pack();
我的文本字段如下所示:
|
如何在GridBagLayout中为它们指定宽度?请参阅 这里有几个选项: 设置JFormattedTextField的columns属性这将最终影响textfield的首选大小 通过以下方式更改GridBagConstraint 将weightx属性设置为大于0的值 将“填充”属性设置为“水平”
您需要在约束中为它们指定权重。权重决定剩余空间的分布方式,如果您不在任何组件上设置权重,则它们都将获得其最小尺寸,而剩余空间将完全超出布局。这将为字段提供固定宽度-它们不会随着容器大小的调整而拉伸和收缩。如果这对你很重要,那么你也应该设置权重。@Ian优秀点Ian。我喜欢根据组件本身的“智能”来建议大小,但大小最终取决于布局,而备受诟病的GBL是一个功能强大、适应性强的布局管理器,在计算字段大小时可以将首选大小作为提示。提供合适的GridBagConstraints是向GBL描述字段或控件应如何理想地显示、定位和扩展/收缩可用空间的方式。+1用于涵盖组件、布局和可用空间组合的另一个重要方面。