gpt4 book ai didi

Java( Swing ): MouseMoved not working at all

转载 作者:行者123 更新时间:2023-12-04 03:43:06 24 4
gpt4 key购买 nike

这让我抓狂。这一定是一个非常简单的问题,但我不可能看到它。

基本上永远不会调用 mouseMoved。下面是代码。

public class MouseMotionThing {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {

@Override
public void run() {
JFrame jFrame = new JFrame();
jFrame.setContentPane(new ContentPane());
jFrame.setSize(400, 400);
jFrame.setVisible(true);
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
});
}
}

还有我的面板。此实现使用 MouseInputListener:

class ContentPane extends JPanel implements MouseInputListener {
int x = 0, y = 0;

public ContentPane() {
setOpaque(true);
addMouseListener(this);
addMouseMotionListener(this);

}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);

g.drawLine(0, 0, x, y);
}

@Override
public void mouseDragged(MouseEvent e) {
System.out.println("Mouse Dragged!");
}

@Override
public void mouseMoved(MouseEvent e) {
System.out.println("CALLED MOUSE MOVED");
x = e.getX();
y = e.getY();

repaint();
}

@Override
public void mouseClicked(MouseEvent e) {
}

@Override
public void mousePressed(MouseEvent e) {
}

@Override
public void mouseReleased(MouseEvent e) {
}

@Override
public void mouseEntered(MouseEvent e) {
}

@Override
public void mouseExited(MouseEvent e) {
}
}

而此替代实现仅使用 MouseMotionListener:

class ContentPane extends JPanel implements MouseMotionListener {
int x = 0, y = 0;

public ContentPane() {
setOpaque(true);
addMouseMotionListener(this);

}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);

g.drawLine(0, 0, x, y);
}

@Override
public void mouseDragged(MouseEvent e) {
System.out.println("Mouse Dragged!");
}

@Override
public void mouseMoved(MouseEvent e) {
System.out.println("CALLED MOUSE MOVED");
x = e.getX();
y = e.getY();

repaint();
}
}

在上述替代实现中,都没有调用过 mouseMoved。我每次都在添加正确的听众,但它根本不起作用。 mouseDragged 虽然工作正常。我错过了什么?

LE:我在 Ubuntu 12.10 和 JDK 7 上测试了代码,运行良好。然后当我回到我的 W8 机器时,它开始工作了。除了重新启动笔记本电脑外,我什么也没做。我无法重现问题,也无法追踪到它,但如果我再次遇到它并设法找到一些东西,我会回来的。

最佳答案

尚不清楚哪里出了问题;它可能有助于进行完整构建。作为引用,我重构了你的 sscce使用 MouseAdapter 并从 JPanel 中删除泄漏的 this

import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class MouseMotionThing {

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame jFrame = new JFrame();
jFrame.add(new MousePanel());
jFrame.pack();
jFrame.setSize(400, 400);
jFrame.setVisible(true);
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
});
}

private static class MousePanel extends JPanel {

Point p = new Point();

public MousePanel() {
setOpaque(true);
addMouseMotionListener(new MouseHandler());
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawLine(0, 0, p.x, p.y);
}

private class MouseHandler extends MouseAdapter {

@Override
public void mouseDragged(MouseEvent e) {
update(e);
}

@Override
public void mouseMoved(MouseEvent e) {
update(e);
}

private void update(MouseEvent e) {
System.out.println(e.paramString());
MousePanel.this.p = e.getPoint();
MousePanel.this.repaint();
}
}
}
}

关于Java( Swing ): MouseMoved not working at all,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15329664/

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