BorderLayout不会在Container中对齐JPanel

时间:2016-12-19 09:31:30

标签: java swing

我想将我的两个JPanel对齐到North和Center,但我的面板不断从左到右添加它,而不是从上到下添加。有什么原因吗?

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);
}
}

我没有添加所有组件来减少代码,但是上面你可以看到截图示例。

2 个答案:

答案 0 :(得分:2)

您没有为JPanel指定名为内容的布局。 JPanel的默认布局是FlowLayout,这就是从左到右添加组件的原因。

尝试指定:

final JPanel content = new JPanel(new BorderLayout());

答案 1 :(得分: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);
}