【问题标题】:How do I highlight the words that replaced the selected words?如何突出显示替换所选单词的单词?
【发布时间】:2015-06-10 04:25:36
【问题描述】:

此代码将所选单词替换为新单词,如下所示:

String search = jTextField1.getText();
String replaced = jTextPane.getText().replace(search, jTextField2.getText());
jTextPane.setText(replaced);

将生词的背景设置为黄色的最简单方法是什么?

【问题讨论】:

  • 两个问题 1. 为什么,2,为什么不在 Document 中搜索,我敢肯定 Oracle 教程 - 如何使用 TextComponents 包含有关在 Document 中搜索和突出显示匹配项的工作代码示例

标签: java swing highlight jtextpane


【解决方案1】:

你可以使用属性:

Simple AttributeSet changed = new SimpleAttributeSet();
StyleConstants.setForeground(changed, Color.RED);
StyleConstants.setBackground(changed, Color.YELLOW);

//  Change attributes on some text

StyledDocument doc = textPane.getStyledDocument();
doc.setCharacterAttributes(20, 4, changed, false);

查找/替换逻辑检查:Find/Replace, Highlight Words。您将修改突出显示代码以使用属性。

【讨论】:

    【解决方案2】:
     private void changeAllActionPerformed(java.awt.event.ActionEvent evt) {
            int j = 0;
            int i = 0;
            int index = 0;
            String search = jTextField1.getText();
            String replaced = jTextPane.getText().replace(search, jTextField2.getText());
            jTextPane.setText(replaced); 
            String newtext = jTextField2.getText();
    
            try{
                if(!jTextField2.getText().isEmpty()){
                    while(i != -1){
                            i = jTextPane.getText().indexOf(newtext, j);
                        if(i == -1)
                            break;
                        if(evt.getSource() == changeAll|| evt.getSource() == changeAllButton){
                            jTextPane.select(i, i + newtext.length());   
                        }
                        Color c = Color.YELLOW;
                        Style s = jTextPane.addStyle("TextBackground", null);
                        StyleConstants.setBackground(s, c);
                        StyledDocument d = jTextPane.getStyledDocument();
                        d.setCharacterAttributes(jTextPane.getSelectionStart(), jTextPane.getSelectionEnd() - jTextPane.getSelectionStart(), jTextPane.getStyle("TextBackground"), false);
                        j = i + search.length();
                        index++;
                    }
                    if (index > 0){
                        jTextPane.grabFocus();
                        jTextPane.setCaretPosition(jTextPane.getText().indexOf(newtext, 0) );
                    } 
            } catch (Exception e){
                jLabel.setText("error");
                System.err.print(e);
    }
    

    【讨论】:

    • What is the easiest way to set the backgrounds of the new words to yellow? - 此解决方案不满足该要求。这将更改文本窗格中“所有”单词的背景。例如如果你在上面的代码sn-p中将“0”改为“1”。即使没有更改,所有“-1”值也会突出显示。此外,由于我在答案中找到的链接中提供的原因,此代码在 Windows 机器上不起作用。
    • @lol,你是什么意思它适用于你的情况?你有没有尝试过我的建议?将上面的代码复制到您的文本窗格中,然后将“0”替换为“1”。不应突出显示“-1”的“1”,因为您的查找/替换逻辑并未更改该值。
    猜你喜欢
    • 2013-01-30
    • 1970-01-01
    • 2014-10-15
    • 1970-01-01
    • 1970-01-01
    • 2014-08-07
    • 2011-02-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多