scrollRectToVisible 不会将滚动窗格移动到顶部
scrollRectToVisible doesn't move scrollpane to top
下面是我的代码的简化版本,用于说明问题。我希望 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);
}
}
如果在 JFrame
可见后滚动 "in the next frame render cycle",它会起作用:
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
必须是 valid/visible 才能发出滚动命令。
下面是我的代码的简化版本,用于说明问题。我希望 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);
}
}
如果在 JFrame
可见后滚动 "in the next frame render cycle",它会起作用:
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
必须是 valid/visible 才能发出滚动命令。