JScrollPane.setviewportview与JScrollPane.add之间的区别 [英] Difference between JScrollPane.setviewportview vs JScrollPane.add
问题描述
我今天遇到了这个新事物,我不知道为什么。例如,当我想在面板中显示某些内容时,我只需将其添加到面板中;但为什么我不能直接添加表格滚动窗格,为什么我必须调用 setviewportview()
方法?什么 add()
方法做什么以及 setViewProtView()
做什么?
I faced this new thing today, and I didn't know why. When I want to show something in panel for example, I just add it to panel; but why I cannot add a table to scroll pane directly, and why I have to call the setviewportview()
method? What does add()
method do and what does setViewProtView()
do?
推荐答案
基本上,你不应该使用 JScrollPane #add
。
Basically, you should not use JScrollPane#add
.
JScrollPane
已经附加了一个组件, JViewport
,这就是 JScrollPane
用于显示添加到视口的任何组件。
JScrollPane
has a single component already attached to it, a JViewport
, this is what the JScrollPane
uses to display any component added to the view port.
setViewportView
是一种方便的方法for for JScrollPane#getViewport#setView
setViewportView
is a convenience method for for JScrollPane#getViewport#setView
基本概念归结为从滚动窗格的角度来看在视图中,它只显示一个组件,因此 add
实际上对它没有任何意义。该方法是从 JComponent延伸
- > 容器
The basic concept comes down to the fact that from the scroll panes point of view, it will only show a single component, so add
doesn't actually make any sense for it. The method is a consequence of extending from JComponent
-> Container
这篇关于JScrollPane.setviewportview与JScrollPane.add之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!