2016-12-19 4 views
1

私は2つのJPanelをNorthとCenterに揃えたいと考えましたが、パネルは上から下に追加するのではなく、左から右に追加していきます。理由は何ですか?BorderLayoutはコンテナ内にJPanelを配置しません

SC

public class EmployeeFrameView extends JInternalFrame 
{ 

    static final int xOffset = 30, yOffset = 30; 

    JPanel panelEmployee; 
    JPanel panelEmergency;   

    public EmployeeFrameView() 
    { 
     super("AddEmployee",true,true,true,true); 
     addComponentsToPane(getContentPane()); 
     pack(); 
     setVisible(true); 
     setLocation(xOffset,yOffset); 
    } 

    private JPanel addComponentsToEmployeePanel(JPanel panelEmployee) 
    { 
    panelEmployee.setLayout(grid); 
    panelEmployee.setBorder(BorderFactory.createTitledBorder("Personal Information")); 

    gbc.anchor = GridBagConstraints.FIRST_LINE_START; 
    gbc.gridx = 0; 
    gbc.gridy = 1; 
    gbc.weightx = 0.5; 
    lblLastName = new JLabel("Last Name:"); 
    panelEmployee.add(lblLastName,gbc); 

    gbc.gridx = 1; 
    gbc.gridy = 1; 
    tfLastName = new JTextField(10); 
    panelEmployee.add(tfLastName,gbc); 

    } 
    private JPanel addComponentsToEmergencyPanel(JPanel panelEmergency) 
{ 
    panelEmergency.setLayout(grid); 

    panelEmergency.setOpaque(true); 
    panelEmergency.setBorder(BorderFactory.createTitledBorder("Emergency Details")); 

    gbc.gridx = 0; 
    gbc.gridy = 1; 
    lblGuardianContactName = new JLabel("Contact Name:"); 
    panelEmergency.add(lblGuardianContactName, gbc); 

    return panelEmergency; 
} 

public void addComponentsToPane(final Container pane) 
{ 
    final JPanel content = new JPanel(); 
    panelEmployee = new JPanel(); 
    panelEmergency = new JPanel(); 

    //Add to content and set layout 
    content.add(addComponentsToEmployeePanel(panelEmployee),BorderLayout.NORTH); 
    content.add(addComponentsToEmergencyPanel(panelEmergency), BorderLayout.CENTER); 

    //Adding ScrollPane to Container. 
    final JScrollPane scrollPane = new JScrollPane(content, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); 
    pane.add(scrollPane); 
} 
} 

私は、コードを減らすために、すべてのコンポーネントを追加しますが、あなたはスクリーンショットのサンプルを見ることができる上記ませんでした。

+1

'BorderLayout'がJPanel''のデフォルトのレイアウトマネージャではありません、 'content.setLayout(新しいのBorderLayout())を追加します。'。 – Berger

+0

@Berger私は混乱してしまいました。これは問題を解決します。 – Francisunoxx

答えて

1

は、このコードを試してみてください。

//constructor 
public EmployeeFrameView() 
    { 
     super("AddEmployee",true,true,true,true); 
     getContentPane.setLayout(new BorderLayout()); 
     addComponentsToPane(getContentPane()); 
     pack(); 
     setVisible(true); 
     setLocation(xOffset,yOffset); 
    } 

//method 
public void addComponentsToPane(Container pane) { 
    JPanel content = new JPanel(); 
    content.setLayout(new BorderLayout()); 
    panelEmployee = new JPanel(); 
    panelEmergency = new JPanel(); 

    content.add(addComponentsToEmployeePanel(panelEmployee), BorderLayout.NORTH); 
    content.add(addComponentsToEmergencyPanel(panelEmergency), BorderLayout.CENTER); 
    JScrollPane scrollPane = new JScrollPane(content, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, 
      JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); 

    pane.add(scrollPane); 
} 
+0

それは私に同じ意見を与えてくれます。 – Francisunoxx

+0

本当にあなたのコンテンツペインのレイアウトを設定していませんでしたか? – XtremeBaumer

+0

これはすでに私の問題を解決しています。 Btwあなたの提案に感謝:D – Francisunoxx

2

あなたは、コンテンツと呼ばれるあなたのJPanelにレイアウトを指定していません。 JPanelのデフォルトのレイアウトはFlowLayoutです。そのため、コンポーネントが左から右に追加されます。

ことを指定しよう:

final JPanel content = new JPanel(new BorderLayout()); 
関連する問題