gpt4 book ai didi

java - Z 排序和叠加(JLabel 和 JEditorPane)

转载 作者:行者123 更新时间:2023-12-02 05:25:53 25 4
gpt4 key购买 nike

我当前正在尝试在 JEditorPane 上显示图像(带有 Jlabel)。我正在使用 Swixml 和表单布局,我已将 JLabel 和 JEditorPane 放在同一位置。因此,我只需将“setVisible”设置为 truefalse 即可显示图像。

到目前为止,一切都很好。但是,当有人尝试在 JEditorPane 中选择文本时,出现了丑陋的事情:Bug illustration

我尝试了一些事情:

  1. 禁用 JEditorPane:

    没有效果,即使选择不可见,文本也会出现在 JLabel 上。

  2. 重新绘制:

    当我重新绘制 JLabel 时,它再次被推到前面,但我必须重复执行此操作(例如每 50 毫秒一次)以使其不可见;

    我也尝试过在选择上重新绘制,但效果不佳,某些情况会被忽略,或者速度变慢,而且仍然很难看

  3. 最后一个想法是当 JLabel 显示时阻止鼠标进入 JEditorPane,但是 MouseEnter 事件不允许我从 EditorPane 中获取点,因此很难将光标放在合适的位置,从 JEditorPane 中...

我尝试了setComponentZOrder,但我没能让它很好地工作(我当然误用了它),但我不认为它能解决我的问题,因为它只改变了绘画顺序(最后一个被绘画的是在上面是Java),但这显然是一个重画问题。

指出我被迫使用 Java 1.6 应该很有用(我必须在 Java 1.7 和 1.8 中使用相同命名的类之间的冲突,Sun 的更改日志谈到了 1.9 中的修复...)

上下文是一个Applet,如果我无法理解(英语不是我的母语),请随时纠正我并询问我。

感谢您的帮助!

最佳答案

让我们看一下 DefaultHighlighter。 JEdi​​torPane 使用它来渲染选择(亮点)它有方法

public void paint(Graphics g) {
// PENDING(prinz) - should cull ranges not visible
int len = highlights.size();
for (int i = 0; i < len; i++) {
HighlightInfo info = highlights.elementAt(i);
if (!(info instanceof LayeredHighlightInfo)) {
// Avoid allocing unless we need it.
Rectangle a = component.getBounds();
Insets insets = component.getInsets();
a.x = insets.left;
a.y = insets.top;
a.width -= insets.left + insets.right;
a.height -= insets.top + insets.bottom;
for (; i < len; i++) {
info = highlights.elementAt(i);
if (!(info instanceof LayeredHighlightInfo)) {
Highlighter.HighlightPainter p = info.getPainter();
p.paint(g, info.getStartOffset(), info.getEndOffset(),
a, component);
}
}
}
}
}

如您所见,它绘制了所有高光。

您可以用您的扩展替换 JEditorPane 的突出显示画家(DefaultHighlighter 的实例)。重写paint()方法并将自定义剪辑添加到Graphics实例。该剪辑应具有基于当前剪辑(其中减去 JLabel 矩形)的自定义形状。

关于java - Z 排序和叠加(JLabel 和 JEditorPane),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26036024/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com