문제

So it's a pretty simple problem, but I can't find a simple way to implement it. I basically have 3 instances of JPanel: north, south and center. Normally, if you play with window height, the center panel will be variable. What I would like, is to have the south panel height being variable instead of the center one.

Here is the code:

public class TestCode {

public static void main(String[] args) {

    JFrame window = new JFrame("Test");
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setSize(300, 300);

    JPanel panelUp = new JPanel();
    JPanel panelCenter = new JPanel();
    JPanel panelDown = new JPanel();

    window.add(panelUp, BorderLayout.NORTH);
    window.add(panelCenter, BorderLayout.CENTER);
    window.add(panelDown, BorderLayout.SOUTH);

    panelUp.setBackground(new Color(200, 240, 200));
    panelCenter.setBackground(new Color(240, 200, 200));
    panelDown.setBackground(new Color(200, 200, 240));

    panelUp.setPreferredSize(new Dimension(panelUp.getWidth(), 50));
    panelCenter.setPreferredSize(new Dimension(panelCenter.getWidth(), 100));
    panelDown.setPreferredSize(new Dimension(panelDown.getWidth(), window.getHeight() - 150));

    window.setVisible(true);
}

}
도움이 되었습니까?

해결책 3

Simply add the top and center into a panel that is itself a border layout :

public static void main(String[] args) {

    JFrame window = new JFrame("Test");
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setSize(300, 300);


    JPanel innerPanel = new JPanel(new BorderLayout());


    JPanel panelUp = new JPanel();
    JPanel panelCenter = new JPanel();
    JPanel panelDown = new JPanel();

    window.add(innerPanel, BorderLayout.NORTH);
    window.add(panelDown, BorderLayout.CENTER);

    innerPanel.add(panelUp, BorderLayout.NORTH);
    innerPanel.add(panelCenter, BorderLayout.SOUTH);

    panelUp.setBackground(new Color(200, 240, 200));
    panelCenter.setBackground(new Color(240, 200, 200));
    panelDown.setBackground(new Color(200, 200, 240));

    panelUp.setPreferredSize(new Dimension(panelUp.getWidth(), 50));
    panelCenter.setPreferredSize(new Dimension(panelCenter.getWidth(), 100));
    panelDown.setPreferredSize(new Dimension(panelDown.getWidth(), window.getHeight() - 150));

    window.setVisible(true);
}

This code should do exactly what you want while hardly altering the code

다른 팁

***************BorderLayout************************
*  ********BorderLayout*in*PAGE_START************ *
*  *                                            * *
*  *   Component in PAGE_START                  * *
*  *                                            * *
*  *   Component in PAGE_END                    * *
*  *                                            * *
*  ********************************************** *
*                                                 *
*      Component in CENTER                        *
*                                                 *
***************************************************

See the Nested Layout Example for more ideas about how to combine layouts to create the required layout.

What I would like, is to have the south panel height being variable instead of the center one.

Have you tried setting the maximum size (setMaximumSize) for the north and center panel? Or perhaps setSize directly.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top