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);
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.
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.
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.
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.
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);
}
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With