Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
运行时添加的Java scrollPane不会滚动_Java_Swing_Jscrollpane - Fatal编程技术网

运行时添加的Java scrollPane不会滚动

运行时添加的Java scrollPane不会滚动,java,swing,jscrollpane,Java,Swing,Jscrollpane,我们正在尝试制作一个包含多个滚动窗格的应用程序。第一种方法或多或少效果不错。我们无法调整运行时必须滚动的面板的大小。因此,为了解决此问题,我们在GUI生成器中设置了大小: 只有在这种黑客修复面板滚动采取正确的大小。因为代码不起作用: PanelGraph.setPreferredSize(PanelGraph.getPreferredSize()); PanelGraph.validate(); TopPanel.setSize(locationX, (panelData

我们正在尝试制作一个包含多个滚动窗格的应用程序。第一种方法或多或少效果不错。我们无法调整运行时必须滚动的面板的大小。因此,为了解决此问题,我们在GUI生成器中设置了大小:

只有在这种黑客修复面板滚动采取正确的大小。因为代码不起作用:

    PanelGraph.setPreferredSize(PanelGraph.getPreferredSize());
    PanelGraph.validate();
    TopPanel.setSize(locationX, (panelDatabase.length * 15));
    TopPanel.setPreferredSize(TopPanel.getPreferredSize());
    TopPanel.validate();
    TopScrollPanel.setViewportView(TopPanel);
    TopScrollPanel.getVerticalScrollBar().setUnitIncrement(15);
    TopScrollPanel.validate();
TopPanel是TopScrollPanel中的面板,我们试图用历史数组的长度乘以一行15的像素来设置大小。此代码不会调整TopPanel或viewportview的大小。这一部分目前还可以使用,但我们仍然想知道如何解决这个问题

对于第二个滚动面板,我们必须在每次更新面板时调整其大小,因为列表会增加。无论我们试图使用什么代码:滚动窗格都不会滚动。单击时,此函数被调用:

public void actionPerformed(ActionEvent e) {
    history = SensorValuesHistory();
    javax.swing.JScrollPane HistoryLogPane = new javax.swing.JScrollPane();
    HistoryLogPane.setLayout(new ScrollPaneLayout()); // edit @kiheru
    HistoryLogPane.setBackground(Color.WHITE);
    HistoryLogPane.setBorder(BorderFactory.createEmptyBorder());
    HistoryLogPane.setLocation(0, 0);
    HistoryLogPane.add(history);
    HistoryLogPane.setSize(new Dimension(history.getSize()));
    history.setPreferredSize(history.getSize());//and we tried: history.setPreferredSize(history.getPreferredSize());
    System.out.println("SIZE: " + HistoryLogPane.getSize());
    HistoryLocationPanel.removeAll();
    HistoryLocationPanel.add(HistoryLogPane);
    HistoryLocationPanel.repaint();
    HistoryLogPane.repaint();
}

由于第一个问题,我们删除了旧的滚动窗格并生成了一个新的滚动窗格。让这个滚动窗格滚动,我们该怎么办?

@kiheru,谢谢你的建议和指导!通过重新验证历史面板而不是滚动面板,功能现在可以正常工作了!这是工作代码:

    public void actionPerformed(ActionEvent e) {
    HistoryLogPane.setLayout(new ScrollPaneLayout());//<<<
    history = (JPanel)HistoryLogPane.getViewport().getView();//<<<
    javax.swing.JScrollPane HistoryLogPane = new javax.swing.JScrollPane();
    HistoryLogPane.setLayout(new ScrollPaneLayout()); // edit @kiheru
    HistoryLogPane.setBackground(Color.WHITE);
    HistoryLogPane.setBorder(BorderFactory.createEmptyBorder());
    HistoryLogPane.setLocation(0, 0);
    HistoryLogPane.add(history);
    HistoryLogPane.setSize(new Dimension(history.getSize()));
    history.setPreferredSize(history.getSize());//and we tried: history.setPreferredSize(history.getPreferredSize());
    history.revalidate();//<<<
    System.out.println("SIZE: " + HistoryLogPane.getSize());
    HistoryLocationPanel.removeAll();
    HistoryLocationPanel.add(HistoryLogPane);
    HistoryLocationPanel.repaint();
    HistoryLogPane.repaint();
}

在我们添加的每一行之后,我们都会在这个表单中添加一个“//问题,如果没有SSCCE/MCVE,并且使用一些GUI构建程序的printscreen,在默认情况下会使非常简单的事情复杂化,那么在这里很难回答这个问题,请阅读有关如何使用滚动窗格的Oracle教程,对于SSCCE/MCVE表单中的工作代码示例,使用重新验证+重新绘制成功,并且使用setPreferredSize显示最接近的父容器不是您在案例中看到的正确方式,甚至是可能的,但是,但是需要代数的基础知识玩像素完美un_knows LayoutManager==各种LayoutManager不同地接受PreferredSizeAll所以,您应该设置JScrollPane的视图,而不是向其添加组件。最简单的方法是使用以视图为参数的,但是setViewportView也应该可以工作,而且您显然已经在另一个面板中使用了它。我的意思不是明确地使用ScrollPaneLayout,这是滚动窗格本身使用的,而是为包含的组件管理的普通布局,如果我读得正确,它就是历史。