我在 JScrollPane 中有一个 JPanel。 JPanel 内部有许多 JButton。然而,当 JPanel 和 JScrollPanel 初始化时,这些 JButton 被设置为 NOT VISIBLE (jbutton.setVisible(false))。仅当另一个类触发此操作时,JButton 才会设置为 VISIBLE (jbutton.setVisible(true))。
但是,尽管这些 JButton 已成功添加并设置为在 JPanel 上可见,但它们在 JPanel 或 JScrollPane 上不可见。我的猜测是,当 JScrollPane 添加 JButtons 时,JButtons 设置为“不可见”,即使 JButtons 设置为“可见”,JScrollPane 也不会检测到它们。
是否有一种方法可以动态更改 JScrollPane 内 JPanel 中 JButton 的可见性?
最佳答案
However, although these JButtons are getting successfully added and set to visible on the JPanel, they are not visible on the JPanel or JScrollPane. My guess is that when the JScrollPane adds the JButtons, the JButtons are set to NOT VISIBLE, and even if the JButtons are set to VISIBLE, the JScrollPane does not detect they are.
必须调用
revalidate()
和repaint()
到JPanel
,作为执行所有更改后的最后一行代码必须更改
setUnitIncrement
对于包裹在JScrollPane
中的JPanel
中的JButtons
,因为滚动太慢,不自然
关于java - 在 JScrollpane 内动态更改 JPanel 中的内容可见性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15624911/