[英]Make scrollable a JPanel child with GridBagLayout
我有一个使JPanel组件可滚动的问题...我花了3个多小时才使它能够工作,但是没有成功,这就是为什么我要您的帮助!
Window类:
public class Window extends JFrame {
public Window(String title) {
super();
Dimension dimension = IHM.getDimension();
this.setTitle(title);
this.setSize(dimension.width, dimension.height);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setMaximumSize(dimension);
this.setMinimumSize(dimension);
this.setContentPane(new Container());
this.setBackground(IHM.getBlue());
this.pack();
this.setVisible(true);
}
}
容器类:
public class Container extends JPanel {
public static Boolean debug = true;
private Dimension lastDimension = new Dimension(0, 0);
public Container() {
// Layout
GridBagLayout layout = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
// Panel configurations
this.setLayout(layout);
/**
* HEADER
*/
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.gridheight = 1;
this.add(new HeaderPanel(), gbc);
/**
* LEFT
*/
gbc.gridx = 0;
gbc.gridy = 1;
gbc.gridwidth = 1;
gbc.fill = GridBagConstraints.BOTH;
JPanel leftPanel = new LeftPanel();
JScrollPane leftPanelScrollable = new JScrollPane(leftPanel);
leftPanelScrollable.setViewportView(leftPanel);
leftPanelScrollable.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
leftPanelScrollable.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
leftPanelScrollable.setBounds(10, 10, leftPanel.getWidth() - 50, leftPanel.getHeight() - 50);
this.add(leftPanelScrollable, gbc);
// ...
}
}
和leftPanel类:
public class LeftPanel extends JPanel {
private static Dimension dimension = new Dimension(IHM.getWidth() / 2, IHM.getHeight() - 220);
public LeftPanel() {
this.setPreferredSize(dimension);
// Add the labels
this.addLabel();
// Add the radios
this.addRadios();
this.setAutoscrolls(true);
}
}
请查看屏幕截图,我没有任何滚动条:(
图片中的结果:
您对这个问题有任何想法吗? 提前致谢!
leftPanelScrollable.setBounds(10, 10, leftPanel.getWidth(), leftPanel.getHeight());
这应该造成损害。 上面的方法强制JScrollPane
与基础面板的大小相同。 让布局管理器完成工作。
PS我还没有测试过。
您还应该尝试使JPanel
实现Scrollable
并设置
public boolean getScrollableTracksViewportHeight()
和
public boolean getScrollableTracksViewportWidth()
返回false。
this.setPreferredSize(dimension);
不要手动设置首选大小。 面板的布局管理器将确定首选大小。 JPanel的默认布局管理器是FlowLayout
,它将提供首选的大小,好像所有组件都在同一行上一样,这显然不是您想要的,因此请使用其他布局管理器。
this.setAutoscrolls(true);
滚动不需要这样做。
JScrollPane leftPanelScrollable = new JScrollPane(leftPanel);
leftPanelScrollable.setViewportView(leftPanel);
无需设置视口视图。 创建滚动窗格时,指定的组件将添加到视口中。
leftPanelScrollable.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
leftPanelScrollable.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
无需设置滚动条策略。 滚动条将在需要时自动显示(一旦您使代码正常工作),并让布局管理器完成工作。
GridBagLayout layout = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
this.setLayout(layout);
我建议您可以使用易于使用的BorderLayout
。 只需将“ headerPanel”添加到BorderLayout.PAGE_START
。 然后将您的“ leftPane”添加到BorderLayout.CENTER
。 然后,在调整框架大小时,所有额外的空间将进入CENTER
并且滚动条将根据需要显示。
所有重要的代码都存在
在问题解决之前,您不知道什么是进口。 如果以上建议已完成,则可以发布适当的SSCCE来演示问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.