setContentType(“text/html”)
JTextPane支持html3.2 , 可通过 setContentType(“text/html”) 开启
JTextPane 默认的 ContentType是text/plain , 可用通过jTextPane.setContentType(“text/html”); 使其支持html
style标签要写到head里面才有用 , 不支持fieldset,legend,button,label
使用html后, setFont(font)无效;
JTextPane在text/plain时会自动换行, 在text/html时不会自动换行
JTextPane 和 JTextArea 的区别
在普通文本 text/plain 时 JTextArea不会自动换行, JTextPane会自动换行
JTextPane继承了JEditorPane的setContentType方法 , JTextArea没有
继承关系: JTextPane ← JEditorPane ← JTextComponent → JTextArea
测试代码
package panePanel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class JTextPane2205311024 {
static JFrame frame = new JFrame("JTextPaneTest");
static JTextPane jTextPane = new JTextPane();
static JScrollPane jScrollPane = new JScrollPane(jTextPane);
static {
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setBounds(100,50,1600,900);
jScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
jScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
jTextPane.setEditable(true);
System.out.println(jTextPane.getContentType());
jTextPane.setContentType("text/html");
jTextPane.setText("""
<html><head><meta charset="utf-8"/> <title>title</title>
<style>
b , big{font-size:25px;}
#unknowtag , unknowtag{background-color:blue;}
#span010 , .FontBlue{ color:blue; border:1px solid black;}
#div060 span{font-size:20px; color:green;}
#div060>div{font-size:20px; color:green;}
#div060> div{font-size:20px; color:green;}
#div060 > div{font-size:20px; color:green;}
</style>
</head><body style="display:absolute; margin:30px; font-family:楷体; ">
<div >
<ul><h1>JTextPane支持html3.2 , 可通过 <b style="font-family:consolas; font-size:30px; color:red;"> setContentType("text/html") </b> 开启
<li>JTextPane 默认的 ContentType是text/plain , 可用通过jTextPane<B>.setContentType("text/html");</B> 使其支持html
<li>style标签要写到head里面才有用 , 不支持fieldset,legend,button,label
<li style="font-family:segoe script; font-size:20px; ">使用html后, setFont(font)无效;
<li>使用html后, setFont(font)无效;
<li>JTextPane在text/plain时会自动换行, 在text/html时不会自动换行
<li>div可以设置border, span设置border无效
<li>支持<h1>~<h6>标签
</ul>
</div>
<div>
<ul>
<h1>JTextPane 和 JTextArea 的区别
<li>在普通文本 text/plain 时 JTextArea不会自动换行, JTextPane会自动换行
<li>JTextPane继承了JEditorPane的setContentType方法 , JTextArea没有
<li>继承关系: JTextPane ← JEditorPane ← <i style="font-size:20px;">JTextComponent</i> → JTextArea <br/>
<span style="font-size:16px; color:#0099ff;">JTextPane</span> ← JEditorPane ← <span style="font-size:20px; border:1px solid #0099ff;">JTextComponent</span> → <span style="font-size:16px; color:#0099ff;">JTextArea</span>
</ul>
</div>
<hr/>
<div style="height:0px; border:1px solid black;" />div可以设置width和height, 但没法height:0px, 有最小高度</div>
<h1>测试一些html标签能不能用</h1>
<div style="position:fixed; left:100px; height:100px; display:inline; width:2600px; height:100px; font-size:14px; color:white; background:black;
border:3px dotted rgb(0,255,188); border-radius:50%;
">这是一个用于测试的div标签, position不起作用, display不起作用, border-radius不起作用</div>
<h2>测试一些input标签能不能用</h2>
<input value="text1" />
<input type="text" value="text2" />
<input type="password" value="password" />
<input type="file" value="file" />
<input type="color" />
<input type="number" value="123" />
<input type="radio" value="button" />
<input type="checkbox" value="button" />
<input type="reset" value="reset重置" />
<input type="submit" value="submit提交" />
<input type="button" value="button" />
<input type="date" value="" />
<input type="time" value="" />
<input type="datetime" value="" />
<input type="datetime-local" value="" />
<input type="week" value="" />
<ol>
<li>text,password起作用,number都不起作用,file却起作用, 不支持color调色板
<li>radio,checkbox起作用
<li>reset,submit起作用 , button不起作用
<li>不支持 date , time , datetime , datetime-local , week
</ol>
<center>这是一个center标签</center>
<em>这是一个em标签</em>
<strong>这是一个strong标签</strong>
<button>不支持button标签</button>
<unknowtag id="unknowtag">未知标签会显示成这个样子</unknowtag>
<span id="span010" >span010 用#id设置style</span> <span style="display:block;">span020 display:block无效</span>
<span class="FontBlue" >span030 通过.class捕获设置颜色为Blue</span>
<div id="div060">
<span>div下的span , 通过#div060 span{font-size:20px; color:green;}设置了style, 大于号>直接子集不起作用</span>
<div>div下的div , 通过#div060>div{font-size:20px; color:green;}设置了style不起作用
</div>
<p>
这是用p标签显示的一段文本
,不加br不会换行
<br/>轻轻的,你来了,\r\n不带走三叉戟
<br/>像一坨铁 , 风看不到他的样子
</p>
<canvas>不支持canvas</canvas>
<svg>不支持svg</svg>
<h1>h1</h1>
<h2>h2</h2>
<h3>h3</h3>
<h4>h4</h4>
<h5>h5</h5>
<h6>h6</h6>
<h7>h7</h7>
</body></html>
""");
jTextPane.setFont(new Font("宋体", Font.PLAIN, 33)); // html方式下, font无效
JPopupMenu jpm = new JPopupMenu("JPopupMenu");
jpm.setToolTipText("ToolTipText");
JMenuItem copy = new JMenuItem("copy");
copy.addActionListener(ev->{ jTextPane.copy();});
JMenuItem paste = new JMenuItem("paste");
paste.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jTextPane.paste();
}
});
jpm.add(copy);
jpm.add(paste);
jTextPane.setComponentPopupMenu(jpm);
frame.add(jScrollPane);
frame.setVisible(true);
}
public static void main(String...arguments) {}
}