功能是“每当我按下按钮时,子弹应该出现在文本窗格中”,但我面临的问题是每当我在子弹前写一个文本然后按下按钮时出现两个子弹而不是一个。
我正在粘贴代码供您参考。
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.SwingUtilities;
import javax.swing.text.AbstractDocument;
import javax.swing.text.html.HTML;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLEditorKit;
public class HTMLDocumentEditor {
JTextPane textPane;
AbstractDocument doc;
public JPanel createPanel(){
JPanel panel = new JPanel();
HTMLDocument document = new HTMLDocument();
textPane = new JTextPane(document);
HTMLEditorKit.InsertHTMLTextAction bulletAction = new HTMLEditorKit.
InsertHTMLTextAction("Bullets", "<li> </li>",HTML.Tag.BODY,HTML.Tag.UL);
JButton bulletButton = new JButton(bulletAction);
bulletButton.setText("Bullet");
textPane.setContentType("text/html");
panel.add(bulletButton,BorderLayout.PAGE_START);
JScrollPane scrollPane = new JScrollPane(textPane);
scrollPane.setPreferredSize(new Dimension(200, 200));
panel.add(scrollPane, BorderLayout.CENTER);
HTMLEditorKit editorKit = new HTMLEditorKit();
textPane.setEditorKit(editorKit);
return panel;
}
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
HTMLDocumentEditor edit = new HTMLDocumentEditor();
frame.add(edit.createPanel());
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event dispatch thread:
//creating and showing this application's GUI.
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
感谢。