基本前提将遵循将任何其他组件添加到面板的相同工作流程......
你可以做类似...
public JPanel createMasterPane() {
JPanel master = new JPanel(new BorderLayout());
JPanel leftPane = new JPanel();
leftPane.add(new JLabel("Left"));
master.add(leftPane, BorderLayout.WEST);
JPanel rightPanel = new JPanel();
rightPanel.add(new JLabel("Right"));
master.add(rightPanel, BorderLayout.EAST);
return master;
}
更好的解决方案(恕我直言)是将各个责任领域分开并做更多类似的事情......
public JPanel createLeftPane() {
JPanel leftPane = new JPanel();
leftPane.add(new JLabel("Left"));
return leftPane;
}
public JPanel createRightPane() {
JPanel rightPanel = new JPanel();
rightPanel.add(new JLabel("Right"));
return rightPanel;
}
public JPanel createMasterPane() {
JPanel master = new JPanel(new BorderLayout());
master.add(createLeftPane(), BorderLayout.WEST);
master.add(createRightPane(), BorderLayout.EAST);
return master;
}
您可能想花一些时间查看如何使用 Swing 创建 UI 以了解更多详细信息...