pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

如何在 JTextPane 中的組件周圍環繞文本?

How to wrap text around components in a JTextPane?(如何在 JTextPane 中的組件周圍環繞文本?)
本文介紹了如何在 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/longText 實例化變量 currentText 以重現上述行為):

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.

附:我不認為在文本窗格中使用 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 中的組件周圍環繞文本?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

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 數據庫)
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) - 簡單問題)
主站蜘蛛池模板: 青岛空压机,青岛空压机维修/保养,青岛空压机销售/出租公司,青岛空压机厂家电话 | 压滤机-洗沙泥浆处理-压泥机-山东创新华一环境工程有限公司 | 生态板-实木生态板-生态板厂家-源木原作生态板品牌-深圳市方舟木业有限公司 | 渗透仪-直剪仪-三轴仪|苏州昱创百科 | 玉米加工设备,玉米深加工机械,玉米糁加工设备.玉米脱皮制糁机 华豫万通粮机 | 尚为传动-专业高精密蜗轮蜗杆,双导程蜗轮蜗杆,蜗轮蜗杆减速机,蜗杆减速机生产厂家 | 沈阳缠绕膜价格_沈阳拉伸膜厂家_沈阳缠绕膜厂家直销 | 无味渗透剂,泡沫抑尘剂,烷基糖苷-威海威能化工有限公司 | 高中学习网-高考生信息学习必备平台| 北京企业宣传片拍摄_公司宣传片制作-广告短视频制作_北京宣传片拍摄公司 | 深圳诚暄fpc首页-柔性线路板,fpc柔性线路板打样生产厂家 | 丹尼克尔拧紧枪_自动送钉机_智能电批_柔性振动盘_螺丝供料器品牌 | 河南不锈钢水箱_地埋水箱_镀锌板水箱_消防水箱厂家-河南联固供水设备有限公司 | 全自动包装秤_全自动上袋机_全自动套袋机_高位码垛机_全自动包装码垛系统生产线-三维汉界机器(山东)股份有限公司 | 深圳天际源广告-形象堆头,企业文化墙,喷绘,门头招牌设计制作专家 | 视觉检测设备_自动化检测设备_CCD视觉检测机_外观缺陷检测-瑞智光电 | 仪器仪表网 - 永久免费的b2b电子商务平台 | 学叉车培训|叉车证报名|叉车查询|叉车证怎么考-工程机械培训网 | 苏州工作服定做-工作服定制-工作服厂家网站-尺品服饰科技(苏州)有限公司 | 神马影院-实时更新秒播| 胶原检测试剂盒,弹性蛋白检测试剂盒,类克ELISA试剂盒,阿达木单抗ELISA试剂盒-北京群晓科苑生物技术有限公司 | 福兰德PVC地板|PVC塑胶地板|PVC运动地板|PVC商用地板-中国弹性地板系统专业解决方案领先供应商! 福建成考网-福建成人高考网 | 桑茶-七彩贝壳桑叶茶 长寿茶| 郑州大巴车出租|中巴车租赁|旅游大巴租车|包车|郑州旅游大巴车租赁有限公司 | VI设计-LOGO设计公司-品牌设计公司-包装设计公司-导视设计-杭州易象设计 | 桑茶-七彩贝壳桑叶茶 长寿茶| 不锈钢/气体/液体玻璃转子流量计(防腐,选型,规格)-常州天晟热工仪表有限公司【官网】 | 超声波焊接机,振动摩擦焊接机,激光塑料焊接机,超声波焊接模具工装-德召尼克(常州)焊接科技有限公司 | 网络推广公司_网络营销方案策划_企业网络推广外包平台-上海澜推网络 | ★塑料拖链__工程拖链__电缆拖链__钢制拖链 - 【上海闵彬】 | 宁夏活性炭_防护活性炭_催化剂载体炭-宁夏恒辉活性炭有限公司 | 裹包机|裹膜机|缠膜机|绕膜机-上海晏陵智能设备有限公司 | 塑胶跑道施工-硅pu篮球场施工-塑胶网球场建造-丙烯酸球场材料厂家-奥茵 | 塑料瓶罐_食品塑料瓶_保健品塑料瓶_调味品塑料瓶–东莞市富慷塑料制品有限公司 | 小型数控车床-数控车床厂家-双头数控车床| 立刷【微电签pos机】-嘉联支付立刷运营中心 | 网带通过式抛丸机,,网带式打砂机,吊钩式,抛丸机,中山抛丸机生产厂家,江门抛丸机,佛山吊钩式,东莞抛丸机,中山市泰达自动化设备有限公司 | 定制奶茶纸杯_定制豆浆杯_广东纸杯厂_[绿保佳]一家专业生产纸杯碗的厂家 | 【法利莱住人集装箱厂家】—活动集装箱房,集装箱租赁_大品牌,更放心 | 内窥镜-工业内窥镜厂家【上海修远仪器仪表有限公司】 | 高压无油空压机_无油水润滑空压机_水润滑无油螺杆空压机_无油空压机厂家-科普柯超滤(广东)节能科技有限公司 |