我不知道如何创建这样的面板。我可以将主面板作为边框布局,并将登录屏幕面板设置为 page_end,但论坛和常见问题解答也必须位于 page_end 上......不知何故,登录屏幕面板以及论坛和常见问题解答面板必须共享page_end 在一起。有什么办法我可以做到这一点或者也许有更好的方法吗?这让我困惑了大约两个小时,我不明白我该怎么做。
现在我有 3 个面板和 1 个框架。 1是添加到主框架的主面板。另外两个面板是登录屏幕面板以及论坛和常见问题解答面板。这是代码。
private void createView() {
//Created essential details for the frame
JFrame frame = new JFrame();
frame.setTitle("Name of the game");
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Defining layouts and panels + giving them layouts
JPanel pMain = new JPanel();
frame.getContentPane().add(pMain);
pMain.setLayout(new BorderLayout());
JPanel pLogin = new JPanel(new GridBagLayout());
pMain.add(pLogin, BorderLayout.PAGE_END);
JPanel pInfo = new JPanel(new GridBagLayout());
pMain.add(pInfo, BorderLayout.PAGE_END);
frame.setVisible(true);
}
最佳答案
这是组件布局
来源
JFrame frame = new JFrame();
frame.setTitle("Name of the game");
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Defining layouts and panels + giving them layouts
JPanel pMain = new JPanel();
frame.getContentPane().add(pMain);
pMain.setLayout(new BorderLayout());
JPanel bottomComponentsPanel = new JPanel(new GridBagLayout());
JPanel pLogin = new JPanel();
pLogin.setBackground(Color.ORANGE);
pLogin.setPreferredSize(new Dimension(100, 100));
JPanel pInfo = new JPanel(new GridBagLayout());
pInfo.setBackground(Color.ORANGE);
pInfo.setPreferredSize(new Dimension(70, 70));
GridBagConstraints constraints = new GridBagConstraints();
constraints.anchor = GridBagConstraints.PAGE_END;
constraints.gridx = 0;
constraints.gridy = 0;
bottomComponentsPanel.add(pLogin, constraints);
constraints.gridx = 1;
constraints.gridy = 0;
bottomComponentsPanel.add(pInfo, constraints);
JPanel bottomPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
bottomPanel.add(bottomComponentsPanel);
pMain.add(bottomPanel, BorderLayout.SOUTH);
frame.setVisible(true);
显示
关于java - JFrame 布局多个 JPanel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32926275/