如何在JTextPane中的组件周围包装文本? [英] How to wrap text around components in a JTextPane?

查看:123
本文介绍了如何在JTextPane中的组件周围包装文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不理解JTextPane中的包装行为。如果我插入一个短文本,然后是一个JComponent,然后再插入短文本,如果框架足够大,我可以在一行中看到插入的东西。但是如果文本更长,所以需要几行,组件总是放在一个新行中。

I don't understand the wrapping behavior in a JTextPane. If I insert a short text, then a JComponent and then again the short text I can see the inserted stuff in one line if the frame is large enough of course. But if the text is much longer so that it takes several lines the component is always placed in a new line.

我已经认识到在将一个组件插入一个组件之后JTextPane它的文本由一个字符变长。因此,如果一个组件被JTextPane视为一个字符,为什么它不像一个字符呢?可能它取决于Java版本?我使用 Java(TM)SE运行时环境(版本1.7.0-b147)

I have recognized that after a component has been inserted into a JTextPane its text gets longer by one character. So if a component is considered by a JTextPane as a character why doesn't it behave like a character? May it depend on the java version? I use Java(TM) SE Runtime Environment (build 1.7.0-b147)

下面是我的代码(使用shortText实例化变量currentText / longText重现上述行为):

Below is my code (instantiate the variable currentText with shortText/longText to reproduce the mentioned behavior):

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.SimpleAttributeSet;

public class Test {
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        JTextPane textPane = new JTextPane();
        textPane.setContentType("text/html");

        String shortText = "one two three four five six seven";
        String longText = "A text component that can be marked up with attributes that are represented graphically. You can find how-to information and examples of using text panes in Using Text Components, a section in The Java Tutorial. This component models paragraphs that are composed of runs of character level attributes. Each paragraph may have a logical style attached to it which contains the default attributes to use if not overridden by attributes set on the paragraph or character run. Components and images may be embedded in the flow of text.";
        String currentText = shortText;

        try {
            // insert text before the component
            textPane.getDocument().insertString(textPane.getDocument().getLength(), currentText,
                    new SimpleAttributeSet());

            textPane.setSelectionStart(textPane.getDocument().getLength());
            textPane.setSelectionEnd(textPane.getDocument().getLength());

            JComboBox component = new JComboBox();
            component.setMaximumSize(component.getPreferredSize());
            textPane.insertComponent(component);

            // insert text after the component
            textPane.getDocument().insertString(textPane.getDocument().getLength(), currentText,
                    new SimpleAttributeSet());

        } catch (BadLocationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        textPane.setEditable(false);

        frame.add(new JScrollPane(textPane));
        frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}


推荐答案

那由于您设置的内容类型,似乎会发生奇怪的行为。尝试删除此行:

That strange behavior seems to happen due to the content type you set. Try removing this line:

textPane.setContentType ( "text/html" );

您将看到之后一切正常。我不确定它为什么会发生 - 可能是某些渲染错误或只是预期的行为。

and you will see that everything works fine after that. I am not sure why it happens - might be either some rendering bug or just an intended behavior.

P.S。我不认为在文本窗格中使用Swing组件(无论原因是什么)是一个不错的选择。但这只是我的意见......

P.S. I don't think that using Swing components inside text pane (whatever the reason is) is a good option. But that is just my opinion...

这篇关于如何在JTextPane中的组件周围包装文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆