[英]How to divide a JPanel into left and right segments?
我想将JPanel划分为左右段。 我怎么做 ? 之后,我会在左右两侧放置面板。
如果不需要调整它们的大小,您只需使用BorderLayout
并在BorderLayout.EAST
和BorderLayout.WEST
插入面板:
JPanel panel = new JPanel( new BorderLayout() );
panel.add( leftPanel, BorderLayout.WEST );
panel.add( rightPanel, BorderLayout.EAST );
您还可以考虑使用允许调整UI大小的JSplitPane
:
JSplitPane pane = new JSplitPane( JSplitPane.HORIZONTAL_SPLIT,
leftPanel, rightPanel );
如果使用JSPlitPane ,则非常容易。
有两种方法
使用GridLayout
使用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
您可以使用SplitPane作为Costis Aivalis建议。
要么
在JPanel上使用Border 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.