Java 启动程序时,如何将包含JPanel的JScrollPane滚动到顶部?
我对JScrollPane有一个问题,当程序启动时,我无法从顶部开始。我希望这里的某个巫师能找到答案:P 下面是一个工作代码示例:Java 启动程序时,如何将包含JPanel的JScrollPane滚动到顶部?,java,swing,jpanel,Java,Swing,Jpanel,我对JScrollPane有一个问题,当程序启动时,我无法从顶部开始。我希望这里的某个巫师能找到答案:P 下面是一个工作代码示例: public class JScrollPaneExample extends JPanel{ public JScrollPaneExample(int rowCount){ setLayout(new BorderLayout()); JScrollPane scrollPane = makeScrollPane(rowCount);
public class JScrollPaneExample extends JPanel{
public JScrollPaneExample(int rowCount){
setLayout(new BorderLayout());
JScrollPane scrollPane = makeScrollPane(rowCount);
add(scrollPane, BorderLayout.PAGE_START);
setPreferredSize(new Dimension(300,300));
}
private JScrollPane makeScrollPane(int rowCount) {
JScrollPane scrollPane = new JScrollPane();
JPanel pane = new JPanel();
pane.setLayout(new GridLayout(rowCount, 0));
for(int i = 0; i < rowCount; i++){
if(i != 4)
pane.add(makeTestField("Test " + (i+1), "Test description: " + (i+1), i));
else {
pane.add(makeTestField("This is a test",
"This is also a test to see if this field will do a line break. "
+ "This is also a test to see if this field will do a line break. "
+ "Something else could be entirely different...",
i));
}
}
scrollPane.getViewport().add(pane);
scrollPane.setPreferredSize(new Dimension(200, 400));
return scrollPane;
}
protected static JPanel makeTestField(String header, String description, int rowCount){
JPanel testField = new JPanel();
testField.setLayout(new BorderLayout());
testField.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
JLabel label = new JLabel(header);
Font font = new Font("Serif", Font.BOLD, 18);
label.setFont(font);
JButton btn = new JButton("Start");
JTextArea textArea = new JTextArea(description);
textArea.setEditable(false);
textArea.setBackground(label.getBackground());
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
textArea.setBorder(BorderFactory.createEmptyBorder(0,0,0,5));
testField.add(label, BorderLayout.PAGE_START);
testField.add(textArea, BorderLayout.CENTER);
testField.add(btn, BorderLayout.LINE_END);
return testField;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
// Turn off metal's use of bold fonts
UIManager.put("swing.boldMetal", Boolean.FALSE);
JFrame frame = new JFrame();
JPanel scrollPane = new JScrollPaneExample(8);
frame.add(scrollPane);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(500,500));
frame.pack();
}
});
}
}
public类JScrollPaneExample扩展了JPanel{
公共JScrollPaneExample(int行计数){
setLayout(新的BorderLayout());
JScrollPane scrollPane=makeScrollPane(行数);
添加(滚动窗格,边框布局。页面开始);
设置首选尺寸(新尺寸(300300));
}
私有JScrollPane MakeCrollPane(int行计数){
JScrollPane scrollPane=新的JScrollPane();
JPanel窗格=新的JPanel();
setLayout(新的GridLayout(rowCount,0));
对于(int i=0;i
我尝试过退出浏览interwebz时发现的许多不同方法,例如设置scrollPanel的视口和其他很多东西,但都没有成功。要做到这一点,需要设置垂直滚动条的值。这是代码
public class JScrollPaneExample extends JPanel{
public JScrollPaneExample(int rowCount){
setLayout(new BorderLayout());
final JScrollPane scrollPane = makeScrollPane(rowCount);
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
scrollPane.getVerticalScrollBar().setValue(0);
}
});
add(scrollPane, BorderLayout.PAGE_START);
setPreferredSize(new Dimension(300,300));
}
private JScrollPane makeScrollPane(int rowCount) {
final JScrollPane scrollPane = new JScrollPane();
final JPanel pane = new JPanel();
pane.setLayout(new GridLayout(rowCount, 0));
for(int i = 0; i < rowCount; i++){
if(i != 4)
pane.add(makeTestField("Test " + (i+1), "Test description: " + (i+1), i));
else {
pane.add(makeTestField("This is a test",
"This is also a test to see if this field will do a line break. "
+ "This is also a test to see if this field will do a line break. "
+ "Something else could be entirely different...",
i));
}
}
scrollPane.getViewport().add(pane);
scrollPane.setPreferredSize(new Dimension(200, 400));
return scrollPane;
}
protected static JPanel makeTestField(String header, String description, int rowCount){
final JPanel testField = new JPanel();
testField.setLayout(new BorderLayout());
testField.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
final JLabel label = new JLabel(header);
final Font font = new Font("Serif", Font.BOLD, 18);
label.setFont(font);
final JButton btn = new JButton("Start");
final JTextArea textArea = new JTextArea(description);
textArea.setEditable(false);
textArea.setBackground(label.getBackground());
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
textArea.setBorder(BorderFactory.createEmptyBorder(0,0,0,5));
testField.add(label, BorderLayout.PAGE_START);
testField.add(textArea, BorderLayout.CENTER);
testField.add(btn, BorderLayout.LINE_END);
return testField;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
// Turn off metal's use of bold fonts
UIManager.put("swing.boldMetal", Boolean.FALSE);
final JFrame frame = new JFrame();
final JPanel scrollPane = new JScrollPaneExample(8);
frame.add(scrollPane);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(500,500));
frame.pack();
}
});
}
}
public类JScrollPaneExample扩展了JPanel{
公共JScrollPaneExample(int行计数){
setLayout(新的BorderLayout());
final JScrollPane scrollPane=makeScrollPane(行数);
SwingUtilities.invokeLater(新的Runnable(){
@凌驾
公开募捐{
scrollPane.getVerticalScrollBar().setValue(0);
}
});
添加(滚动窗格,边框布局。页面开始);
设置首选尺寸(新尺寸(300300));
}
私有JScrollPane MakeCrollPane(int行计数){
final JScrollPane scrollPane=新JScrollPane();
最终JPanel窗格=新JPanel();
setLayout(新的GridLayout(rowCount,0));
对于(int i=0;i