2017-05-29 4 views
0

フォームにGBレイアウトを使用しています。現在、2つの行があり、それぞれ6つの要素で構成されています(最初の行にはラベル、2番目のテキストフィールドがあります)。私の問題は、X 5 Y 1に表示されるべき要素がX 6 Y 0に表示されることです。私が作っている間違いを教えてください。Java Gridbag missalign - 最初の行に表示される2行目の最後のセル

あなたはおよそ GridBagConstraintsオブジェクト con.add(txtPresence);を忘れてしまったあなたの最後のコンポーネントを追加する

アドオン(コンポーネント部品、オブジェクト制約)

を呼び出す必要がGridBagLayoutを使用し

public GUI() { 
    super("Bla"); 
    GridBagLayout layout = new GridBagLayout(); 
    setLayout(layout); 
    GridBagConstraints gbc = new GridBagConstraints();  
    [enter image description here][1]gbc.insets = new Insets(10,10,10,10); 
    Container con = getContentPane(); 

    panel1 = new JPanel(); 

    labBrawn = new JLabel("Brawn"); 
    gbc.gridx = 0; 
    gbc.gridy = 0; 
    con.add(labBrawn, gbc); 

    labAgility = new JLabel("Agility"); 
    gbc.gridx = 1; 
    gbc.gridy = 0; 
    con.add(labAgility, gbc); 

    labIntellect = new JLabel("Intellect"); 
    gbc.gridx = 2; 
    gbc.gridy = 0; 
    con.add(labIntellect, gbc); 

    labCunning = new JLabel("Cunning"); 
    gbc.gridx = 3; 
    gbc.gridy = 0; 
    con.add(labCunning, gbc); 

    labWillpower = new JLabel("Willpower"); 
    gbc.gridx = 4; 
    gbc.gridy = 0; 
    con.add(labWillpower, gbc); 

    labPresence = new JLabel("Presence"); 
    gbc.gridx = 5; 
    gbc.gridy = 0; 
    con.add(labPresence, gbc); 



    txtBrawn = new JTextField("", 2); 
    gbc.gridx = 0; 
    gbc.gridy = 1; 
    con.add(txtBrawn, gbc); 

    txtAgility = new JTextField("", 2); 
    gbc.gridx = 1; 
    gbc.gridy = 1; 
    con.add(txtAgility, gbc); 

    txtIntellect = new JTextField("", 2); 
    gbc.gridx = 2; 
    gbc.gridy = 1; 
    con.add(txtIntellect, gbc); 

    txtCunning = new JTextField("", 2); 
    gbc.gridx = 3; 
    gbc.gridy = 1; 
    con.add(txtCunning, gbc); 

    txtWillpower = new JTextField("", 2); 
    gbc.gridx = 4; 
    gbc.gridy = 1; 
    con.add(txtWillpower, gbc); 

    txtPresence = new JTextField("", 2); 
    gbc.gridx = 5; 
    gbc.gridy = 1; 
    con.add(txtPresence); 


} 

答えて

1

。それがなければ、コンポーネントは最初の行の最後のコンポーネントの後に置かれます。最初の空き領域は制約されていないと見なされます(Y = 0にして、次の利用可能なXに移動します)。 JPanelGridBagLayoutのコンポーネントを追加する場合は、GridBagConstraintsオブジェクトを使用せずに、1つの行に入れます。

あなたのGridBagConstraintsオブジェクトにweights,fillanchorを設定することを忘れないでください。

私のGridBagManagerレポをgithubで見ることもできます。どのように動作するかを示すデモが含まれています。あなたはまだどのようにGridBagLayoutが動作するか理解する必要があります。

SSCCE:(あなたのコードのクイックフィックス)

public class GUI extends JFrame { 

    public static void main(String[] args) { 
     GUI gui = new GUI(); 
     gui.setSize(600, 600); 
     gui.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 

     SwingUtilities.invokeLater(() -> { 
      gui.setVisible(true); 
     }); 
    } 

    public GUI() { 
     super(); 

     //those should be initialized outside 
     JLabel labBrawn = new JLabel("Brawn"); 
     JLabel labAgility = new JLabel("Agility"); 
     JLabel labIntellect = new JLabel("Intellect"); 
     JLabel labCunning = new JLabel("Cunning"); 
     JLabel labWillpower = new JLabel("Willpower"); 
     JLabel labPresence = new JLabel("Presence"); 

     JTextField txtBrawn = new JTextField("", 2); 
     JTextField txtAgility = new JTextField("", 2); 
     JTextField txtIntellect = new JTextField("", 2); 
     JTextField txtCunning = new JTextField("", 2); 
     JTextField txtWillpower = new JTextField("", 2); 
     JTextField txtPresence = new JTextField("", 2); 
     //-------------------------------------------- 

     //here goes actual GUI 
     GridBagLayout gridBagLayout = new GridBagLayout(); 
     GridBagConstraints gbc = new GridBagConstraints(); 
     gbc.insets = new Insets(10, 10, 10, 10); 
     gbc.anchor = GridBagConstraints.NORTH; 
     gbc.fill = GridBagConstraints.BOTH; 
     gbc.gridwidth = 1; 
     gbc.gridheight = 1; 

     JPanel dataInputPanel = new JPanel(); 
     dataInputPanel.setLayout(gridBagLayout); 

     gbc.weightx = 1; 
     gbc.weighty = 0; 

     gbc.gridx = 0; 
     gbc.gridy = 0; 
     dataInputPanel.add(labBrawn, gbc); 

     gbc.gridx = 1; 
     gbc.gridy = 0; 
     dataInputPanel.add(labAgility, gbc); 

     gbc.gridx = 2; 
     gbc.gridy = 0; 
     dataInputPanel.add(labIntellect, gbc); 

     gbc.gridx = 3; 
     gbc.gridy = 0; 
     dataInputPanel.add(labCunning, gbc); 

     gbc.gridx = 4; 
     gbc.gridy = 0; 
     dataInputPanel.add(labWillpower, gbc); 

     gbc.gridx = 5; 
     gbc.gridy = 0; 
     dataInputPanel.add(labPresence, gbc); 

     gbc.gridx = 0; 
     gbc.gridy = 1; 
     dataInputPanel.add(txtBrawn, gbc); 

     gbc.gridx = 1; 
     gbc.gridy = 1; 
     dataInputPanel.add(txtAgility, gbc); 

     gbc.gridx = 2; 
     gbc.gridy = 1; 
     dataInputPanel.add(txtIntellect, gbc); 

     gbc.gridx = 3; 
     gbc.gridy = 1; 
     dataInputPanel.add(txtCunning, gbc); 

     gbc.gridx = 4; 
     gbc.gridy = 1; 
     dataInputPanel.add(txtWillpower, gbc); 

     gbc.gridx = 5; 
     gbc.gridy = 1; 
     gbc.weighty = 1; 
     dataInputPanel.add(txtPresence, gbc); 

     dataInputPanel.setBackground(Color.red); 
     this.add(dataInputPanel, BorderLayout.NORTH); 
    } 
} 
+0

オーマイ...おかげで多く、そのような質問を悩ませて申し訳ありません。 – Faire

+0

@ファイヤーうん、問題ありません。いつでも私に起こります。だから私はGridBagManagerを作成し、抽象パネルには 'add()'メソッドがあり、このクラスのフィールドである 'add(comp、gbc)'、 'GridBagConstraints gbc'を呼び出します。そのような単純な。それを見て、幸せなコーディング! – bigby

関連する問題