我想将JPanel划分为左右段。我怎么做 ?之后,我会在左右两侧放置面板。
答案 0 :(得分:10)
如果无需调整大小,只需使用BorderLayout
并在BorderLayout.EAST
和BorderLayout.WEST
中插入面板:
JPanel panel = new JPanel( new BorderLayout() );
panel.add( leftPanel, BorderLayout.WEST );
panel.add( rightPanel, BorderLayout.EAST );
您还可以考虑使用允许调整用户界面的JSplitPane
:
JSplitPane pane = new JSplitPane( JSplitPane.HORIZONTAL_SPLIT,
leftPanel, rightPanel );
答案 1 :(得分:2)
如果您使用JSPlitPane,则非常容易。
答案 2 :(得分:2)
有两种方式
使用GridLayout
使用JSplitPane(隐藏分隔符)
答案 3 :(得分:2)
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);
答案 4 :(得分:2)
使用JSplitPane或GridLayout
答案 5 :(得分:2)
您可以使用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);
答案 6 :(得分:1)
JPanel leftPanel = new JPanel();
JPanel rightPanel = new JPanel();
JSplitPane pane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftPanel, rightPanel);
答案 7 :(得分:1)
JPanel example = new JPanel(new GridLayout(1,2));
example.add(p1);
example.add(p2);
答案 8 :(得分:0)
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);
}
}