边框布局无法按预期工作
Border layout doesn't work as intended
我想实现下面的布局。
共有 6 个面板。顶部的4个按钮是一个面板,图像右侧的3个按钮也是一个面板。除了这两个之外,还有 4 个其他面板,如边界所示。我尝试了下面的代码,但所有内容都以分散的方式显示。
mainPanel.add(topToolBarPanel,BorderLayout.PAGE_START);
mainPanel.add(lefsideToolBarPanel,BorderLayout.LINE_START);
mainPanel.add(descriptionPanel,BorderLayout.LEFT);
mainPanel.add(mapPanel,BorderLayout.CENTER);
mainPanel.add(propertiesPanel,BorderLayout.EAST);
mainPanel.add(tablePanel,BorderLayout.PAGE_END);
如何才能实现如图所示的设计?我需要将所有面板都安排在该 mainPanel 内。虽然我不能使用空布局。请指教
在 trashgod 的回答之后 :
JPanel gridPanel = new JPanel(new GridLayout(1, 0));
gridPanel.add(jInternalFrame1);
gridPanel.add(descriptionPanel);
mainPanel.add(gridPanel, BorderLayout.LINE_START);
mainPanel.add(topToolBarPanel,BorderLayout.PAGE_START);
mainPanel.add(tablePanel,BorderLayout.PAGE_END);
mainPanel.add(mapPanel,BorderLayout.CENTER);
mainPanel.add(PropertiesPanel,BorderLayout.LINE_END);
我得到的:
将lefsideToolBarPanel
和descriptionPanel
添加到具有GridLayout
的面板;将新面板添加到 BorderLayout
.
Panel p new Panel(new GridLayout(1, 0));
p.add(lefsideToolBarPanel);
p.add(descriptionPanel);
//mainPanel.add(lefsideToolBarPanel, BorderLayout.LINE_START);
//mainPanel.add(descriptionPanel, BorderLayout.LEFT);
mainPanel.add(p, BorderLayout.LINE_START);
没有BorderLayout.LEFT
。另见 A Visual Guide to Layout Managers.
附录:您更新后的问题显示了 topToolBarPanel
的元素,应将其添加到 PAGE_START
,而不是 LINE_START
。
//mainPanel.add(topToolBarPanel,BorderLayout.LINE_START);
mainPanel.add(topToolBarPanel,BorderLayout. PAGE_START);
The width of the propertiesPanel
and height of the tablePanel
need to be increased. I used setSize()
…
对于 propertiesPanel
,您可以覆盖 getPreferredSize()
,如 here. For the tablePanel
, override getPreferredScrollableViewportSize()
to customize the size of the table's enclosing JScrollPane
, for 所述。
我建议使用 JLabel 作为您的 "layout" 以使用 setBounds(x, y, width, height) 精确定位您的对象。它看起来类似于:
JButton button = new JButton("Text or Image");
JLabel backgr = new JLabel();
JFrame frame = new JFrame("JLabel as Layout");
button.setBounds(100, 200, 340, 40);
backgr.add(button);
frame.add(backgr);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 600);
frame.setLocation(40, 40);
frame.validate();
frame.setVisible(true);
我知道这对你来说只是一个简单的例子,但我认为它应该用于解释......所以只需将所有内容添加到 backgr JLabeland 你就可以开始了. Quick and dirty example but the a way to go.
我想实现下面的布局。
共有 6 个面板。顶部的4个按钮是一个面板,图像右侧的3个按钮也是一个面板。除了这两个之外,还有 4 个其他面板,如边界所示。我尝试了下面的代码,但所有内容都以分散的方式显示。
mainPanel.add(topToolBarPanel,BorderLayout.PAGE_START);
mainPanel.add(lefsideToolBarPanel,BorderLayout.LINE_START);
mainPanel.add(descriptionPanel,BorderLayout.LEFT);
mainPanel.add(mapPanel,BorderLayout.CENTER);
mainPanel.add(propertiesPanel,BorderLayout.EAST);
mainPanel.add(tablePanel,BorderLayout.PAGE_END);
如何才能实现如图所示的设计?我需要将所有面板都安排在该 mainPanel 内。虽然我不能使用空布局。请指教
在 trashgod 的回答之后 :
JPanel gridPanel = new JPanel(new GridLayout(1, 0));
gridPanel.add(jInternalFrame1);
gridPanel.add(descriptionPanel);
mainPanel.add(gridPanel, BorderLayout.LINE_START);
mainPanel.add(topToolBarPanel,BorderLayout.PAGE_START);
mainPanel.add(tablePanel,BorderLayout.PAGE_END);
mainPanel.add(mapPanel,BorderLayout.CENTER);
mainPanel.add(PropertiesPanel,BorderLayout.LINE_END);
我得到的:
将lefsideToolBarPanel
和descriptionPanel
添加到具有GridLayout
的面板;将新面板添加到 BorderLayout
.
Panel p new Panel(new GridLayout(1, 0));
p.add(lefsideToolBarPanel);
p.add(descriptionPanel);
//mainPanel.add(lefsideToolBarPanel, BorderLayout.LINE_START);
//mainPanel.add(descriptionPanel, BorderLayout.LEFT);
mainPanel.add(p, BorderLayout.LINE_START);
没有BorderLayout.LEFT
。另见 A Visual Guide to Layout Managers.
附录:您更新后的问题显示了 topToolBarPanel
的元素,应将其添加到 PAGE_START
,而不是 LINE_START
。
//mainPanel.add(topToolBarPanel,BorderLayout.LINE_START);
mainPanel.add(topToolBarPanel,BorderLayout. PAGE_START);
The width of the
propertiesPanel
and height of thetablePanel
need to be increased. I usedsetSize()
…
对于 propertiesPanel
,您可以覆盖 getPreferredSize()
,如 here. For the tablePanel
, override getPreferredScrollableViewportSize()
to customize the size of the table's enclosing JScrollPane
, for
我建议使用 JLabel 作为您的 "layout" 以使用 setBounds(x, y, width, height) 精确定位您的对象。它看起来类似于:
JButton button = new JButton("Text or Image");
JLabel backgr = new JLabel();
JFrame frame = new JFrame("JLabel as Layout");
button.setBounds(100, 200, 340, 40);
backgr.add(button);
frame.add(backgr);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 600);
frame.setLocation(40, 40);
frame.validate();
frame.setVisible(true);
我知道这对你来说只是一个简单的例子,但我认为它应该用于解释......所以只需将所有内容添加到 backgr JLabeland 你就可以开始了. Quick and dirty example but the a way to go.