- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试编写利用JLayer
来阻止事件进入由该层装饰的组件的代码。但是,我无法找到一种方法来做到这一点。
Here is a 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) {
}
}
}
我搜索了一些开发人员尝试做我正在做的事情的例子,我发现的最佳答案是 this thread 上的最后一篇文章之一。 。基本上,建议是在 LayerUI
的 eventDispatched
方法中使用 InputEvent
。在我的示例中,我尝试这样做(当 blockInput
标志设置为 true
时),但它不会阻止输入进入 Canvas
- 仍然打印 “Mouse clicked on canvas”...
消息。但是,“鼠标单击图层”...
不会打印,因此使用事件似乎只会阻止事件在 JLayer
内处理,但我需要它们不要进入 Canvas
。作为一次孤注一掷的尝试,我还尝试在 processMouseEvent
中使用 MouseEvent
,但没有成功。
我担心这个问题可能是因为 Canvas
是一个 AWT 组件,而不是一个轻量级的 Swing 组件。不幸的是,对于我正在编写的应用程序来说,它必须是一个 AWT Canvas
。
有没有人设法从由 JLayer
装饰的重量级组件中获取 block 输入事件?
最佳答案
您的图层似乎可以与 JComponent
配合使用,例如 JPanel
。另请参阅Mixing Heavyweight and Lightweight Components和How to Decorate Components with the JLayer Class .
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);
}
}
}
关于java - 使用 JLayer 阻止对 Canvas 的输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25087035/
如何在jlayer中停止MP3? (stop() 不再使用) 我的代码如下: //main class mp3_main private AdvancedPlayer player;
我刚刚开始使用 Jlayer 库来播放 MP3。它工作得很好,我可以播放这首歌。我唯一的问题是实现暂停和恢复方法。由于我对多线程的了解有限,我认为让播放 MP3 的线程等待,声音会停止,为了恢复歌曲,
我对 jLayer 有疑问。我有一个带有开始播放歌曲代码的按钮: private void jButton2ActionPerformed(java.awt.event.ActionEvent evt
我注意到很多主题都是关于使用 JLayer 暂停/恢复 MP3 的,所以为了帮助大家,我专门为此设计了一个完整的类(class)!请参阅下面的答案。 注意:这是供我个人使用的,因此它可能不像某些人希望
我和一个 friend 正在将 MP3 播放器编程作为学校项目。我们即将完成,现在陷入了尝试编写一个函数来改变播放器音量的阶段。我们正在使用: 音频设备 高级玩家 我知道其他人已经问过同样的问题,但我
我一直在测试一个可以在 PC 和 Android 平台上运行的 Java 库,前段时间有人告诉我使用 JLayer。今天我正在愉快地编码,然后出现了这个异常: java.lang.ExceptionI
由于某种原因,每个图像仅显示 1/5(或更少)。由于它是分层 Pane ,因此至少应完全显示 1 个。布局为空,因为这是一个学习程序为什么会发生这种情况? 代码如下:类测试1 import javax
我想在图像上放置一个网格。我使用了一个使用 paintComponent 方法保存图像的 JLabel,我使用这个方法是因为在我的项目和 JLayer GridLayer; JLabel im
我的音乐播放器项目中的 JLayer 有一个非常具体的问题。我想包括一些东西来调整音量,但似乎不太容易实现。 这是因为 JLayer 本身不支持它。我从我的项目中排除了 Player 类并更改了一些方
您好,我想降低音量播放声音。我用了jl1.0.1让它发挥作用。我正在搜索如何控制音量,这就是我使用的代码 public static void playSound(String name){
我需要知道我应该使用什么来控制我使用 jLayer 创建的 MP3 播放器的音量?我可以使用哪些 API 或方法来控制 mp3 文件的音量? 最佳答案 public class VolumeSli
我正在尝试用 JLayer 包装一个 JButton 以向其添加一些效果/功能。当我用工具栏中的包装器替换按钮时,它会出于某种原因绘制按钮边框。工具栏已将翻转设置为 true。 为什么会发生这种情况,
我在 Clojure 中编写了几百行应用程序,这些应用程序使用 JLayer Java 库作为我在后台运行 mp3 的音频。这工作正常,但只要我在后台运行任何其他音乐、YouTube 等(我正在运行
我有一个线程来播放音频,但是当我关闭表单时,我想停止播放器播放音频文件,我的线程运行方法是: public static Player playplayer; public static FileIn
我正在使用 jLayer 1.0 来播放一个简单的 mp3 文件。除了前 3 秒左右,它工作正常(在轨道开头添加 5 秒的静音解决了这个问题)。这是我正在使用的代码: try { final
正在关注 Oracle's Myopia guide ,我有一个简单的 JPanel,它作为 JLayer 添加到 JFrame。很简单,这模糊了 JPanel 的组件。但是,我正在尝试在此 JPan
我正在使用 JLayer 解码 MP3,并将其存储为 PCM 数据。但是,无论我使用什么 MP3 文件,我总是会收到索引为 443 的 ArrayIndexOutOfBoudnsException。我
我正在尝试将图像背景设置在 3 个 JButton 后面。截至目前,它正在绘制背景,然后将按钮放在没有背景的下方(拉伸(stretch)窗口以查看)。我将如何使用 JLayeredPane 将背景图像
关闭。这个问题需要debugging details .它目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and th
我想在用户输入无效时绘制一个图标。我找到了 Oracle 的示例并根据我的目的对其进行了修改。图标的绘制工作正常,但是当我更改值以更正图标时,图标不会完全不可见:在 JPanel 上绘制的部分仍然显示
我是一名优秀的程序员,十分优秀!