包含 JPanel 的 JScrollPane 不会滚动
Posted
技术标签:
【中文标题】包含 JPanel 的 JScrollPane 不会滚动【英文标题】:JScrollPane containing a JPanel won't scroll 【发布时间】:2013-05-15 12:03:27 【问题描述】:我有一个 JScrollPane,其中包含一个 JPanel,其中包含一个 JPanel 集合。
集合大小可以更改,这就是我将它放在 JScrollPane 中的原因。
由于某种原因,无论我做什么,旋钮都不会显示在滚动条上。
如您所见,主 JPanel(预览版)确实有一个布局。我读到如果布局为空,这可能会发生。
有谁知道如何解决这个问题?
记住这一点
ReactionPreviewPanel 扩展了 JPanel。 show(ArrayList) 方法将创建一堆 ReactionPreviewPanel 并使用 SpringLayout 在主(预览)JPanel 中组织它们。这是我的代码:
import java.awt.Color;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.SpringLayout;
import javax.swing.SwingConstants;
public class ResultsPanel extends JPanel
private static final long serialVersionUID = -1713825505191028954L;
private JTextPane name, url, tags;
private JButton clear;
private JLabel results;
private JScrollPane scroller;
private JPanel previews;
public ResultsPanel()
super();
initElements();
addElements();
setLayout(getSpringLayout());
private void initElements()
this.previews = new JPanel();
this.previews.setLayout(new SpringLayout());
this.previews.setBackground(Color.white);
this.scroller = new JScrollPane(this.previews, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
this.scroller.getVerticalScrollBar().setUnitIncrement(1);
this.results = new JLabel("[Displaying nothing]");
this.results.setHorizontalAlignment(SwingConstants.CENTER);
this.name = new JTextPane();
this.name.setEditable(false);
this.url = new JTextPane();
this.url.setEditable(false);
this.tags = new JTextPane();
this.tags.setEditable(false);
this.clear = new JButton("Clear");
this.clear.setEnabled(false);
private void addElements()
add(clear);
add(this.results);
add(this.scroller);
private SpringLayout getSpringLayout()
SpringLayout layout = new SpringLayout();
layout.putConstraint(SpringLayout.SOUTH, this.clear, -5, SpringLayout.SOUTH, this);
layout.putConstraint(SpringLayout.EAST, this.clear, -5, SpringLayout.EAST, this);
layout.putConstraint(SpringLayout.WEST, this.clear, 5, SpringLayout.WEST, this);
layout.putConstraint(SpringLayout.NORTH, this.results, 5, SpringLayout.NORTH, this);
layout.putConstraint(SpringLayout.EAST, this.results, -5, SpringLayout.EAST, this);
layout.putConstraint(SpringLayout.WEST, this.results, 5, SpringLayout.WEST, this);
layout.putConstraint(SpringLayout.NORTH, this.scroller, 2, SpringLayout.SOUTH, this.results);
layout.putConstraint(SpringLayout.EAST, this.scroller, -5, SpringLayout.EAST, this);
layout.putConstraint(SpringLayout.WEST, this.scroller, 5, SpringLayout.WEST, this);
layout.putConstraint(SpringLayout.SOUTH, this.scroller, -2, SpringLayout.NORTH, this.clear);
return layout;
public void show(ArrayList<Reaction> reactions)
this.results.setText(reactions.size() + " results");
this.previews.removeAll();
this.previews.repaint();
if (reactions == null || reactions.size() == 0)
return;
ArrayList<ReactionPreviewPanel> panels = new ArrayList<ReactionPreviewPanel>();
for (Reaction r : reactions)
ReactionPreviewPanel temp = new ReactionPreviewPanel(r);
panels.add(temp);
for (ReactionPreviewPanel rpp : panels)
this.previews.add(rpp);
SpringLayout layout = new SpringLayout();
for (int i = 0; i < panels.size(); i++)
layout.putConstraint(SpringLayout.WEST, panels.get(i), 2, SpringLayout.WEST, this.previews);
layout.putConstraint(SpringLayout.EAST, panels.get(i), -3, SpringLayout.EAST, this.previews);
if (i == 0)
layout.putConstraint(SpringLayout.NORTH, panels.get(i), 2, SpringLayout.NORTH, this.previews);
else
layout.putConstraint(SpringLayout.NORTH, panels.get(i), 2, SpringLayout.SOUTH, panels.get(i - 1));
this.previews.setLayout(layout);
【问题讨论】:
【参考方案1】:如果没有更多详细信息,我猜您的 ResultsPanel 的容器正在扩展以包含整个 ResultsPanel,因此永远不需要滚动条。强制限制结果面板和/或结果面板容器的最大大小。
【讨论】:
我认为不需要更多细节...包含面板与预览窗格的大小(比滚动窗格大)没有任何关系。以上是关于包含 JPanel 的 JScrollPane 不会滚动的主要内容,如果未能解决你的问题,请参考以下文章
如何使JScrollPane(在BorderLayout中,包含JPanel)平滑自动滚动
带有 boxLayout 的 JScrollPane 中的 Java 可拖动 JPanel