java - scrollRectToVisible 不会将滚动 Pane 移动到顶部

标签 java swing jscrollpane

下面是我的代码的简短版本,用于说明该问题。我希望scrollRectToVisible 能够将滚动条和滚动 Pane 移回顶部,但它仍然位于底部。预先感谢您的任何建议。

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);
    }
}

最佳答案

如果在 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();
            }
        });
    }
}

看起来JFrame必须有效/可见才能发出滚动命令。

关于java - scrollRectToVisible 不会将滚动 Pane 移动到顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55665903/

相关文章:

java - 检测 JSON 属性的类型

java - setMnemonic() 并通过按键调用方法

java - 当滚动条位于底部时将其滚动到底部

java - 如何删除文本文件中的特定字符串?

java - 根据分辨率更改 3D 模型纹理

java - 在 Java 构建路径上找不到 "c:url"(org.apache.taglibs.standard.tag.rt.core.UrlTag) 的标记处理程序类

java - 我可以在 Java 中设置一个窗口,使其具有统一的大小调整(即正方形)吗?

java - 通过 JTextField 快速搜索 JTable 中的批量数据

java - 在 java Swing 中使用特定 JButtons 在容器上垂直滚动

Java JTable 列标题未显示 - JScrollPane?