JPanel 中的鼠标移动事件

发布于 2024-12-20 19:19:30 字数 496 浏览 1 评论 0原文

我在使鼠标事件正常工作时遇到一些问题。我在 JLayeredPane 内部有一个 JPanel,它位于 JScrollPane 中。诚然,我对使用 Swing 相当陌生,但本质上,我希望 JPanel 对鼠标移动做出反应,但一直无法让它工作。

public class CellHighlighter extends JPanel implements MouseMotionListener{

    public CellHighlighter(){

    }

    public void mouseMoved(MouseEvent evt){
        System.out.println(evt.getPoint().x + ", " + evt.getPoint().y);
    }

    public void mouseDragged(MouseEvent evt){System.out.println("message");}

}

任何帮助将不胜感激,提前致谢!

I'm having some issues with getting my mouse events to work. I have a JPanel inside of a JLayeredPane which is in a JScrollPane. Admittedly, I am fairly new to working with Swing, but essentially, I want the JPanel to react to the mouse moving, but have been unable to get it to work.

public class CellHighlighter extends JPanel implements MouseMotionListener{

    public CellHighlighter(){

    }

    public void mouseMoved(MouseEvent evt){
        System.out.println(evt.getPoint().x + ", " + evt.getPoint().y);
    }

    public void mouseDragged(MouseEvent evt){System.out.println("message");}

}

Any help would be much appreciated, thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

等待我真够勒 2024-12-27 19:19:30

您是否正在使用 MouseListener 注册您的 JPanel 对象?
比如:

    public CellHighlighter(){
       this.addMouseMotionListener(this);
    }

或者您可能需要将 MouseListener 添加到 ScrollPane 或 LayeredPane 中?

Are you registering your JPanel Object with the MouseListener?
Something like:

    public CellHighlighter(){
       this.addMouseMotionListener(this);
    }

Or maybe you need to add the MouseListener to The ScrollPane or LayeredPane?

掐死时间 2024-12-27 19:19:30

以下是一些您可以使用的演示代码:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class ReactPanel extends JPanel implements MouseMotionListener {

    public ReactPanel(){
        setPreferredSize(new Dimension(450, 450));
        setBackground(Color.GREEN);
        addMouseMotionListener(this);
    }

    @Override
    public void mouseDragged(MouseEvent e) {
        System.out.println("Mouse dragged (" + e.getX() + ',' + e.getY() + ')');
    }

    @Override
    public void mouseMoved(MouseEvent e) {
        System.out.println("Mouse moved (" + e.getX() + ',' + e.getY() + ')');

    }

    public static void main(String[] args){
        //Create and set up the window.
        JFrame frame = new JFrame("MouseMotionEventDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Create and set up the content pane.
        JComponent newContentPane = new ReactPanel();
        newContentPane.setOpaque(true); //content panes must be opaque
        frame.setContentPane(newContentPane);

        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }
}

Here's some demo code you can play with:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class ReactPanel extends JPanel implements MouseMotionListener {

    public ReactPanel(){
        setPreferredSize(new Dimension(450, 450));
        setBackground(Color.GREEN);
        addMouseMotionListener(this);
    }

    @Override
    public void mouseDragged(MouseEvent e) {
        System.out.println("Mouse dragged (" + e.getX() + ',' + e.getY() + ')');
    }

    @Override
    public void mouseMoved(MouseEvent e) {
        System.out.println("Mouse moved (" + e.getX() + ',' + e.getY() + ')');

    }

    public static void main(String[] args){
        //Create and set up the window.
        JFrame frame = new JFrame("MouseMotionEventDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Create and set up the content pane.
        JComponent newContentPane = new ReactPanel();
        newContentPane.setOpaque(true); //content panes must be opaque
        frame.setContentPane(newContentPane);

        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文