Java JScrollPane不设置其滚动条';立即删除位置属性
我有一个Java JScrollPane不设置其滚动条';立即删除位置属性,java,swing,Java,Swing,我有一个JScrollPane显示JPanel,它添加了一个BoxLayout.Y_轴布局,即垂直向下)小JPanels,其中包含一些带有标题和边框的文本 创建时,如果JScrollPane中较大的JPanel占用的空间大于可查看的空间,因此会出现垂直滚动条,则myJScrollPane不必将其滚动设置为顶部 修复此问题需要设置JScrollPanel的值属性,该属性用于存储其向下滚动的距离。但是,在将元素添加到JScrollPane中,然后将该JScrollPane添加到它所属的帧中时,其位置
JScrollPane
显示JPanel
,它添加了一个BoxLayout.Y_轴
布局,即垂直向下)小JPanel
s,其中包含一些带有标题和边框的文本
创建时,如果JScrollPane
中较大的JPanel
占用的空间大于可查看的空间,因此会出现垂直滚动条,则myJScrollPane
不必将其滚动设置为顶部
修复此问题需要设置JScrollPanel
的值
属性,该属性用于存储其向下滚动的距离。但是,在将元素添加到JScrollPane
中,然后将该JScrollPane
添加到它所属的帧中时,其位置的属性(例如值、最大值、最小值)尚未设置,因此我无法将值设置为零,因为它已经为零,在意识到它不是零之前
下面是我创建的一个小程序,用来演示这个问题
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
public class App
{
public static void main ( String[] args )
{
SwingUtilities.invokeLater( new Runnable()
{
@Override
public void run ()
{
new App();
}
} );
}
public App()
{
// Making the main window.
JFrame window = new JFrame();
window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
window.setSize( 200, 200 );
window.setLayout(
new BoxLayout( window.getContentPane(), BoxLayout.Y_AXIS ) );
// Making the JPanel that the scrollbox will be on.
JPanel scrollBoxPanel = new JPanel();
scrollBoxPanel
.setLayout( new BoxLayout( scrollBoxPanel, BoxLayout.Y_AXIS ) );
scrollBoxPanel.setMaximumSize( new Dimension( 300, 500 ) );
// Making the JScrollPane.
JScrollPane listScroller = new JScrollPane( scrollBoxPanel );
// Making some panels of text.
JPanel smallPanel;
JTextArea textArea;
for ( int i = 0; i < 3; i++ )
{
smallPanel = new JPanel();
smallPanel.setLayout( new BorderLayout() );
textArea = new JTextArea(
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum fringilla, augue vitae venenatis venenatis, dui sapien suscipit libero, et malesuada quam odio vel sapien.\n" );
textArea.setLineWrap( true );
TitledBorder criticalPanelBorder = BorderFactory.createTitledBorder(
BorderFactory.createEtchedBorder( EtchedBorder.RAISED ),
"Box " + i, TitledBorder.LEFT, TitledBorder.TOP,
new Font( null, Font.BOLD, 14 ) );
smallPanel.setBorder( new CompoundBorder( criticalPanelBorder,
new EmptyBorder( 5, 5, 5, 5 ) ) );
smallPanel.add( textArea );
scrollBoxPanel.add( smallPanel );
}
// Adding the JScrollPane to the window and displaying.
listScroller.validate();
window.add( listScroller );
window.setVisible( true );
// Printing the attributes of the JScrollBar. Note that the value prints
// as 0, but is not zero in the program.
JScrollBar scrollBar = listScroller.getVerticalScrollBar();
System.out.println( "Value: " + scrollBar.getValue() + ", Max: "
+ scrollBar.getMaximum() + ", Min: " + scrollBar.getMinimum() );
}
}
导入java.awt.BorderLayout;
导入java.awt.Dimension;
导入java.awt.Font;
导入javax.swing.BorderFactory;
导入javax.swing.BoxLayout;
导入javax.swing.JFrame;
导入javax.swing.JPanel;
导入javax.swing.JScrollBar;
导入javax.swing.JScrollPane;
导入javax.swing.JTextArea;
导入javax.swing.SwingUtilities;
导入javax.swing.border.CompoundBorder;
导入javax.swing.border.EmptyBorder;
导入javax.swing.border.EtchedBorder;
导入javax.swing.border.TitledBorder;
公共类应用程序
{
公共静态void main(字符串[]args)
{
SwingUtilities.invokeLater(新的Runnable()
{
@凌驾
公开作废运行()
{
新应用程序();
}
} );
}
公共应用程序()
{
//制作主窗口。
JFrame窗口=新JFrame();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
设置窗口大小(200200);
window.setLayout(
新的BoxLayout(window.getContentPane(),BoxLayout.Y_轴));
//制作滚动框将打开的JPanel。
JPanel scrollBoxPanel=新的JPanel();
滚动框面板
.setLayout(新的BoxLayout(滚动BoxPanel,BoxLayout.Y_轴));
scrollBoxPanel.setMaximumSize(新尺寸(300500));
//制作JScrollPane。
JScrollPane listScroller=新的JScrollPane(scrollBoxPanel);
//制作一些文本面板。
JPanel smallPanel;
JTextArea textArea;
对于(int i=0;i<3;i++)
{
smallPanel=newjpanel();
setLayout(新的BorderLayout());
textArea=新的JTextArea(
“Lorem ipsum dolor sit amet,是一位杰出的建筑设计师。前庭,威尼斯人的生命,两位智者的自由,以及一位智者。”;
textArea.setLineWrap(true);
TitledBorder criticalPanelBorder=BorderFactory.createTitledBorder(
BorderFactory.createEtchedBorder(EtchedBorder.RAISED),
“Box”+i,TitledBorder.LEFT,TitledBorder.TOP,
新字体(null,Font.BOLD,14));
smallPanel.setBorder(新复合边框)(criticalPanelBorder,
新订单(5,5,5,5));
添加(文本区域);
scrollBoxPanel.add(smallPanel);
}
//将JScrollPane添加到窗口并显示。
listcoller.validate();
添加(列表滚动器);
window.setVisible(true);
//打印JScrollBar的属性。请注意,值会打印
//为0,但在程序中不是零。
JScrollBar scrollBar=listScroller.getVerticalScrollBar();
System.out.println(“值:”+scrollBar.getValue()+”,最大值:
+scrollBar.getMaximum()+”,Min:“+scrollBar.getMinimum());
}
}
如何将滚动条的值设置为零
如何将滚动条的值设置为零
将代码包装到第二个SwingUtilities.invokeLater()中。这将确保在构建GUI后执行代码
如何将滚动条的值设置为零
将代码包装到第二个SwingUtilities.invokeLater()中。这将确保在构建GUI后执行代码。正常工作,+1。然而,这感觉有点像黑客。如果问题没有其他解决方案,我会接受这个答案,但理想情况下,我希望有一种方法可以立即设置值,而不需要等待所有其他AWT事件处理,除非编程GUI或Swing的某些方面表明这是在面临此类问题时执行此类操作的标准方法?@AlexW,很高兴它起到了作用,别忘了“接受”通过单击复选标记,让人们知道问题已经解决。我希望有一种方法可以立即设置值
-是的,您尝试打印值,结果是0,因此,幕后的代码并不总是按顺序工作,原因可能是我们不知道或不理解,我们对此无能为力。你也许能在适当的时候找到一些听众来产生一个事件,但我只是按照我所知道的去做。这是论坛中常见的解决方案,不仅适用于此,也适用于其他情况。有效,+1。然而,这感觉有点像黑客。如果这个问题没有其他解决方案,我会接受这个答案,但理想情况下,我希望有一种方法可以立即设置值,而不需要等待所有其他AWT事件被处理,除非编程GUI或Swing的某些方面表明这是处理这些事件的标准方法