简体   繁体   English

JTextField,JComboBox的Java JFrame水平调整大小

[英]Java JFrame horizontal resize of JTextField, JComboBox

When I resize a window, the buttons stay the same size. 调整窗口大小时,按钮保持相同大小。 How can I do a horizontal resizing when I change the JFrame window size? 更改JFrame窗口大小时如何进行水平调整大小?

I want the horizontal size of userTextArea and typeList to expand and contract depending o if the user increases or decreases the window area. 我希望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 is a flexible Layout Manager and should suit your purpose: GridBagLayout是一种灵活的布局管理器,应适合您的目的:

mainFrame.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
mainFrame.add(label, c);

Use Fill when the component's display area is larger than the component's requested size to determine whether and how to resize the component. 当组件的显示区域大于组件的请求大小时,请使用“ Fill来确定是否以及如何调整组件的大小。 Valid values (defined as GridBagConstraints constants) include NONE (the default), HORIZONTAL (make the component wide enough to fill its display area horizontally, but do not change its height), VERTICAL (make the component tall enough to fill its display area vertically, but do not change its width), and BOTH (make the component fill its display area entirely). 有效值(定义为GridBagConstraints常数)包括NONE (默认值), HORIZONTAL (使组件足够宽以水平填充其显示区域,但不改变其高度), VERTICAL (使组件足够高以垂直填充其显示区域) ,但不改变它的宽度),和BOTH (使组件完全填满其显示区域)。

For more information on the GridBagLayout click this link 有关GridBagLayout的更多信息,请单击此链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM