gpt4 book ai didi

java - Java Swing 中的 MouseMotionListener,将其与组件内的组件一起使用等

转载 作者:行者123 更新时间:2023-12-03 05:58:34 26 4
gpt4 key购买 nike

我正在 Swing 中开发触摸用户界面。虽然我知道这不是最佳选择,但我的截止日期很短,没有时间触摸屏特定的 GUI 包(如果有的话)。

我希望我的用户能够在屏幕上“滑动”手指,并且我可以用它来移动特殊的 JScrollPane View 。代码很简单 -

    public class PanScrollPane extends JScrollPane implements MouseMotionListener{  
public PanScrollPane() {
super();
this.addMouseMotionListener(this);
}
@Override
public void mouseDragged(MouseEvent arg0) {
System.out.println("Mouse Dragged!");
}
@Override
public void mouseMoved(MouseEvent arg0) {
System.out.println("Mouse Moved!");
}

我遇到的问题是 JScrollPane 是各种 JComponent 的容器。当我第一次开始研究这个问题时,我认为 MouseMovedEvent 和 MouseDraggedEvent 会沿着“GUI 树”向上传播,直到它们遇到带有专门针对该事件的监听器的组件。现在看来,我添加到 panScrollPane 的任何组件都会阻止任何这些 MouseMotion 事件,使我无法平移。

    panScrollPane.add(new JButton("This thing blocks any mouse motion events"));

我认为手动传播 MouseEvent(向每个组件添加监听器,然后让它们将事件发送给其父组件)是可行的。然而,这是一项非常耗时的工作,因为我宁愿把时间花在其他事情上,我想知道你们中是否有人知道解决这个问题的方法。

感谢您的阅读,并希望感谢您的回答! :)

编辑:为了让我的意图更清楚。我只希望 panPanel 捕获 mousemotion 事件,任何其他事件(如 MouseClick、MouseRelease)都应该正常处理

最佳答案

这种临时方法利用了现有的 JScrollPane key bindings 中通常使用的操作。您必须根据 Scrollable 的实现调整 N .

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.Action;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JViewport;
import javax.swing.Timer;

/** @see http://stackoverflow.com/questions/7201509 */
public class ScrollAction extends JPanel {

private static final int TILE = 64;
private static final int DELTA = 16;

public ScrollAction() {
this.setOpaque(false);
this.setFocusable(true);
this.setPreferredSize(new Dimension(50 * TILE, 50 * TILE));
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.lightGray);
int w = this.getWidth() / TILE + 1;
int h = this.getHeight() / TILE + 1;
for (int row = 0; row < h; row++) {
for (int col = 0; col < w; col++) {
if ((row + col) % 2 == 0) {
g.fillRect(col * TILE, row * TILE, TILE, TILE);
}
}
}
}

private void display() {
JFrame f = new JFrame("ScrollAction");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JScrollPane scrollPane = new JScrollPane(this);
final ScrollTimer left = new ScrollTimer(scrollPane, "scrollLeft");
final ScrollTimer right = new ScrollTimer(scrollPane, "scrollRight");
final ScrollTimer up = new ScrollTimer(scrollPane, "scrollUp");
final ScrollTimer down = new ScrollTimer(scrollPane, "scrollDown");
final JViewport viewPort = scrollPane.getViewport();
viewPort.setPreferredSize(new Dimension(5 * TILE, 5 * TILE));
viewPort.addMouseMotionListener(new MouseAdapter() {

@Override
public void mouseMoved(MouseEvent e) {
left.stop();
if (e.getX() < DELTA) {
left.start();
}
right.stop();
if (e.getX() > viewPort.getWidth() - DELTA) {
right.start();
}
up.stop();
if (e.getY() < DELTA) {
up.start();
}
down.stop();
if (e.getY() > viewPort.getHeight() - DELTA) {
down.start();
}
}
});
f.add(scrollPane);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}

private static final class ScrollTimer implements ActionListener {

private static int N = 10;
private static int DELAY = 100;
private String cmd;
private Timer timer;
private Action action;
private JScrollPane scrollPane;
private int count;

public ScrollTimer(JScrollPane scrollPane, String action) {
this.cmd = action;
this.timer = new Timer(DELAY, this);
this.action = scrollPane.getActionMap().get(action);
this.scrollPane = scrollPane;
}

@Override
public void actionPerformed(ActionEvent e) {
if (count++ < N) {
action.actionPerformed(new ActionEvent(scrollPane, 0, cmd));
} else {
timer.stop();
}
}

public void start() {
count = 0;
timer.start();
}

public void stop() {
timer.stop();
count = 0;
}
}

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {

@Override
public void run() {
new ScrollAction().display();
}
});
}
}

关于java - Java Swing 中的 MouseMotionListener,将其与组件内的组件一起使用等,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7201509/

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