java - JTextPane 中的双列表项插入

标签 java html swing jtextpane htmleditorkit

我有一个按钮,可以将无序列表项插入到 JTextPane 中.但是,当我单击按钮插入列表项时,插入了两个项目符号而不是一个。仅在第一次插入时插入一颗子弹。

我从我的应用程序中删除了功能并将代码粘贴到一个小的 SSCCE(如下)中,但问题仍然存在。有人知道这里会发生什么吗?

[问题已解决,下面是完整的解决代码。有两种方法可以做到这一点,引用节目中的功能和项目符号按钮]

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.Element;
import javax.swing.text.ElementIterator;
import javax.swing.text.html.HTML;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLEditorKit;

public class Main {

    private static Button2 show = new Button2 ("Show"); 
    private static LIButton bullets = new LIButton("Bullets", HTML.Tag.UL);
    private static JEditorPane pane = new JEditorPane();

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                create();
            }
        });
    }

    private static void create() throws HeadlessException {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pane.setPreferredSize(new Dimension(300, 300));
        pane.setContentType("text/html");
        frame.add(pane, BorderLayout.CENTER);
        JPanel panel = new JPanel();
        panel.add(bullets);
        panel.add(show);
        frame.add(panel, BorderLayout.SOUTH);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    static class LIButton extends JButton {

        static final String LI_HTML = "<HTML><BODY><UL><LI></LI></UL></BODY></HTML>";

        public LIButton(String name, HTML.Tag parent) {
            super(new HTMLEditorKit.InsertHTMLTextAction(
                name, LI_HTML, HTML.Tag.UL, HTML.Tag.LI, HTML.Tag.BODY, HTML.Tag.UL));
        }
    }

    static class Button2 extends JButton implements ActionListener {

        static final String LI_HTML = "<HTML><BODY><UL><LI></LI></UL></BODY></HTML>";

        public Button2(String name) {
            super(name);
            this.addActionListener(this);
        }

        @Override
        public void actionPerformed(ActionEvent ae) {
            HTMLDocument doc = (HTMLDocument) pane.getDocument();
            HTMLEditorKit kit = (HTMLEditorKit) pane.getEditorKit();

            try {
                kit.insertHTML(doc, doc.getLength() - 1, LI_HTML, 0, 1, null);
            } catch (BadLocationException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}

最佳答案

下面的例子似乎可行。

enter image description here

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.HeadlessException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextPane;
import javax.swing.text.html.HTML;
import javax.swing.text.html.HTMLEditorKit;

public class Main {

    private static LIButton bullets = new LIButton("Bullets", HTML.Tag.UL);
    private static JTextPane pane = new JTextPane();

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                create();
            }
        });
    }

    private static void create() throws HeadlessException {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pane.setPreferredSize(new Dimension(300, 300));
        pane.setContentType("text/html");
        pane.setText("<HTML><BODY><UL></UL></BODY></HTML>");
        frame.add(pane, BorderLayout.CENTER);
        JPanel panel = new JPanel();
        panel.add(bullets);
        frame.add(panel, BorderLayout.SOUTH);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    static class LIButton extends JButton {

        static final String LI_HTML = "<LI>item</LI>";

        public LIButton(String name, HTML.Tag parent) {
            super(new HTMLEditorKit.InsertHTMLTextAction(
                name, LI_HTML, parent, HTML.Tag.LI));
        }
    }
}

关于java - JTextPane 中的双列表项插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8570673/

相关文章:

java - 加密/解密(使用 AES)大文件并通过 HTTP 传输

java - 如何在 gradle bootRun 结束时运行附加任务

java - JPA/hibernate 错误违反外键约束

html - 在 HTML/CSS 中维护大小和滚动嵌套表格

java - 将 JComponent 添加到小程序的 Pane 中

java - 在 Eclipse IDE 中播放音频文件;但不是 JAR 文件

java - Jenkins 工作状态的详尽列表?

javascript - 仪表板仅更改内容而无需重新加载整页

css - div 填充剩余高度 css

java - 使用swing在java中为JButton创建热键