gpt4 book ai didi

java - 包含 JPanel 的 JScrollPane 不会滚动

转载 作者:行者123 更新时间:2023-12-01 04:40:26 25 4
gpt4 key购买 nike

我有一个 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);
}
}

最佳答案

如果没有更多详细信息,我猜您的 ResultsPanel 的容器正在扩展以包含整个 ResultsPanel,因此永远不需要滚动条。强制限制结果面板和/或结果面板容器的最大大小。

关于java - 包含 JPanel 的 JScrollPane 不会滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16657843/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com