Java JFrame调整JTextField、JComboBox的水平大小
调整窗口大小时,按钮的大小保持不变。更改JFrame窗口大小时如何进行水平调整 我希望userTextArea和typeList的水平大小根据用户增加或减少窗口面积而扩展和收缩Java JFrame调整JTextField、JComboBox的水平大小,java,swing,layout,jframe,layout-manager,Java,Swing,Layout,Jframe,Layout Manager,调整窗口大小时,按钮的大小保持不变。更改JFrame窗口大小时如何进行水平调整 我希望userTextArea和typeList的水平大小根据用户增加或减少窗口面积而扩展和收缩 public ClearQuestWindow(String title){ protected JTextField userTextArea; protected JLabel userLabel; protected JLabel typeLabel; protected JComboBox typeLis
public ClearQuestWindow(String title){
protected JTextField userTextArea;
protected JLabel userLabel;
protected JLabel typeLabel;
protected JComboBox typeList;
super(title);
setLayout(null);
setMinimumSize(new Dimension(790, 625));
// Set to system look and feel
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch(Exception e) {
System.out.println("Error setting native LAF: " + e);
}
//Text field instantiation
userTextArea = new JTextField();
//Labels instantiation
userLabel = new JLabel("User Name:");
typeLabel = new JLabel("Type:");
//ComboBox instantiation
typeList = new JComboBox(typeString);
userLabel.setSize(100, 30);
userLabel.setLocation(10, 80 );
userLabel.setFont(new Font("Times New Roman", Font.PLAIN, 18));
userTextArea.setLocation(100, 85);
userTextArea.setSize(150, 23);
typeLabel.setSize(100, 30);
typeLabel.setLocation(10, 110 );
typeLabel.setForeground(Color.red);
typeLabel.setFont(new Font("Times New Roman", Font.PLAIN, 18));
typeList.setLocation(100, 115);
typeList.setSize(150, 23);
}
GridBagLayout
是一个灵活的布局管理器,应该适合您的用途:
mainFrame.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
mainFrame.add(label, c);
当组件的显示区域大于组件请求的大小时,使用Fill
,以确定是否以及如何调整组件的大小。有效值(定义为GridBagConstraints
常量)包括NONE
(默认值)、水平
(使组件足够宽以水平填充其显示区域,但不改变其高度)、垂直
(使组件足够高,以垂直填充其显示区域,但不改变其宽度),和两者都(使组件完全填充其显示区域)
解决这个问题的第一步是摆脱像setLayout(null)这样的无意义的东西;
。使用布局!有些布局会考虑组件的首选大小,而其他布局会将它们拉伸到可用空间。请查看。如果您只希望每行上的第二个组件展开,则应该可以使用SpringLayout或GridBagLayout。请参阅上面链接中的示例。