JTextPane:将其添加到布局后如何更新它?

发布于 2025-01-02 00:43:53 字数 1492 浏览 1 评论 0原文

我之前发布过与此类似的问题,但无论如何,我想这更好地解释了我的问题。请参阅此链接。如果您注意到,所有示例都表明您的文本窗格内容必须先准备好,然后才能添加到内容窗格!为什么会这样呢?

例如,这段代码:

    public class PaneInsertionMethods {
  public static void main(String[] args) {
    final JTextPane pane = new JTextPane();
    pane.replaceSelection("text");
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(pane, BorderLayout.CENTER);
    frame.setSize(360, 180);
    frame.setVisible(true);
  }
}

效果很好。但是,如果我尝试做这样的事情:

public class PaneTest extends JFrame {
private JTextPane pane;
public PaneTest() {
   initComponents();
}
private void initComponents() {
    pane = new JTextPane();
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    getContentPane().add(pane, BorderLayout.CENTER);
    setSize(360, 180);
    setVisible(true);
}
public void populatePane() {
    pane.replaceSelection("text");  
   //or something like this.. doesn't work
   //pane.revalidate(); pane.repaint();

}
public static void main(String args[]) {
    PaneTest test = new PaneTest();
    test.populatePane();
    //or even something like this doesn't work:
    //SwingUtilities.invokeLater(new Runnable() {
    // public void run() {
    //     test.populatePane();
    //   }});
}}

我在第二个示例中看到的只是一个空的文本窗格。我做错了什么?

I've posted a question similar to this before, but anyway, I guess this explains my problem better. Please refer to this link. If you notice, all examples suggest that your textpane content has to prepared before it is added to the content pane! Why is this so?

For instance, this piece of code:

    public class PaneInsertionMethods {
  public static void main(String[] args) {
    final JTextPane pane = new JTextPane();
    pane.replaceSelection("text");
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(pane, BorderLayout.CENTER);
    frame.setSize(360, 180);
    frame.setVisible(true);
  }
}

works well. But, if I try to do something like this:

public class PaneTest extends JFrame {
private JTextPane pane;
public PaneTest() {
   initComponents();
}
private void initComponents() {
    pane = new JTextPane();
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    getContentPane().add(pane, BorderLayout.CENTER);
    setSize(360, 180);
    setVisible(true);
}
public void populatePane() {
    pane.replaceSelection("text");  
   //or something like this.. doesn't work
   //pane.revalidate(); pane.repaint();

}
public static void main(String args[]) {
    PaneTest test = new PaneTest();
    test.populatePane();
    //or even something like this doesn't work:
    //SwingUtilities.invokeLater(new Runnable() {
    // public void run() {
    //     test.populatePane();
    //   }});
}}

All I get to see is a empty textpane in the second example. What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尘曦 2025-01-09 00:43:53

我只是发现当文本窗格不可编辑时,replaceSelection 不会插入文本(在我的情况下它是不可编辑的!) - 我不敢相信我读了 50 遍关于此的 javadoc,但我一直错过了这一行。无论如何,现在问题解决了!

I just figured that replaceSelection doesn't insert text when the textpane is not editable (in my case it was not editable!) - I can't believe I read the javadoc on this like 50 times, but I kept missing that one line. Anyway, problem solved now!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文