[英]JScrollPane does not set its scrollbar's positional properties immediately
我有一個顯示JPanel
的JScrollPane
,它已添加到其中(在BoxLayout.Y_AXIS
布局中,即垂直向下),其中包含一些帶有標題和邊框的小型JPanel
。
創建此文件后,如果JScrollPane
較大的JPanel
占用的空間超過了可見的空間,從而出現了垂直滾動條,則我的JScrollPane
不一定將其滾動設置為頂部。
解決此問題的方法包括設置JScrollPanel
的value
屬性,該屬性存儲其向下滾動的距離。 但是,在將元素添加到JScrollPane
並將該JScrollPane
添加到它所屬的框架時,尚未設置其位置的屬性(例如,值,最大值,最小值),因此我無法將value
設置為零因為它已經是零,所以在意識到它不為零之前。
下面是我創建的一個帶注釋的小型程序,用於演示該問題。
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() );
}
}
如何將滾動條的值設置為零?
如何將滾動條的值設置為零?
將代碼包裝在第二個SwingUtilities.invokeLater()中。 這將確保在構建GUI之后執行代碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.