java 在一个 JFrame 中使用两个 JPanel
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13057836/
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
Using two JPanels in one JFrame
提问by Kynian
I'm attempting to create a program that allows the user to click a button to place something in the JPanel
and allowing them to move this item around. I have already found a good layout to use to allow the moving components (see thislink). However, I'm just curious the best way to create a layout like this? My hope is to have something like this:
我正在尝试创建一个程序,该程序允许用户单击按钮以在其中放置某些内容JPanel
并允许他们四处移动该项目。我已经找到了一个很好的布局来允许移动组件(请参阅此链接)。但是,我只是好奇创建这样的布局的最佳方法是什么?我希望有这样的事情:
How can I accomplish this? Would I want to use two JPanel
's or something else?
我怎样才能做到这一点?我想使用两个JPanel
还是其他东西?
回答by Dan D.
The main panel (or the window content pane) would have to have a BorderLayout
as the layout manager.
主面板(或窗口内容面板)必须有一个BorderLayout
作为布局管理器。
Then, the buttons panel would be added to BorderLayout.WEST
and the drag panel to BorderLayout.CENTER
.
然后,按钮面板将被添加到BorderLayout.WEST
,拖动面板将被添加到BorderLayout.CENTER
。
There is a Visual Guideto swing layout managers.
有一个摆动布局管理器的视觉指南。
回答by Maxim Shoustin
Try to use JSplitPane
:
尝试使用JSplitPane
:
Here is a code example:
这是一个代码示例:
class SplitPane extends JFrame {
private JSplitPane splitPaneV;
private JSplitPane splitPaneH;
private JPanel panel1;
private JPanel panel2;
private JPanel panel3;
public SplitPane(){
setTitle( "Split Pane Application" );
setBackground( Color.gray );
JPanel topPanel = new JPanel();
topPanel.setLayout( new BorderLayout() );
getContentPane().add( topPanel );
// Create the panels
createPanel1();
createPanel2();
createPanel3();
// Create a splitter pane
splitPaneV = new JSplitPane( JSplitPane.VERTICAL_SPLIT );
topPanel.add( splitPaneV, BorderLayout.CENTER );
splitPaneH = new JSplitPane( JSplitPane.HORIZONTAL_SPLIT );
splitPaneH.setLeftComponent( panel1 );
splitPaneH.setRightComponent( panel2 );
splitPaneV.setLeftComponent( splitPaneH );
splitPaneV.setRightComponent( panel3 );
}
public void createPanel1(){
panel1 = new JPanel();
panel1.setLayout( new BorderLayout() );
// Add some buttons
panel1.add( new JButton( "North" ), BorderLayout.NORTH );
panel1.add( new JButton( "South" ), BorderLayout.SOUTH );
panel1.add( new JButton( "East" ), BorderLayout.EAST );
panel1.add( new JButton( "West" ), BorderLayout.WEST );
panel1.add( new JButton( "Center" ), BorderLayout.CENTER );
}
public void createPanel2(){
panel2 = new JPanel();
panel2.setLayout( new FlowLayout() );
panel2.add( new JButton( "Button 1" ) );
panel2.add( new JButton( "Button 2" ) );
panel2.add( new JButton( "Button 3" ) );
}
public void createPanel3(){
panel3 = new JPanel();
panel3.setLayout( new BorderLayout() );
panel3.setPreferredSize( new Dimension( 400, 100 ) );
panel3.setMinimumSize( new Dimension( 100, 50 ) );
panel3.add( new JLabel( "Notes:" ), BorderLayout.NORTH );
panel3.add( new JTextArea(), BorderLayout.CENTER );
}
public static void main( String args[] ){
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (Exception evt) {}
// Create an instance of the test application
SplitPane mainFrame = new SplitPane();
mainFrame.pack();
mainFrame.setVisible( true );
}
}
You can play with splitPaneH.setOneTouchExpandable
true/false
你可以玩 splitPaneH.setOneTouchExpandable
真/假
You can confugure divider location for both like:
您可以为两者配置分隔符位置,例如:
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension d = tk.getScreenSize();
int width = d.width;
int height = d.height;
spane.setDividerLocation((width*3)/4);
spanex.setDividerLocation(width/4);