0

当我调整窗口大小时,按钮保持相同大小。更改 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);

}

在此处输入图像描述

在此处输入图像描述

4

1 回答 1

0

GridBagLayout是一个灵活的布局管理器,应该适合您的目的:

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

Fill当组件的显示区域大于组件请求的大小时使用,以确定是否以及如何调整组件的大小。有效值(定义为GridBagConstraints常量)包括NONE(默认),HORIZONTAL(使组件足够宽以水平填充其显示区域,但不改变其高度),VERTICAL(使组件足够高以垂直填充其显示区域,但不要改变它的宽度)和BOTH(使组件完全填满它的显示区域)。

有关 GridBagLayout 的更多信息,请单击此链接

于 2016-12-08T09:54:37.550 回答