在JTextArea或JTextPane中设置文本样式 [英] Styling text in a JTextArea or JTextPane

查看:105
本文介绍了在JTextArea或JTextPane中设置文本样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我有一个JTextPane来保存一些我想要设计的文本。 JTextArea对我来说会更好用,但我被告知不能在这些文本中设置样式?

然而,JTextPane似乎没有适当的样式。例如,下面的代码仅仅包含HTML返回:

  public static void main(String [] args){
JFrame j =新的JFrame(Hello!);
j.setSize(200,200);
JTextPane k = new JTextPane();
k.setText(< html>< strong>嘿!< / strong>< / html>);
j.add(k);
j.setVisible(true);
}

当用户想要在JTextPane中设置样式时,与界面交互,但到目前为止,它只是返回字符串仍然在HTML!帮助!

解决方案

如果您想在<一个href =http://docs.oracle.com/javase/tutorial/uiswing/components/editorpane.html =nofollow noreferrer> JTextPane ,那么你必须设置为 JTextPane#setContentType(text / html ); ,示例这里



编辑:



用于 JEditorPanes / JTextPanes

code>是否有另一种实现方法 StyledDocument < a>, MutableAttribute设置并自定义 Highlighte r,例如此处



am方法是不使用 Html语法


Basically, I have a JTextPane to hold some text which I wish to style. JTextArea would have been better for me to use but I'm told you cannot style the text in these?

However, the JTextPane doesn't seem to style properly. For example, the following code is just returned with the HTML included:

public static void main(String[] args) {
    JFrame j = new JFrame("Hello!");
    j.setSize(200,200);
    JTextPane k = new JTextPane();
    k.setText("<html><strong>Hey!</strong></html>");
    j.add(k);
    j.setVisible(true);
}

I want to be able to just style some text in a JTextPane when a user interacts with the interface, but so far, it just returns the string with the HTML still in! Help!

解决方案

If you want to diplaying Html contents in the JTextPane then you have to set for JTextPane#setContentType("text/html");, example here

EDIT:

for JEditorPanes / JTextPanes is there another way by implements StyledDocument, MutableAttributeSet and with customized Highlighter, example here

a.m. way is without using Html syntax

这篇关于在JTextArea或JTextPane中设置文本样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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