当我们为添加到JScrollPane的JPanel使用setPreferredSize时,为什么它向JScrollPane显示滚动条 [英] Why it shows scrollbar to JScrollPane when we use setPreferredSize for the JPanel which is added to JScrollPane
问题描述
请参见以下代码块
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
public class test extends JFrame {
public test(){
this.setBounds(0,0,300,700);
JPanel pnltemp= new JPanel();
//pnltemp.setBounds(0,0,400,1000);
pnltemp.setPreferredSize(new Dimension(400,1000));
JScrollPane scrtemp= new JScrollPane();
scrtemp.getViewport().add(pnltemp);
this.getContentPane().add(scrtemp);
this.getContentPane().add(scrtemp);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String args[]){
new test();
}
}
- 当我们通过注释"pnltemp.setBounds(0,0,400,1000);"运行上述代码时并将其替换为"pnltemp.setPreferredSize(new Dimension(400,1000));"该窗口显示水平和垂直滚动条. 但是当我们通过注释"pnltemp.setPreferredSize(new Dimension(400,1000));"运行同一程序时并替换为"pnltemp.setBounds(0,0,400,1000)"时,该窗口将不显示水平和垂直滚动条.
- When we run the above code by commenting "pnltemp.setBounds(0,0,400,1000);" and replacing it with "pnltemp.setPreferredSize(new Dimension(400,1000));" the window shows horizontal and vertical scrollbars. but when we run the same programm by commenting "pnltemp.setPreferredSize(new Dimension(400,1000));" and replacing it wiht "pnltemp.setBounds(0,0,400,1000)" the window does not shows horizontal and vertical scrollbars.
为什么同一程序通过更改setBounds和setPreferredSize方法的行为有所不同;因为这两种方法在行为上看起来都是一样的.
why same program behaves differently by changing the setBounds and setPreferredSize method; as these two methods are looks same in behaviour.
还是这样的事情,当我们使用JScrollPane获取滚动条时,必须使用setPreferredSize();.我们要在JScrollPane中添加的组件的方法.
Or is it something like this that when we are using the JScrollPane to get the Scrollbar we must use the setPreferredSize(); method for the componant that we are going to add in JScrollPane.
- 我的第二个问题是,当我们将pnltemp添加到scrtemp时,即,将JPanel直接添加到JScrollPane时,当我们说时,它不会给出错误的意思.
scrtemp.add(pnltemp);
scrtemp.add(pnltemp);
它没有给出任何错误,但是也没有在scrtemp中显示pnltemp和滚动条. 但是当我们键入 scrtemp.getViewPort.add(pnltemp);
it does not give any error but it also does not show the pnltemp and scrollbar in scrtemp. but when we type scrtemp.getViewPort.add(pnltemp);
它没有给出任何错误,但它还在scrtemp中显示了pnltemp和滚动条.
it does not give any error but it also shows the pnltemp and scrollbar in scrtemp.
我通过将背景颜色分配给JPanel和JScrollPane进行了检查.
I checked this by assigning the background color to JPanel and JScrollPane.
有人可以解释这个吗?
谢谢!
推荐答案
来自 JScrollPane
JavaDocs.
From the JScrollPane
JavaDocs.
默认情况下,
JScrollPane
使用ScrollPaneLayout
处理其子组件的布局.ScrollPaneLayout
通过以下两种方法之一确定制作视口视图的大小:
By default
JScrollPane
usesScrollPaneLayout
to handle the layout of its child Components.ScrollPaneLayout
determines the size to make the viewport view in one of two ways:
- 如果视图实现
Scrollable
,则使用getPreferredScrollableViewportSize
,getScrollableTracksViewportWidth
和getScrollableTracksViewportHeight
的组合,否则
使用 -
getPreferredSize
.
- If the view implements
Scrollable
a combination ofgetPreferredScrollableViewportSize
,getScrollableTracksViewportWidth
andgetScrollableTracksViewportHeight
is used, otherwise getPreferredSize
is used.
这篇关于当我们为添加到JScrollPane的JPanel使用setPreferredSize时,为什么它向JScrollPane显示滚动条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!