2013-03-07 113 views
0

當我調整窗口大小,按鈕保持相同的尺寸。當我更改JFrame窗口大小時,如何進行水平調整大小?的Java的JFrame JTextField的水平調整,JComboBox的

我想userTextArea和類型串的水平擴大規模,並根據o如果用戶增加或減少窗口區域的合同。

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

+3

解決這個問題的第一步就是要擺脫像'setLayout的(空)的廢話;'。 ***使用佈局!***有尊重組件的首選尺寸,而其他人將其延伸到可用空間佈局。 – 2013-03-07 19:09:12

+0

看一看[佈局管理器](http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html) – aymeric 2013-03-07 19:10:08

+0

如果你只是想在每一行第二構成元素擴大,那麼你應該能夠使用SpringLayout或GridBagLayout。看到上面鏈接的例子。 – camickr 2013-03-07 19:48:36

回答

0

GridBagLayout是一個靈活的佈局管理器,應該適合你的目的:

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

使用Fill當組件的DISP佈局面積大於組件的請求大小,以確定是否以及如何調整組件大小。有效值(定義爲GridBagConstraints常量)包括NONE(默認值),HORIZONTAL(使足夠組件寬以水平填滿其顯示區域,但不改變它的高度),VERTICAL(使足夠的成分高,能夠垂直地填滿其顯示區域,但不改變它的寬度),和BOTH(使組件完全填滿其顯示區域)。

For more information on the GridBagLayout click this link