[英]Java JFrame horizontal resize of JTextField, JComboBox
调整窗口大小时,按钮保持相同大小。 更改JFrame窗口大小时如何进行水平调整大小?
我希望userTextArea和typeList的水平大小根据用户是否增加或减小窗口区域来扩展和收缩。
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
(默认值), HORIZONTAL
(使组件足够宽以水平填充其显示区域,但不改变其高度), VERTICAL
(使组件足够高以垂直填充其显示区域) ,但不改变它的宽度),和BOTH
(使组件完全填满其显示区域)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.