2017-02-28 24 views
-1

GridBagLayoutを使用してアイテムを表示するパネルを設計しています。私が探しています結果はこのようなものである:現在、私は見ています、しかしGridBagLayoutの行にすべての項目が表示されない

enter image description here

enter image description here

私はその後、私のパネルを初期化し、レイアウト設定

JPanel surveyDetailsFieldsPane = new JPanel(); 
surveyDetailsFieldsPane.setMinimumSize(new Dimension(0, 300)); 
surveyDetailsFieldsPane.setPreferredSize(new Dimension(410, 300)); 
GridBagLayout detailsGridBagLayout = new GridBagLayout(); 
surveyDetailsFieldsPane.setLayout(detailsGridBagLayout); 
surveyDetailsFieldsPane.setBorder(new EmptyBorder(STANDARD_BORDER, 
      STANDARD_BORDER, 
      STANDARD_BORDER, 
      STANDARD_BORDER)); 

グリッドに追加するコンポーネントを初期化します。

JLabel jobNameLbl.setText("Job Name:"); 
JTextField jobNameTF = new JTextField(20); 

JLabel jobNumLbl.setText("Job Number:"); 
JTextField jobNumTF = new JTextField(12); 

その後、私はaddComponentToSurveyDetailsGridメソッドを使用してグリッドにコンポーネントを追加します。

// Row 1 
addComponentToSurveyDetailsGrid(jobNameLbl, 0, 0, 1, 1, 0, 0, GridBagConstraints.NONE, GridBagConstraints.WEST); 
addComponentToSurveyDetailsGrid(jobNameTF, 1, 0, 1, 1, 0, 0, GridBagConstraints.HORIZONTAL, GridBagConstraints.WEST); 
// Row 2 
addComponentToSurveyDetailsGrid(jobNumLbl, 0, 1, 1, 1, 10, 0, GridBagConstraints.NONE, GridBagConstraints.WEST); 
addComponentToSurveyDetailsGrid(jobNumTF, 1, 1, 0, 1, 40, 0, GridBagConstraints.HORIZONTAL, GridBagConstraints.WEST); 
addComponentToSurveyDetailsGrid(dateLbl, 4, 1, 1, 1, 10, 0, GridBagConstraints.NONE, GridBagConstraints.WEST); 
addComponentToSurveyDetailsGrid(dateTF, 5, 1, 0, 1, 40, 0, GridBagConstraints.HORIZONTAL, GridBagConstraints.WEST); 

使用方法を以下に示します。

private void addComponentToSurveyDetailsGrid(Component component, int gridX, int gridY, 
          int gridWidth, int gridHeight, int weightX, 
          int weightY, int fill, int anchor) { 

    GridBagConstraints constraint = new GridBagConstraints(); 
    constraint.gridx = gridX; 
    constraint.gridy = gridY; 
    constraint.gridwidth = gridWidth; 
    constraint.gridheight = gridHeight; 
    constraint.weightx = weightX; 
    constraint.weighty = weightY;  
    constraint.fill = fill; 
    constraint.anchor = anchor; 
    constraint.insets = new Insets(Dimens.SMALL_BORDER, 
           Dimens.SMALL_BORDER, 
           Dimens.SMALL_BORDER, 
           Dimens.SMALL_BORDER); 
    detailsGridBagLayout.setConstraints(component, constraint); 
    surveyDetailsFieldsPane.add(component); 
} 

私はここで間違って何をしたか教えてください!

+0

gridbaglayoutは非常に難しいと思うので、私はjgoodiesからformlayoutに切り替えました。あなたのグリッドをどのようにして作成したのですか? – XtremeBaumer

+1

1)もう少し早い時期に、[MCVE]または[短く、自己完結型、正しい例](http://www.sscce。 org /)。 2)ASCIIアート、またはGUIの*意図された*レイアウトの簡単な図を最小サイズで提供し、サイズ変更可能な場合は幅と高さを増やします。 –

+0

@AndrewThompson提供されている図面に何が問題なのですか? – petehallw

答えて

3

は、私はそれはあなたの問題を解決しますが疑う:

addComponentToSurveyDetailsGrid(jobNumLbl, 0, 1, 1, 1, 10, 0, ... 
addComponentToSurveyDetailsGrid(jobNumTF, 1, 1, 0, 1, 40, 0, ... 
addComponentToSurveyDetailsGrid(dateLbl, 4, 1, 1, 1, 10, 0, ... 
addComponentToSurveyDetailsGrid(dateTF, 5, 1, 0, 1, 40, 0, ... 

あなたのgridx値は、0、1、4、5

あなただけのグリッド4にコンポーネントを追加しようとすることはできません。 GridBagLayoutはグリッド2と3にコンポーネントを魔法のように追加しません。したがって、グリッド2と3にコンポーネントを追加する必要があります。

ジョブ名テキストフィールドがその下の3つのコンポーネントのスペースを占めるようにする場合は、 jobnameテキストフィールドを追加するときにgridwidth値を3に指定する必要があります。

How to Use GridBagLayoutのSwingチュートリアルのセクションを読んでください。このチュートリアルでは、gridwidthパラメータの動作について説明し、ダウンロードしてテストするための実例を提供します。

関連する問題