我正试图编写利用JLayer
来阻止事件进入由层装饰的组件的代码。然而,我想不出一个方法来做这件事。
这是SSCCE。这是一个pastebin链接,因为当我尝试使用它时,代码插入总是会破坏格式。
public class Window extends JFrame{
private static boolean blockInput = true;
private CustomCanvas canvas = new CustomCanvas();
public static void main(String[] args) {
new Window().setVisible(true);
}
public Window() {
canvas.addMouseListener(canvas);
LayerUI<Canvas> layerUI = new CanvasLayerUI();
JLayer<Canvas> canvasLayer = new JLayer<Canvas>(canvas, layerUI);
add(canvasLayer);
setSize(800, 600);
}
private class CanvasLayerUI extends LayerUI<Canvas> {
@Override
public void eventDispatched(AWTEvent e, JLayer<? extends Canvas> l) {
if (blockInput) {
if (e instanceof InputEvent) {
((InputEvent) e).consume();
}
} else {
super.eventDispatched(e, l);
}
}
@Override
public void installUI(JComponent c) {
super.installUI(c);
if (c instanceof JLayer) {
JLayer<?> layer = (JLayer<?>) c;
layer.setLayerEventMask(AWTEvent.MOUSE_EVENT_MASK);
}
}
@Override
protected void processMouseEvent(MouseEvent e, JLayer<? extends Canvas> l) {
super.processMouseEvent(e, l);
if (e.getID() == MouseEvent.MOUSE_CLICKED) {
System.out.println("Mouse click on layer: " + e.getPoint());
}
}
}
private class CustomCanvas extends Canvas implements MouseListener {
@Override
public void mouseClicked(MouseEvent e) {
System.out.println("Mouse click on canvas: " + e.getPoint());
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
}
}
我搜索了一些开发人员试图做我正在做的事情的例子,我找到的最好的答案是这个帖子上的最后一个帖子。建议基本上是在LayerUI
法中消耗eventDispatched
法中的InputEvent
。在我的示例中,我尝试这样做(当blockInput
标志设置为true
时),但它不会阻止输入进入Canvas
- "Mouse clicked on canvas"...
消息仍然打印。但是,没有打印"Mouse clicked on layer"...
,因此消费事件似乎只会阻止事件在JLayer
中进行处理,但我需要它们不要转到Canvas
。作为一个绝望的尝试,我也试图消耗processMouseEvent
中的MouseEvent
,但没有运气。
我担心这个问题可能是因为Canvas
是一个AWT组件,而不是轻量级的Swing组件。不幸的是,对于我正在编写的应用程序,它必须是AWT Canvas
。
是否有人设法从JLayer
装饰的重分量组件中获得块输入事件?
您的图层似乎与JComponent
一起工作,例如JPanel
。参见混合重量级和轻量级组件和如何用JLayer类装饰组件。
import java.awt.AWTEvent;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.InputEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLayer;
import javax.swing.JPanel;
import javax.swing.plaf.LayerUI;
public class MyWindow extends JFrame {
private boolean blockInput = true;
private CustomPanel panel = new CustomPanel();
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
new MyWindow().setVisible(true);
});
}
public MyWindow() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
LayerUI<JPanel> layerUI = new CanvasLayerUI();
JLayer<JPanel> canvasLayer = new JLayer<>(panel, layerUI);
add(canvasLayer);
pack();
setLocationRelativeTo(null);
}
private class CanvasLayerUI extends LayerUI<JPanel> {
@Override
public void eventDispatched(AWTEvent e, JLayer<? extends JPanel> l) {
if (blockInput) {
if (e instanceof InputEvent) {
((InputEvent) e).consume();
}
} else {
super.eventDispatched(e, l);
}
}
@Override
public void installUI(JComponent c) {
super.installUI(c);
if (c instanceof JLayer) {
JLayer<?> layer = (JLayer<?>) c;
layer.setLayerEventMask(AWTEvent.MOUSE_EVENT_MASK);
}
}
@Override
protected void processMouseEvent(MouseEvent e, JLayer<? extends JPanel> l) {
super.processMouseEvent(e, l);
if (e.getID() == MouseEvent.MOUSE_CLICKED) {
System.out.println("Mouse click on layer: " + e.getPoint());
}
}
}
private class CustomPanel extends JPanel {
public CustomPanel() {
this.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
System.out.println("Mouse click on canvas: " + e.getPoint());
}
});
}
@Override
public Dimension getPreferredSize() {
return new Dimension(640, 480);
}
}
}