JFrame 布局多个 JPanel
JFrame laying out multiple JPanels
我不确定如何创建这样的面板。我可以将主面板设置为边框布局并将登录屏幕面板设置为 page_end 但是论坛和常见问题解答也必须在 page_end 上......不知何故登录屏幕面板和论坛和常见问题小组必须一起分享 page_end。有什么办法可以做到这一点,或者有更好的方法吗?这让我困惑了大约 2 个小时,我不明白我该怎么做。
现在我有 3 个面板和 1 个框架。 1是添加到主框架的主面板。其他 2 个面板是登录屏幕面板以及论坛和常见问题解答面板。这是代码。
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);
显示
我不确定如何创建这样的面板。我可以将主面板设置为边框布局并将登录屏幕面板设置为 page_end 但是论坛和常见问题解答也必须在 page_end 上......不知何故登录屏幕面板和论坛和常见问题小组必须一起分享 page_end。有什么办法可以做到这一点,或者有更好的方法吗?这让我困惑了大约 2 个小时,我不明白我该怎么做。
现在我有 3 个面板和 1 个框架。 1是添加到主框架的主面板。其他 2 个面板是登录屏幕面板以及论坛和常见问题解答面板。这是代码。
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);