Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java JPanel How to fixed sizes

I want to have a resizable panel, that always has the top green panel of a fixed depth. i.e. all changes in height should effect the yellow panel only.

My code below is almost OK, except the green panel varies in size a little.

How do I do this?

            Panel.setLayout(new BoxLayout(Panel, BoxLayout.Y_AXIS));
        Panel.setAlignmentX(Component.LEFT_ALIGNMENT);

        JPanel TopPanel = new JPanel();
        TopPanel.setPreferredSize(new Dimension(80,150));
        TopPanel.setVisible(true);
        TopPanel.setBackground(Color.GREEN);
        JPanel MainPanel = new JPanel();
        MainPanel.setPreferredSize(new Dimension(80,750));
        MainPanel.setVisible(true);
        MainPanel.setOpaque(true);
        MainPanel.setBackground(Color.YELLOW);

        Panel.add(TopPanel);
        Panel.add(MainPanel);

enter image description here

like image 315
ManInMoon Avatar asked Nov 12 '13 17:11

ManInMoon


People also ask

How do I change the layout of a JPanel?

You can set a panel's layout manager using the JPanel constructor. For example: JPanel panel = new JPanel(new BorderLayout()); After a container has been created, you can set its layout manager using the setLayout method.

How do you resize a frame in Java?

You can change the size of the JFrame by simply placing the cursor in the corners and dragging it. Or if you press the resize option next to close(X) in the upper right corner, it will be enlarged to full-screen size. This happens because the resize is set to “true” by default.

Which method is used for resizing the frame in Java?

To resize a frame, There is a method JFrame. setSize(int width, int height) which takes two parameters width and height. Below is the code to change the window size of a JFrame.

What does JPanel repaint do?

Ideally if you want to draw in an area then you would extend JPanel with a custom paint and place it below the button. To explain repaint further - the point of 'repaint' is to tell the window manager that you have changed something that requires the component to be redrawn.


2 Answers

Your question didn't restrict the solution to a BoxLayout, so I am going to suggest a different layout manager.

I would attack this with a BorderLayout and put the green panel in the PAGE_START location. Then put the yellow panel in the CENTER location without a preferredSize call.

http://docs.oracle.com/javase/tutorial/uiswing/layout/border.html

Here is an SSCCE example of the solution:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class TestPad extends JFrame {
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.getContentPane().setLayout(new BorderLayout());

        JPanel green = new JPanel();
        green.setPreferredSize(new Dimension(80, 150));
        green.setBackground(Color.GREEN);

        JPanel yellow = new JPanel();
        yellow.setBackground(Color.YELLOW);

        frame.getContentPane().add(green, BorderLayout.PAGE_START);
        frame.getContentPane().add(yellow, BorderLayout.CENTER);

        frame.pack();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
}
like image 96
jzd Avatar answered Oct 07 '22 09:10

jzd


If you make your Panel use BorderLayout instead of BoxLayout and put TopPanel in BorderLayout.NORTH and MainPanel in BorderLayout.CENTER, then they will both resize horizontally, but only the MainPanel will resize vertically.

See the BorderLayout documentation

like image 3
gla3dr Avatar answered Oct 07 '22 07:10

gla3dr