【问题标题】:How to wrap text around components in a JTextPane?如何在 JTextPane 中的组件周围环绕文本?
【发布时间】:2012-08-27 10:13:04
【问题描述】:

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

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

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

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);
    }
}

【问题讨论】:

    标签: java swing insert jtextpane jcomponent


    【解决方案1】:

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

    textPane.setContentType ( "text/html" );
    

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

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

    【讨论】:

    • 是的,它有效。为什么您认为使用 JTextPane 不是一个好主意?我想创建一个练习程序。练习必须包含组合框和文本字段。用户还必须能够“下划线”(选择)文本。如何以其他方式做到这一点?使用 JLabels 或禁用 JTextFields?你不能选择他们的文字,可以吗?即使你可以,我也认为每次你有一段文字时都添加这么多这样的元素是不好的。
    • @ka3ak 好吧,这可能是最简单的做事方式的少数情况之一 :)
    猜你喜欢
    • 1970-01-01
    • 2010-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多