2016-06-17 4 views
1

私はJavaのスイングにはまだまだ慣れていないし、そのトリックを学ぼうとしていますが、フレーム内の2つのJPanelのアライメントには苦労しています。下のスクリーンショットを見ると、私の列名JPanelの配置とその下のJPanelの配置が正しく整列していません。2つのJPanelを垂直に並べる方法は?

以下は私のコードです。

JPanel columns = new JPanel(new GridBagLayout()); 
    GridBagConstraints gbc = new GridBagConstraints(); 
    gbc.weighty = 1.0; 
    gbc.weightx = 1.0; 
    gbc.fill = GridBagConstraints.VERTICAL; 
    gbc.insets = new Insets(5, 5, 5, 5); 
    gbc.anchor = GridBagConstraints.VERTICAL; 


    text = "<html><b>Action</b></html>";   
    JLabel label_action = new JLabel(text); 
    columns.add(label_action, gbc); 
    columns.setLayout(new GridBagLayout()); 


    JPanel values= new JPanel(new GridBagLayout()); 
    values.setBorder(BorderFactory.createRaisedBevelBorder()); 

    gbc.weighty = 1.0; 
    gbc.weightx = 1.0; 
    gbc.fill = GridBagConstraints.VERTICAL; 
    gbc.insets = new Insets(5, 5, 5, 5); 
    gbc.anchor = GridBagConstraints.VERTICAL; 

    DefaultComboBoxModel<String> model = new DefaultComboBoxModel<String>(); 

    model.addElement("click"); 
    model.addElement("Input Keys"); 

    final JComboBox<String> comboBox1 = new JComboBox<String>(model); 
    AutoCompleteDecorator.decorate(comboBox1); 
    comboBox1.setPreferredSize(new Dimension(200, 22)); 
    values.add(comboBox1, gbc); 
    values.setLayout(new GridBagLayout()); 

enter image description here

+1

setLayout' 'に対するすべての呼び出しを削除します。すでにJPanelをレイアウト(具体的にはGridBagLayout)で作成しています。新しいレイアウトを設定すると、以前に指定したすべての制約が破棄されます。 – VGR

+0

両方のJPanelからsetLayoutを削除すると、コンパイルエラーが発生します。スレッド「AWT-EventQueue-0」の例外java.lang.IllegalArgumentException:不正なアンカー値 – Naseem

+1

グリッドの行ゼロのタイトルを持つ単一のJPanelを使用する必要があります。 – FredK

答えて

2

でもGridBagLayoutで動作するように気にしないでください。今日の要件を満たしていない、うまく設計されていないレイアウトマネージャです。 MigLayoutまたはGroupLayoutのいずれかをお勧めします。 MigLayoutであなたのレイアウトを作成することがいかに簡単であるか

ルック:あなたのUIを作成する超簡単です

package com.zetcode; 

import javax.swing.JComboBox; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JTextField; 
import javax.swing.SwingUtilities; 
import net.miginfocom.swing.MigLayout; 


public class MigLayoutAlignmentEx extends JFrame { 

    public MigLayoutAlignmentEx() { 

     initUI(); 
    } 

    private void initUI() { 

     setLayout(new MigLayout()); 

     JLabel lbl1 = new JLabel("Action"); 
     JLabel lbl2 = new JLabel("Choose Selector"); 
     JLabel lbl3 = new JLabel("Element Properties"); 
     JLabel lbl4 = new JLabel("Values"); 
     JLabel lbl5 = new JLabel("Element Name"); 
     JLabel lbl6 = new JLabel("Comments"); 

     JComboBox combo1 = new JComboBox(); 
     JComboBox combo2 = new JComboBox(); 

     JTextField field1 = new JTextField(15); 
     JTextField field2 = new JTextField(15); 
     JTextField field3 = new JTextField(15); 
     JTextField field4 = new JTextField(15); 

     add(lbl1); 
     add(lbl2); 
     add(lbl3); 
     add(lbl4); 
     add(lbl5); 
     add(lbl6, "wrap"); 

     add(combo1, "w 150lp"); 
     add(combo2, "w 150lp"); 
     add(field1); 
     add(field2); 
     add(field3); 
     add(field4, "wrap"); 

     pack(); 

     setTitle("MigLayout example"); 
     setLocationRelativeTo(null); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
    } 


    public static void main(String[] args) { 

     SwingUtilities.invokeLater(() -> { 
      MigLayoutAlignmentEx ex = new MigLayoutAlignmentEx(); 
      ex.setVisible(true); 
     });   
    } 
} 

。コンポーネントをセルに配置するだけです。ただし、グリッドが事前作成されているMigLayoutとは異なり、 GridBagLayoutでは、各セルを手動で作成する必要があります。これは難しく、エラーが発生する可能性があります。

enter image description here

+0

うわー!あなたは 'MigLayout'に夢中ですね。そのレイアウトマネージャーをプログラムしましたか? –

+0

'MigLayout'はMikael Grevによって作成されました。それは使いやすく非常に柔軟です。現代のUIの要件を満たすことができる少数のマネージャの1人。 –

関連する問題