Java 格式化HTML编辑工具包

Java 格式化HTML编辑工具包,java,html,swing,dom,htmleditorkit,Java,Html,Swing,Dom,Htmleditorkit,我正在尝试在JTextPane中进行一些基本的格式化。为此,我决定使用html(HTMLDocument和HTMLEditorKit) 下面是按钮侦听器的操作代码,该按钮应将所选文本设置为粗体 boldButton.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent arg0) {

我正在尝试在JTextPane中进行一些基本的格式化。为此,我决定使用html(HTMLDocument和HTMLEditorKit)

下面是按钮侦听器的操作代码,该按钮应将所选文本设置为粗体

boldButton.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent arg0) {

                    System.out.print(outputTextPane.getSelectedText());

                        int offset = outputTextPane.getSelectionStart();
                        int length = outputTextPane.getSelectionEnd()-outputTextPane.getSelectionStart();
                        String content = "";
                        try {
                            content = outputDoc.getText(offset, length);
                        } catch (BadLocationException e1) {
                            // TODO Auto-generated catch block
                            e1.printStackTrace();
                        }                   
                        try {
                            outputDoc.replace(offset, length, "", null);
                            outputKit.insertHTML(outputDoc, outputTextPane.getSelectionStart(), "<b>"+content+"</b>", 0, 0, HTML.Tag.B);

                        } catch (BadLocationException | IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }

            }

        });
boldButton.addActionListener(新ActionListener(){
@凌驾
已执行的公共无效操作(操作事件arg0){
System.out.print(outputTextPane.getSelectedText());
int offset=outputTextPane.getSelectionStart();
int length=outputTextPane.getSelectionEnd()-outputTextPane.getSelectionStart();
字符串内容=”;
试一试{
content=outputDoc.getText(偏移量,长度);
}捕获(BadLocationException e1){
//TODO自动生成的捕捉块
e1.printStackTrace();
}                   
试一试{
outputDoc.replace(偏移量,长度,“”,空);
outputKit.insertHTML(outputDoc,OutputExtPane.getSelectionStart(),“”+content+“”,0,0,HTML.Tag.B);
}捕获(BadLocationException | IOException e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
});
除非您尝试使粗体文本也加下划线(基本上是相同的动作侦听器),否则它会起作用。源代码如下所示:

text -> select "text" and press bold button
<b>text</b> -> select "x" and press underline button
<b>te</b><u>x</u><b>t</b>
text->选择“text”并按粗体按钮
文本->选择“x”并按下划线按钮
文本
下面是按钮侦听器的操作代码,该按钮应将所选文本设置为粗体

boldButton.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent arg0) {

                    System.out.print(outputTextPane.getSelectedText());

                        int offset = outputTextPane.getSelectionStart();
                        int length = outputTextPane.getSelectionEnd()-outputTextPane.getSelectionStart();
                        String content = "";
                        try {
                            content = outputDoc.getText(offset, length);
                        } catch (BadLocationException e1) {
                            // TODO Auto-generated catch block
                            e1.printStackTrace();
                        }                   
                        try {
                            outputDoc.replace(offset, length, "", null);
                            outputKit.insertHTML(outputDoc, outputTextPane.getSelectionStart(), "<b>"+content+"</b>", 0, 0, HTML.Tag.B);

                        } catch (BadLocationException | IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }

            }

        });
不要创造你自己的行为。使用编辑器工具包提供的操作。例如:

JButton bold = new JButton( new StyledEditorKit.BoldAction() );

谢谢,我真不敢相信解决问题这么容易。我花了大约3个小时来解决这个问题。。。