简体   繁体   English

如何将JPanel划分为左右段?

[英]How to divide a JPanel into left and right segments?

I want to divide a JPanel into left and right segments. 我想将JPanel划分为左右段。 How do I do that ? 我怎么做 ? After that, I will place panels in the left and right half. 之后,我会在左右两侧放置面板。

If there is no need to resize them, you can simply use a BorderLayout and insert your panels in the BorderLayout.EAST and BorderLayout.WEST : 如果不需要调整它们的大小,您只需使用BorderLayout并在BorderLayout.EASTBorderLayout.WEST插入面板:

JPanel panel = new JPanel( new BorderLayout() );
panel.add( leftPanel, BorderLayout.WEST );
panel.add( rightPanel, BorderLayout.EAST );

You could also consider using a JSplitPane which allows to resize the UI: 您还可以考虑使用允许调整UI大小的JSplitPane

JSplitPane pane = new JSplitPane( JSplitPane.HORIZONTAL_SPLIT, 
                                  leftPanel, rightPanel );

如果使用JSPlitPane ,则非常容易。

there are two ways 有两种方法

  • use GridLayout 使用GridLayout

  • use JSplitPane (with hidden divider) 使用JSplitPane(隐藏分隔符)

JPanel panel = new JPanel(new BorderLayout());
panel.add(c1, BorderLayout.WEST);
panel.add(c2, BorderLayout.EAST);

JPanel panel = new JPanel(new GridLayout(1, 2));
panel.add(c1);
panel.add(c2);

使用JSplitPane或GridLayout

You can use SplitPane as Costis Aivalis suggested. 您可以使用SplitPane作为Costis Aivalis建议。
Or 要么
Use Border Layout Manager on JPanel. 在JPanel上使用Border Layout Manager
Put your left side components in WEST side and put your right side components in EAST side of layout manager. 将左侧组件放在WEST侧,将右侧组件放在布局管理器的EAST侧。

JPanel panel = new JPanel(new BorderLayout());
panel.add(c1, BorderLayout.WEST);
panel.add(c2, BorderLayout.EAST);
JPanel leftPanel = new JPanel();
JPanel rightPanel = new JPanel();
JSplitPane pane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftPanel, rightPanel);
JPanel example = new JPanel(new GridLayout(1,2));
example.add(p1);
example.add(p2);
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSplitPane;

public class Display{

JFrame frame=new JFrame("Drawing");
North north;
South south;
East east;
West west;
Center center;
public  int width=600,height=600;

public Display() {
    // TODO Auto-generated constructor stub
    frame.setSize(width,width);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocationRelativeTo(null);
    frame.setResizable(true);

    north=new North(frame);
    south=new South(frame);
    east=new East(frame);
    west=new West(frame);
    center=new Center(frame);

    frame.setLayout(new BorderLayout());


JSplitPane pane2=new JSplitPane(JSplitPane.VERTICAL_SPLIT,west,east);

frame.add(pane2);

    frame.setVisible(true);

  }

 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM