gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 01:52:19 25 4
gpt4 key购买 nike

下面是我的代码的简短版本,用于说明该问题。我希望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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com