java 如何将 JPanel 分为左右段?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15782623/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to divide a JPanel into left and right segments?
提问by SuperStar
I want to divide a JPanel into left and right segments. How do I do that ? After that, I will place panels in the left and right half.
我想将 JPanel 分成左右段。我怎么做 ?之后,我将在左右半边放置面板。
回答by Robin
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
:
如果不需要调整它们的大小,您可以简单地使用 aBorderLayout
并将面板插入BorderLayout.EAST
和BorderLayout.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:
您还可以考虑使用JSplitPane
允许调整 UI 大小的 a:
JSplitPane pane = new JSplitPane( JSplitPane.HORIZONTAL_SPLIT,
leftPanel, rightPanel );
回答by Costis Aivalis
It is very easy if you use a JSPlitPane.
如果您使用JSPlitPane ,这很容易。
回答by mKorbel
there are two ways
有两种方法
use GridLayout
use JSplitPane (with hidden divider)
使用网格布局
使用 JSplitPane(带有隐藏分隔线)
回答by afk5min
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);
回答by Thargor
Use a JSplitPaneor a GridLayout
使用JSplitPane或 GridLayout
回答by Kishor Prakash
You can use SplitPane as Costis Aivalis suggested.
Or
Use Border Layout Manageron JPanel.
Put your left side components in WEST side and put your right side components in EAST side of layout manager.
您可以按照 Costis Aivalis 的建议使用 SplitPane。
或者在 JPanel 上
使用边框布局管理器。
将左侧组件放在 WEST 侧,将右侧组件放在布局管理器的 EAST 侧。
JPanel panel = new JPanel(new BorderLayout());
panel.add(c1, BorderLayout.WEST);
panel.add(c2, BorderLayout.EAST);
回答by Mohan Raj B
JPanel leftPanel = new JPanel();
JPanel rightPanel = new JPanel();
JSplitPane pane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftPanel, rightPanel);
回答by soumer
JPanel example = new JPanel(new GridLayout(1,2));
example.add(p1);
example.add(p2);
回答by Sunil Yadav
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);
}
}