2013-06-14 11 views
7

コンポーネントを整列しようとすると、左側または右側のいずれかになります。GridBagLayoutを使用してJPanelのコンポーネント中心を揃える方法は?

この問題を解決して、パネルのサイズを400 x 350ピクセルに設定する方法を教えてください。ここで

enter image description here

私のコードです.... titleLabelResultLabelを中心に

public TimeGui() { 

    layout = new GridBagLayout(); 
    setSize(400, 350); //**Its not working** 
    setBackground(Color.LIGHT_GRAY); 
    setBorder(BorderFactory.createLineBorder(Color.BLACK)); 
    setBorder(new TitledBorder(new EtchedBorder(), "Time Conversion")); 

    setLayout(layout); 
    layoutConstraints = new GridBagConstraints();  
    textField1 = new JTextField(10); 
    textField2 = new JTextField(10); 

    String[] names1 = {"Seconds", "Minutes", "Hours", "Days", "Weeks"}; 


    comboBox1 = new JComboBox<>(names1); 
    comboBox2 = new JComboBox<>(names1); 

    titleLabel = new JLabel("Time Conversion Unit", JLabel.CENTER); 
    resultLabel = new JLabel("Result Label"); 
    equalLabel = new JLabel("="); 

    convertButton = new JButton("Convert"); 


    layoutConstraints.fill = GridBagConstraints.HORIZONTAL; 
    Insets inset = new Insets(10, 10, 10, 10); 
    layoutConstraints.anchor = GridBagConstraints.CENTER; 

    addComponent(titleLabel, 0, 0, 2, 2, inset); // I tried (0,1,2,2) 



    addComponent(comboBox1, 3, 0, 2, 3, inset); 

    addComponent(comboBox2, 3, 2, 2, 3, inset); 

    addComponent(textField1, 6, 0, 1, 2, inset); 

    addComponent(equalLabel, 6, 1, 1, 2, inset); 

    addComponent(textField2, 6, 2, 1, 2, inset); 

    addComponent(resultLabel, 8, 1, 2, 1, inset); 

    addComponent(convertButton, 10, 0, 2, 2, inset); 

} 

private void addComponent(Component component, int row, 
     int column, int width, int height, Insets inset1) { 
    layoutConstraints.gridx = column; 
    layoutConstraints.gridy = row; 
    layoutConstraints.gridwidth = width; 
    layoutConstraints.gridheight = height; 
    layoutConstraints.insets = inset1; 
    layout.setConstraints(component, layoutConstraints); 
    add(component); 
} 
} 
+0

イメージがない場合は、少なくとも1つの[___SSCCE___](http://sscce.org/)を提供できれば賢明です。 [** imgur **](http://imgur.com/)、[** dropbox **](https://www.dropbox.com/home)、[*]などのサイトに画像をアップロードするだけです。 * 4shared **](http://www.4shared.com/)。より高い特権を持っている人はあなたの質問にそれを追加します。 –

+0

okパネルのサイズを設定する方法を教えてください。 – user1874936

答えて

4

を整列しなければならない問題は、あなたとあなたのgridwidthfillプロパティである...

enter image description hereenter image description here

基本的に私が変更したのは...

addComponent(titleLabel, 0, 0, GridBagConstraints.REMAINDER, 2, inset); // I tried (0,1,2,2) 
addComponent(comboBox1, 3, 0, 1, 3, inset); 
addComponent(comboBox2, 3, 2, 1, 3, inset); 
addComponent(textField1, 6, 0, 1, 2, inset); 
addComponent(equalLabel, 6, 1, 1, 2, inset); 
addComponent(textField2, 6, 2, 1, 2, inset); 
layoutConstraints.fill = GridBagConstraints.NONE; 
addComponent(resultLabel, 8, 0, GridBagConstraints.REMAINDER, 1, inset); 
addComponent(convertButton, 10, 0, GridBagConstraints.REMAINDER, 2, inset); 

他のいくつかを試してみることができます。パネルの実際のサイズを定義するために、あなたができる最善のTimeGuigetPreferredSizeメソッドをオーバーライドすることであるとして

...

@Override 
public Dimension getPreferredSize() { 
    return new Dimension(400, 350); 
} 

どのようなサイズあなたの親コンテナに「提案」しますレイアウトされたいと思っています。ちょっと覚えておいてください、これはオプションの値であり、レイアウト管理者はそれを無視する権限があります。

+0

偉大な... thanx多く – user1874936

+0

コンポーネントのサイズをincreseする方法。だからそれは明確に見える – user1874936

+0

それはそれを表示するために使用しているレイアウトマネージャによって異なります。私の答えの下に記載されているように 'getPreferredSize'をオーバーライドすることから始めてください。 – MadProgrammer

関連する問題