繁体   English   中英

BorderLayout不会在容器中对齐JPanel

[英]BorderLayout won't align JPanel in the Container

我想将两个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);
}
}

我并没有添加所有组件来减少代码,但是在上面您可以看到Screenshot示例。

您尚未为JPanel指定一个名为content的布局。 JPanel的默认布局是FlowLayout,这就是为什么要从左向右添加组件的原因。

尝试指定:

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

试试这个代码:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM