【问题标题】:Java JFrame horizontal resize of JTextField, JComboBoxJTextField,JComboBox的Java JFrame水平调整大小
【发布时间】:2013-02-23 03:36:53
【问题描述】:

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

}

【问题讨论】:

  • 解决这个问题的第一步是摆脱像setLayout(null);这样的废话。 使用布局!有些布局会尊重组件的首选大小,而其他布局会将它们拉伸到可用空间。
  • 如果您只想扩展每行的第二个组件,那么您应该能够使用 SpringLayout 或 GridBagLayout。请参阅上面链接中的示例。

标签: java swing layout jframe layout-manager


【解决方案1】:

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

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

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

For more information on the GridBagLayout click this link

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-14
    • 1970-01-01
    • 1970-01-01
    • 2012-07-19
    • 1970-01-01
    相关资源
    最近更新 更多