【问题标题】:scrollRectToVisible doesn't move scrollpane to topscrollRectToVisible 不会将滚动窗格移动到顶部
【发布时间】:2019-04-13 13:19:23
【问题描述】:

下面是我的代码的简化版本来说明问题。我希望 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);
    }
}

【问题讨论】:

    标签: java swing jscrollpane


    【解决方案1】:

    如果您在 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 必须有效/可见才能发出滚动命令。

    【讨论】:

    • 谢谢,是的,它确实有效并解决了我的主程序中的问题。我不完全理解它为什么起作用,因为在我的主程序中,在我调用 scrollRectToVisible 之前 JFrame 已经有效且可见。无论如何,谢谢,我花了很长时间试图解决这个问题。
    猜你喜欢
    • 2011-07-12
    • 1970-01-01
    • 2021-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多