Java 使用kit.insertHTML()更新时,jTextPane中的第一个单词消失
我做了一个拼写检查之类的东西。在此代码中,当用户单击链接时,将显示一个选项列表,当选择某个选项时,超链接中的文本将更新。 我的问题是,当我更新第一个单词时,它就消失了。我正在为它编写一个示例工作代码。“链接”一词是可点击的Java 使用kit.insertHTML()更新时,jTextPane中的第一个单词消失,java,swing,jtextpane,htmleditorkit,Java,Swing,Jtextpane,Htmleditorkit,我做了一个拼写检查之类的东西。在此代码中,当用户单击链接时,将显示一个选项列表,当选择某个选项时,超链接中的文本将更新。 我的问题是,当我更新第一个单词时,它就消失了。我正在为它编写一个示例工作代码。“链接”一词是可点击的 import java.awt.ComponentOrientation; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.IOException;
import java.awt.ComponentOrientation;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JEditorPane;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.event.HyperlinkEvent;
import javax.swing.text.BadLocationException;
import javax.swing.text.html.HTML;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLEditorKit;
public class TextPaneTesting1 extends javax.swing.JFrame {
/**
* Creates new form TextPaneTesting
*/
static HTMLEditorKit kit = new HTMLEditorKit();
static HTMLDocument doc = new HTMLDocument();
static String mainText;
public TextPaneTesting1() throws IOException {
initComponents();
jTextPane1.setEditorKit(kit);
jTextPane1.setDocument(doc);
mainText = "<a href = '#'>Link</a> NotLink <a href = '#'>Link</a> NotLink <a href = '#'>Link</a>.";
jTextPane1.setEditable(false);
jTextPane1.setText(mainText);
jTextPane1.setCaretPosition(0);
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
jTextPane1 = new javax.swing.JTextPane();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jTextPane1.setContentType("text/UTF-8"); // NOI18N
jTextPane1.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
jTextPane1.addHyperlinkListener(new javax.swing.event.HyperlinkListener() {
public void hyperlinkUpdate(javax.swing.event.HyperlinkEvent evt) {
jTextPane1HyperlinkUpdate(evt);
}
});
jScrollPane1.setViewportView(jTextPane1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 613, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
private void jTextPane1HyperlinkUpdate(javax.swing.event.HyperlinkEvent evt) {
if(evt.getEventType() == HyperlinkEvent.EventType.ACTIVATED){
int begin = evt.getSourceElement().getStartOffset();
int end = evt.getSourceElement().getEndOffset();
popup = new JPopupMenu();
JMenuItem listOption = new JMenuItem("Click It");
listOption.setFont(new java.awt.Font("Tahoma", java.awt.Font.PLAIN, 12));
listOption.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
try {
updateText(begin, end);
} catch (IOException ex) {
Logger.getLogger(TextPaneTesting1.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
popup.add(listOption);
popup.show(jTextPane1, jTextPane1.getMousePosition().x, jTextPane1.getMousePosition().y);
}
}
static void updateText(int begin, int end) throws IOException{
try {
doc.remove(begin, end - begin);
String replaced = "<a href = '#'>Replaced</a>";
kit.insertHTML((HTMLDocument) doc, begin, replaced, 0, 0, HTML.Tag.A);
} catch (BadLocationException ex) {
Logger.getLogger(TextPaneTesting1.class.getName()).log(Level.SEVERE, null, ex);
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(TextPaneTesting1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(TextPaneTesting1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(TextPaneTesting1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(TextPaneTesting1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
try {
new TextPaneTesting1().setVisible(true);
} catch (IOException ex) {
Logger.getLogger(TextPaneTesting1.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
}
// Variables declaration - do not modify
private javax.swing.JScrollPane jScrollPane1;
private static javax.swing.JTextPane jTextPane1;
// End of variables declaration
private javax.swing.JPopupMenu popup;
}
导入java.awt.ComponentOrientation;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.io.IOException;
导入java.util.logging.Level;
导入java.util.logging.Logger;
导入javax.swing.JEditorPane;
导入javax.swing.JMenuItem;
导入javax.swing.jpopmenu;
导入javax.swing.event.HyperlinkEvent;
导入javax.swing.text.BadLocationException;
导入javax.swing.text.html.html;
导入javax.swing.text.html.HTMLDocument;
导入javax.swing.text.html.HTMLEditorKit;
公共类TextPaneTesting1扩展了javax.swing.JFrame{
/**
*创建新表单TextPaneTesting
*/
静态HTMLEditorKit=新的HTMLEditorKit();
静态HTMLDocument doc=新的HTMLDocument();
静态字符串主文本;
public TextPaneTesting1()引发IOException{
初始化组件();
jTextPane1.setEditorKit(工具包);
jTextPane1.setDocument(doc);
mainText=“NotLink NotLink。”;
jTextPane1.setEditable(false);
jTextPane1.setText(主文本);
jTextPane1.setCaretPosition(0);
}
/**
*从构造函数中调用此方法来初始化表单。
*警告:不要修改此代码。此方法的内容始终为
*由表单编辑器重新生成。
*/
@抑制警告(“未选中”)
//
私有组件(){
jScrollPane1=newjavax.swing.JScrollPane();
jTextPane1=newjavax.swing.JTextPane();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jTextPane1.setContentType(“text/UTF-8”);//NOI18N
jTextPane1.setFont(新的java.awt.Font(“Tahoma”,0,18));//NOI18N
jTextPane1.addHyperlinkListener(新的javax.swing.event.HyperlinkListener(){
public void hyperlinkUpdate(javax.swing.event.HyperlinkEvent evt){
jTextPane1HyperlinkUpdate(evt);
}
});
jScrollPane1.setViewportView(jTextPane1);
javax.swing.GroupLayout=newjavax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(布局);
layout.setHorizontalGroup(
createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1,javax.swing.GroupLayout.Alignment.TRAILING,javax.swing.GroupLayout.DEFAULT_SIZE,613,Short.MAX_值)
);
layout.setVerticalGroup(
createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1,javax.swing.GroupLayout.DEFAULT\u SIZE,300,Short.MAX\u值)
);
包装();
}//
私有void jTextPane1HyperlinkUpdate(javax.swing.event.HyperlinkEvent evt){
if(evt.getEventType()==HyperlinkEvent.EventType.ACTIVATED){
int begin=evt.getSourceElement().getStartOffset();
int end=evt.getSourceElement().getEndOffset();
popup=new JPopupMenu();
JMenuItem listOption=新建JMenuItem(“单击它”);
setFont(新的java.awt.Font(“Tahoma”,java.awt.Font.PLAIN,12));
listOption.addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件evt){
试一试{
updateText(开始、结束);
}捕获(IOEX异常){
Logger.getLogger(TextPaneTesting1.class.getName()).log(Level.SEVERE,null,ex);
}
}
});
添加(列表选项);
show(jTextPane1,jTextPane1.getMousePosition().x,jTextPane1.getMousePosition().y);
}
}
静态void updateText(int begin,int end)引发IOException{
试一试{
单据删除(开始,结束-开始);
字符串“”替换;
insertHTML((HTMLDocument)文档,开始,替换,0,0,HTML.Tag.A);
}捕获(BadLocationException ex){
Logger.getLogger(TextPaneTesting1.class.getName()).log(Level.SEVERE,null,ex);
}
}
/**
*@param指定命令行参数
*/
公共静态void main(字符串参数[]){
/*设置Nimbus的外观和感觉*/
//
/*如果Nimbus(在JavaSE6中引入)不可用,请使用默认的外观。
*详情请参阅http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
试一试{
for(javax.swing.UIManager.LookAndFeelInfo:javax.swing.UIManager.getInstalledLookAndFeels()){
if(“Nimbus”.equals(info.getName())){
setLookAndFeel(info.getClassName());
打破
}
}
}捕获(ClassNotFoundException ex){
java.util.logging.Logger.getLogger(TextPaneTesting1.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);
}catch(实例化异常){
java.util.logging.Logger.getLogger(TextPaneTesting1.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);
}捕获(非法访问例外){
java.util.logging.Logger.getLogger(TextPaneTesting1.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);
}catch(javax.swing.UnsupportedLookAndFeelException ex){
java.util.logging.Logger.getLogger(TextPaneTesting1.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.event.HyperlinkEvent;
import javax.swing.text.BadLocationException;
import javax.swing.text.Element;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLEditorKit;
public class TextPaneTesting1 extends javax.swing.JFrame {
/**
* Creates new form TextPaneTesting
*/
static HTMLEditorKit kit = new HTMLEditorKit();
static HTMLDocument doc = new HTMLDocument();
static String mainText;
public TextPaneTesting1() throws IOException {
initComponents();
jTextPane1.setEditorKit(kit);
doc=(HTMLDocument)jTextPane1.getDocument();
mainText = "<a href = '#'>Link</a> NotLink <a href = '#'>Link</a> NotLink <a href = '#'>Link</a>.";
jTextPane1.setEditable(false);
jTextPane1.setText(mainText);
jTextPane1.setCaretPosition(0);
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
jTextPane1 = new javax.swing.JTextPane();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jTextPane1.setContentType("text/UTF-8"); // NOI18N
jTextPane1.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
jTextPane1.addHyperlinkListener(new javax.swing.event.HyperlinkListener() {
public void hyperlinkUpdate(javax.swing.event.HyperlinkEvent evt) {
jTextPane1HyperlinkUpdate(evt);
}
});
jScrollPane1.setViewportView(jTextPane1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 613, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
private void jTextPane1HyperlinkUpdate(final javax.swing.event.HyperlinkEvent event) {
if(event.getEventType() == HyperlinkEvent.EventType.ACTIVATED){
popup = new JPopupMenu();
JMenuItem listOption = new JMenuItem("Click It");
listOption.setFont(new java.awt.Font("Tahoma", java.awt.Font.PLAIN, 12));
listOption.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
try {
updateText(event.getSourceElement());
} catch (IOException ex) {
Logger.getLogger(TextPaneTesting1.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
popup.add(listOption);
popup.show(jTextPane1, jTextPane1.getMousePosition().x, jTextPane1.getMousePosition().y);
}
}
static void updateText(Element elem) throws IOException{
try {
final int begin = elem.getStartOffset();
final int end = elem.getEndOffset();
String replaced = "<a href = '#'>Replaced</a>";
doc.insertBeforeStart(elem, replaced);
doc.remove(begin+"Replaced".length(), end - begin);
} catch (BadLocationException ex) {
Logger.getLogger(TextPaneTesting1.class.getName()).log(Level.SEVERE, null, ex);
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(TextPaneTesting1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(TextPaneTesting1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(TextPaneTesting1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(TextPaneTesting1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
try {
new TextPaneTesting1().setVisible(true);
} catch (IOException ex) {
Logger.getLogger(TextPaneTesting1.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
}
// Variables declaration - do not modify
private javax.swing.JScrollPane jScrollPane1;
private static javax.swing.JTextPane jTextPane1;
// End of variables declaration
private javax.swing.JPopupMenu popup;
}