JFrame布局多个JPanel

时间:2015-10-03 18:55:25

标签: java panel gridbaglayout

The Panel I am making

我不确定如何创建这样的面板。我可以将主面板设置为borderlayout并将登录屏幕面板设置为page_end但是论坛和常见问题解答也必须在page_end上.....某种程度上登录屏幕面板和论坛和常见问题解答面板必须共享page_end在一起。有没有我可以做到这一点或者更好的方式?这让我困惑了大约2个小时,我不明白我会怎么做。

现在我有3个面板和1个框架。 1是添加到主框架的主面板。另外两个面板是loginscreen面板和论坛和常见问题解答面板。这是代码。

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

}

1 个答案:

答案 0 :(得分:2)

这是组件布局

来源

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

显示

enter image description here