Java Swing';s HTMLEditorKit删除跨度元素的嵌套
我正在使用Java Swing';s HTMLEditorKit删除跨度元素的嵌套,java,swing,jeditorpane,dom,htmleditorkit,Java,Swing,Jeditorpane,Dom,Htmleditorkit,我正在使用JTextPane,HTMLDocument 和HTMLEditorKit。代码如下所示: <html> <head> </head> <body> <p> <span title="tag:one">Outer span.</span> <span title="tag:two">Inner span.</span> <span ti
JTextPane
,HTMLDocument
和HTMLEditorKit
。代码如下所示:
<html>
<head>
</head>
<body>
<p>
<span title="tag:one">Outer span.</span> <span title="tag:two">Inner
span.</span> <span title="tag:one"> Outer span continued.</span>
</p>
</body>
</html>
公共类SimpleHTMLEditor扩展JFrame{
私有静态最终长serialVersionUID=1L;
私有最终JTextPane文本窗格;
私人最终HTMLEditorKit edtKit;
私人HTMLDocument文件;
公共静态void main(字符串[]args){
最终SimpleHTMLEditor编辑器=新SimpleHTMLEditor();
编辑器.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
editor.setVisible(true);
}
公共SimpleHTMLEditor(){
超级(“简单HTML编辑器”);
textPane=新的JTextPane();
edtKit=新的HTMLEditorKit();
textPane.setEditorKit(edtKit);
doc=新的HTMLDocument();
textPane.setDocument(doc);
最终容器内容=getContentPane();
添加(文本窗格,BorderLayout.CENTER);
添加(createToolBar(),BorderLayout.NORTH);
setJMenuBar(createMenuBar());
设置大小(500240);
textPane.requestFocusInWindow();
}
/**
*使用组合框创建工具栏,该组合框允许创建和
*使用不同的条件和各自的表达风格。
*@返回工具栏
*/
私有JToolBar createToolBar(){
最终JToolBar=新的JToolBar();
返回杆;
}
/**
*创建菜单栏。它包含:
*读/写HTML文件的操作
* 在弹出窗口中显示HTML源代码的操作。
*@返回菜单栏
*/
私有JMenuBar createMenuBar(){
最终JMenuBar菜单栏=新JMenuBar();
最终JMenu mnuFile=新JMenu(“文件”);
菜单栏添加(mnuFile);
最终保存操作actSave=新建保存操作();
mnuFile.add(actSave);
最终加载动作actLoad=新加载动作();
mnuFile.add(actLoad);
最终JMenuItem mnuPreview=新JMenuItem(“预览”);
菜单栏添加(mnuPreview);
mnuPreview.addActionListener(新ActionListener(){
@凌驾
已执行的公共无效行动(行动事件ae){
最终HTMLPreview预览器=新HTMLPreview(SimpleHTMLEditor.this,
getDocSource());
previewer.setVisible(true);
}
});
返回菜单栏;
}
/**
*帮助器方法从HTML文档中提取HTML源代码
*@返回HTML源代码
*/
私有字符串getDocSource(){
最终StringWriter sw=新StringWriter();
试一试{
write(sw,doc,0,doc.getLength());
}捕获(IOException | BadLocationException e1){
e1.printStackTrace();
}
试一试{
sw.close();
}捕获(最终IOE1异常){
e1.printStackTrace();
}
返回sw.toString();
}
类SaveAction扩展了AbstractAction{
私有静态最终长serialVersionUID=1L;
公共储蓄行动(){
超级(“保存到文件”);
}
@凌驾
已执行的公共无效操作(操作事件ev){
final JFileChooser chooser=新JFileChooser();
if(chooser.showsavedilog(SimpleHTMLEditor.this)!=JFileChooser.APPROVE\u选项)
返回;
最终文件targetFile=chooser.getSelectedFile();
if(targetFile==null)
返回;
FileWriter=null;
试一试{
writer=新文件编写器(targetFile);
textPane.write(writer);
}捕获(最终IOEX){
JOptionPane.showMessageDialog(SimpleHTMLEditor.this,
“文件未保存”,“错误”,
JOptionPane.ERROR\u消息);
}最后{
if(writer!=null){
试一试{
writer.close();
}捕获(最终IOX异常){
}
}
}
}
}
类LoadAction扩展了AbstractAction{
私有静态最终长serialVersionUID=1L;
公共加载操作(){
超级(“从文件加载”);
}
@凌驾
已执行的公共无效操作(操作事件ev){
final JFileChooser chooser=新JFileChooser();
if(chooser.showOpenDialog(SimpleHTMLEditor.this)!=JFileChooser.APPROVE\u选项)
返回;
最终文件sourceFile=chooser.getSelectedFile();
if(sourceFile==null)
返回;
FileReader=null;
试一试{
reader=新文件读取器(sourceFile);
doc=(HTMLDocument)edtKit.createDefaultDocument();
textPane.setDocument(doc);
读取(读卡器,文件,0);
}捕获(最终IOEX){
JOptionPane.showMessageDialog(SimpleHTMLEditor.this,
“文件”“+sourceFile.getAbsolutePath()+
“未加载”、“错误”,
JOptionPane.ERROR\u消息);
}捕获(最终错误定位异常e){
e、 printStackTrace();
}最后{
if(读卡器!=null){
试一试{
reader.close();
}捕获(最终IOX异常){
}
}
}
}
}
}
/**
*用于将编辑器的当前内容显示为HTML的弹出窗口
*源代码。
*/
类HTMLPreview扩展了JDialog{
私有静态最终长serialVersionUID=1L;
公共HTMLPreview(JFrame父对象,字符串so
<html>
<head>
</head>
<body>
<p>
<span title="tag:one">Outer span.</span> <span title="tag:two">Inner
span.</span> <span title="tag:one"> Outer span continued.</span>
</p>
</body>
</html>