首页 > 解决方案 > Resizing Jpanel grid columns

问题描述

I'm wondering if it is possible to resize Jpanels, I've been searching for a while but haven't found what I'm looking for.

This is my code:

    JFrame frame = new JFrame("My Frame");
    JPanel panel = new JPanel(new GridLayout(1,2)); // split the panel in 1 rows and 2 cols

    panel.add(new PixelPanel());
    panel.add(new MyPanel());

    frame.getContentPane().add(panel);

    frame.setVisible(true);
    frame.setSize(1920, 1080);

This creates a layout with the 2 columns next to eachother with a border exactly in the middle seperating the 2 Jpanels.A lot of software nowadays offers the user an option to resize the width of the 2 panels. So by grabbing the border and pulling it to the left or right 1 panel will grow larger and the other one will slink down. Is there any build in feature for this? And if not, would it be better to build this behavior onto Jpanel, or would it be better to make custom panel system?

Thank you in advance!

(if the behavior im after isnt clear, I could make a gif explaining it)

标签: javaswingjframejpanel

解决方案


JSplitPane 是您正在寻找的。

JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, new PixelPanel(), new MyPanel());

推荐阅读