Java - 将 JEditorPane 添加到 JScrollPane 中并开始滚动到最底部
Java - Add JEditorPane into a JScrollPane and scroll to the very bottom initially
我有一个使用 "text/html" 类型的 JEditorPane,我已将 editorPane 添加到 JScrollPane。一切都很好,除了它在启动时显示 JEditorPane 的最底部。我想默认显示最顶部。
这是我的代码:
public class DisclaimerPage extends JPanel {
private static final String DISCLAIMER_CONTENT =
"<h2>This is a H2 header</h2>" + //Bold first line (Title)
"<p> This is the first paragraph having many lines of text. Text Text Text"
+ "Text Text TextText Text TextText Text TextText Text TextText Text TextText Text Text</p>" //Content
+ "<p> <b>Bold Second Paragraph</b>: there will be N number of paragraph after this.</p>"
+ "<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>"
+ "<p> Last Paragraph </p>";
private JScrollPane disclaimerScrollPane;
private JEditorPane disclaimerContentPane;
public DisclaimerPage() {
setLayout(new BorderLayout(0, 15));
disclaimerContentPane = new JEditorPane();
disclaimerContentPane.setEditable(false);
disclaimerContentPane.setContentType("text/html");
disclaimerContentPane.setText(DISCLAIMER_CONTENT);
disclaimerScrollPane = new JScrollPane(disclaimerContentPane);
disclaimerScrollPane.setPreferredSize(new Dimension(480, 360));
this.add(disclaimerScrollPane, BorderLayout.CENTER);
}
}
通过使用
disclaimerContentPane.setCaretPosition(0);
可以解决这个问题
我有一个使用 "text/html" 类型的 JEditorPane,我已将 editorPane 添加到 JScrollPane。一切都很好,除了它在启动时显示 JEditorPane 的最底部。我想默认显示最顶部。
这是我的代码:
public class DisclaimerPage extends JPanel {
private static final String DISCLAIMER_CONTENT =
"<h2>This is a H2 header</h2>" + //Bold first line (Title)
"<p> This is the first paragraph having many lines of text. Text Text Text"
+ "Text Text TextText Text TextText Text TextText Text TextText Text TextText Text Text</p>" //Content
+ "<p> <b>Bold Second Paragraph</b>: there will be N number of paragraph after this.</p>"
+ "<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>"
+ "<p> Last Paragraph </p>";
private JScrollPane disclaimerScrollPane;
private JEditorPane disclaimerContentPane;
public DisclaimerPage() {
setLayout(new BorderLayout(0, 15));
disclaimerContentPane = new JEditorPane();
disclaimerContentPane.setEditable(false);
disclaimerContentPane.setContentType("text/html");
disclaimerContentPane.setText(DISCLAIMER_CONTENT);
disclaimerScrollPane = new JScrollPane(disclaimerContentPane);
disclaimerScrollPane.setPreferredSize(new Dimension(480, 360));
this.add(disclaimerScrollPane, BorderLayout.CENTER);
}
}
通过使用
disclaimerContentPane.setCaretPosition(0);
可以解决这个问题