Java 在运行时添加的 scrollPane 不会滚动
Java scrollPane added on runtime won't scroll
我们正在尝试制作一个包含多个滚动窗格的应用程序。第一个或多或少工作正常。我们无法调整它必须在运行时滚动的面板的大小。因此,为了解决这个问题,我们在 GUI 构建器中设置大小:
只有通过这个 hacky 修复面板才能滚动到正确的大小。因为代码不起作用:
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 或视口视图的大小。这部分目前有效,但我们仍然想知道如何解决这个问题?
对于第二个滚动面板,我们每次更新面板时都必须调整它的大小,因为列表会增长。无论我们尝试使用什么代码:滚动窗格都不会滚动。当我们点击这个函数时调用:
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();
}
由于第一个问题,我们删除了旧的滚动窗格并生成了一个新的。我们需要做什么才能让这个 scrollPane 滚动?
@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();
}
在我们添加的每一行之后,我们都放了一个“//<<<”来指出它。
只有通过这个 hacky 修复面板才能滚动到正确的大小。因为代码不起作用:
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 或视口视图的大小。这部分目前有效,但我们仍然想知道如何解决这个问题?
对于第二个滚动面板,我们每次更新面板时都必须调整它的大小,因为列表会增长。无论我们尝试使用什么代码:滚动窗格都不会滚动。当我们点击这个函数时调用:
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();
}
由于第一个问题,我们删除了旧的滚动窗格并生成了一个新的。我们需要做什么才能让这个 scrollPane 滚动?
@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();
}
在我们添加的每一行之后,我们都放了一个“//<<<”来指出它。