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

时间:2013-03-07 19:07:06

标签: java swing layout jframe layout-manager

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

}

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:0)

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

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

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

For more information on the GridBagLayout click this link