scrollRectToVisible不会将滚动窗格移到顶部

时间:2019-04-13 13:19:23

标签: java jscrollpane

下面是我的代码的简化版本,以说明问题。我希望scrollRectToVisible将滚动条和滚动窗格移回顶部,但仍保留在底部。预先感谢您的任何建议。

package testing;

import javax.swing.SwingUtilities;

public class Testing {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            public void run(){
                new GUItest();
            }
        });
    }

}



package testing;

import java.awt.Dimension;
import java.awt.Rectangle;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JScrollPane;

public class GUItest extends JFrame  {
    private JEditorPane myEditorPane;
    private JScrollPane myScrollPane;

    public GUItest(){
        myEditorPane = new JEditorPane(); 
        myScrollPane = new JScrollPane(myEditorPane);
        myScrollPane.setPreferredSize(new Dimension(400, 200));
        getContentPane().add(myScrollPane);
        myEditorPane.setContentType("text/html");
        myEditorPane.setText("<html>" + "test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>" + "</html>");
        Rectangle rect = new Rectangle(1,1,1,1);
        myEditorPane.scrollRectToVisible(rect);
        pack();
        setVisible(true);

    }    
}

1 个答案:

答案 0 :(得分:1)

如果在int min(int x, int y, int z){ return min(min(x, y), z); // the compiler is getting confused over whether to //call std::min which takes two parameters or user-defined min which //takes three parameters } 可见后在“下一帧渲染周期”中进行滚动,则该方法有效:

JFrame

看起来import javax.swing.SwingUtilities; import java.awt.Dimension; import java.awt.Rectangle; import javax.swing.JEditorPane; import javax.swing.JFrame; import javax.swing.JScrollPane; public class GUItest extends JFrame { private JEditorPane myEditorPane; private JScrollPane myScrollPane; public GUItest(){ myEditorPane = new JEditorPane(); myScrollPane = new JScrollPane(myEditorPane); myScrollPane.setPreferredSize(new Dimension(400, 100)); getContentPane().add(myScrollPane); myEditorPane.setContentType("text/html"); myEditorPane.setText("<html>" + "test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>" + "</html>"); Rectangle rect = new Rectangle(1,1,1,1); pack(); setVisible(true); SwingUtilities.invokeLater(new Runnable() { public void run() { myEditorPane.scrollRectToVisible(rect); } }); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable(){ public void run(){ new GUItest(); } }); } } 必须有效/可见才能发出滚动命令。