久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何在 JTextPane 中的組件周圍環(huán)繞文本?

How to wrap text around components in a JTextPane?(如何在 JTextPane 中的組件周圍環(huán)繞文本?)
本文介紹了如何在 JTextPane 中的組件周圍環(huán)繞文本?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我不理解 JTextPane 中的包裝行為.如果我插入一個短文本,然后是一個 JComponent,然后再插入一個短文本,如果框架足夠大,我可以在一行中看到插入的內(nèi)容.但是如果文本更長以至于需要多行,組件總是被放置在一個新的行中.

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.

我已經(jīng)認識到,在將組件插入 JTextPane 后,它的文本會變長一個字符.因此,如果一個組件被 JTextPane 視為一個字符,為什么它的行為不像一個字符?可能取決于java版本嗎?我使用 Java(TM) SE 運行時環(huán)境(內(nèi)部版本 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/longText 實例化變量 currentText 以重現(xiàn)上述行為):

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

推薦答案

由于您設置的內(nèi)容類型,這種奇怪的行為似乎發(fā)生了.嘗試刪除此行:

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

textPane.setContentType ( "text/html" );

然后您會發(fā)現(xiàn)一切正常.我不確定它為什么會發(fā)生 - 可能是一些渲染錯誤或只是預期的行為.

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.

附:我不認為在文本窗格中使用 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...

這篇關(guān)于如何在 JTextPane 中的組件周圍環(huán)繞文本?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

MyBatis, how to get the auto generated key of an insert? [MySql](MyBatis,如何獲取插入的自動生成密鑰?[MySql])
Inserting to Oracle Nested Table in Java(在 Java 中插入 Oracle 嵌套表)
Java: How to insert CLOB into oracle database(Java:如何將 CLOB 插入 oracle 數(shù)據(jù)庫)
Why does Spring-data-jdbc not save my Car object?(為什么 Spring-data-jdbc 不保存我的 Car 對象?)
Use threading to process file chunk by chunk(使用線程逐塊處理文件)
URL Connection (FTP) in Java - Simple Question(Java 中的 URL 連接 (FTP) - 簡單問題)
主站蜘蛛池模板: 婷婷久久综合 | 欧美激情综合网 | 日韩欧美在线观看 | 欧美午夜精品 | 久久亚洲一区二区 | 日韩精品一区二区三区在线播放 | 欧美专区日韩专区 | 伊人免费在线观看 | 中国一级毛片免费 | 视频在线观看一区 | 久久久久午夜 | 精品成人免费一区二区在线播放 | 就操在线 | 九九热这里只有精品在线观看 | 日韩在线高清 | 欧美日韩国产中文字幕 | 亚洲精品成人av | 亚洲人在线 | 国产在线精品一区二区三区 | 国产成人精品一区二区三区网站观看 | 日韩国产一区二区三区 | 亚洲午夜av| 欧美精品成人一区二区三区四区 | 久久久久国产一区二区三区四区 | 国产高清视频在线观看 | 欧美激情亚洲激情 | 久久国产精品无码网站 | 亚洲精品久久久久久一区二区 | 视频1区2区| 日本中文字幕日韩精品免费 | 91精品国产91久久久久久丝袜 | 国产三区精品 | 欧美色性 | 久久99精品久久久久久噜噜 | 亚洲综合中文字幕在线观看 | 欧美一级做a爰片免费视频 国产美女特级嫩嫩嫩bbb片 | 久久久久久久久久久久久九 | 成人在线精品视频 | 日日摸夜夜添夜夜添特色大片 | 一区二区三区四区在线视频 | 亚洲一区二区三区免费在线观看 |