Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在文本上设置突出显示将删除该文本上的鼠标突出显示_Java_Swing - Fatal编程技术网

Java 在文本上设置突出显示将删除该文本上的鼠标突出显示

Java 在文本上设置突出显示将删除该文本上的鼠标突出显示,java,swing,Java,Swing,我在文本区域中突出显示一些文本: Highlighter highlighter = getHighlighter(); Highlighter.HighlightPainter painter = new DefaultHighlighter.DefaultHighlightPainter(new Color(201, 197, 198)); highlighter.addHighlight(0,10, painter); 这个很好用。但是,我希望在使用鼠标突出显示文本时使用默认的突出显示颜

我在文本区域中突出显示一些文本:

Highlighter highlighter = getHighlighter();
Highlighter.HighlightPainter painter = new DefaultHighlighter.DefaultHighlightPainter(new Color(201, 197, 198));
highlighter.addHighlight(0,10, painter);
这个很好用。但是,我希望在使用鼠标突出显示文本时使用默认的突出显示颜色。当鼠标不再突出显示文本时,它将恢复为我选择的突出显示颜色,
新颜色(201197198)

鼠标高光是否可能优先于我设置的高光


谢谢

您可以定义自己的荧光灯并设置为JTextComponent。请参阅DefaultHighlighter

绘制顺序在下面的方法中定义,但高光和LayeredHighlightInfo无法访问以覆盖(包级别)

public void paintLayeredHighlights(图形g、int p0、int p1、,
形状视图边界,
JTextComponent编辑器,视图){
对于(int counter=highlights.size()-1;计数器>=0;计数器--){
对象标记=highlights.elementAt(计数器);
if(标记LayeredHighlightInfo的实例){
LayeredHighlightInfo lhi=(LayeredHighlightInfo)标记;
int start=lhi.getStartOffset();
int end=lhi.getEndOffset();
如果((p0start)||
(p0>=开始和p0<结束)){
lhi.paintLayeredHighlights(g、p0、p1、viewBounds、,
编辑(视图);
}
}
}
}
可能(Stas和Rob)实现了自己的荧光灯,其中需要根据鼠标选择从API中覆盖矩形/形状

但更方便的是将JTextPane与AttributeSet一起使用,但忽略了带有彩色矩形的荧光笔

比如说

import java.awt.Color;
import javax.swing.*;
import javax.swing.text.*;

public class ColorPane extends JTextPane {

    private static final long serialVersionUID = 1L;

    public void appendNaive(Color c, String s) { // naive implementation
        // bad: instiantiates a new AttributeSet object on each call
        SimpleAttributeSet aset = new SimpleAttributeSet();
        StyleConstants.setForeground(aset, c);
        int len = getText().length();
        setCaretPosition(len); // place caret at the end (with no selection)
        setCharacterAttributes(aset, false);
        replaceSelection(s); // there is no selection, so inserts at caret
    }

    public void append(Color c, String s) { // better implementation--uses       
        StyleContext sc = StyleContext.getDefaultStyleContext(); // StyleContext
        AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY,
                StyleConstants.Foreground, c);
        int len = getDocument().getLength(); // same value as
        //getText().length();
        setCaretPosition(len); // place caret at the end (with no selection)
        setCharacterAttributes(aset, false);
        replaceSelection(s); // there is no selection, so inserts at caret
    }

    public static void main(String argv[]) {
        UIManager.put("TextPane.selectionBackground", Color.yellow);
        UIManager.put("TextPane.selectionForeground", Color.blue);
        ColorPane pane = new ColorPane();
        for (int n = 1; n <= 400; n += 1) {
            if (isPrime(n)) {
                pane.append(Color.red, String.valueOf(n) + ' ');
            } else if (isPerfectSquare(n)) {
                pane.append(Color.blue, String.valueOf(n) + ' ');
            } else {
                pane.append(Color.black, String.valueOf(n) + ' ');
            }
        }
        JFrame f = new JFrame("ColorPane example");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setContentPane(new JScrollPane(pane));
        f.setSize(600, 400);
        f.setVisible(true);
    }

    public static boolean isPrime(int n) {
        if (n < 2) {
            return false;
        }
        double max = Math.sqrt(n);
        for (int j = 2; j <= max; j += 1) {
            if (n % j == 0) {
                return false; // j is a factor
            }
        }
        return true;
    }

    public static boolean isPerfectSquare(int n) {
        int j = 1;
        while (j * j < n && j * j > 0) {
            j += 1;
        }
        return (j * j == n);
    }
}

你的意思是你不想将荧光笔应用于所选文本?我仍然想在你使用鼠标时应用荧光笔。我想保留我设置的高光,但当我使用鼠标突出显示所选文本时,我希望鼠标高光生效(即,文本高光变为蓝色)。当鼠标不再突出显示该文本时,它会返回到我设置的突出显示,你能不能再突出显示一点?如何使用鼠标突出显示文本-将鼠标悬停在某些文本或选定文本上?您能提供一个小屏幕截图来显示您想要实现的目标吗?对于整个JTextArea,此选项应该是(f.e.红色),对于纯文本,红色,对于突出显示的文本,相同的红色是一个不错的选择,因为它通常反映了系统默认值或用户的控制面板选择。向属性添加一些内容意味着文档(模型)更改。如果你想突出显示例如拼写检查错误,这会很慢。感谢关于Attibute的课程,很好的代码+1,可能我遗漏了一些东西,但让我们离开,不要与OP的问题相关,所以嗯。。。我们应该在哪里获得“亮点”和“分层HighlightInfo”?
import java.awt.Color;
import javax.swing.*;
import javax.swing.text.*;

public class ColorPane extends JTextPane {

    private static final long serialVersionUID = 1L;

    public void appendNaive(Color c, String s) { // naive implementation
        // bad: instiantiates a new AttributeSet object on each call
        SimpleAttributeSet aset = new SimpleAttributeSet();
        StyleConstants.setForeground(aset, c);
        int len = getText().length();
        setCaretPosition(len); // place caret at the end (with no selection)
        setCharacterAttributes(aset, false);
        replaceSelection(s); // there is no selection, so inserts at caret
    }

    public void append(Color c, String s) { // better implementation--uses       
        StyleContext sc = StyleContext.getDefaultStyleContext(); // StyleContext
        AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY,
                StyleConstants.Foreground, c);
        int len = getDocument().getLength(); // same value as
        //getText().length();
        setCaretPosition(len); // place caret at the end (with no selection)
        setCharacterAttributes(aset, false);
        replaceSelection(s); // there is no selection, so inserts at caret
    }

    public static void main(String argv[]) {
        UIManager.put("TextPane.selectionBackground", Color.yellow);
        UIManager.put("TextPane.selectionForeground", Color.blue);
        ColorPane pane = new ColorPane();
        for (int n = 1; n <= 400; n += 1) {
            if (isPrime(n)) {
                pane.append(Color.red, String.valueOf(n) + ' ');
            } else if (isPerfectSquare(n)) {
                pane.append(Color.blue, String.valueOf(n) + ' ');
            } else {
                pane.append(Color.black, String.valueOf(n) + ' ');
            }
        }
        JFrame f = new JFrame("ColorPane example");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setContentPane(new JScrollPane(pane));
        f.setSize(600, 400);
        f.setVisible(true);
    }

    public static boolean isPrime(int n) {
        if (n < 2) {
            return false;
        }
        double max = Math.sqrt(n);
        for (int j = 2; j <= max; j += 1) {
            if (n % j == 0) {
                return false; // j is a factor
            }
        }
        return true;
    }

    public static boolean isPerfectSquare(int n) {
        int j = 1;
        while (j * j < n && j * j > 0) {
            j += 1;
        }
        return (j * j == n);
    }
}
class HighlightCaret extends DefaultCaret {

    private static final Highlighter.HighlightPainter unfocusedPainter = new DefaultHighlighter.DefaultHighlightPainter(new Color(230, 230, 210));
    private static final long serialVersionUID = 1L;
    private boolean isFocused;

    @Override
    protected Highlighter.HighlightPainter getSelectionPainter() {
        return isFocused ? super.getSelectionPainter() : unfocusedPainter;
    }

    @Override
    public void setSelectionVisible(boolean hasFocus) {
        if (hasFocus != isFocused) {
            isFocused = hasFocus;
            super.setSelectionVisible(false);
            super.setSelectionVisible(true);
        }
    }
}