简体   繁体   English

java如何判断鼠标键是否按下

[英]How do I determine if the mouse button is pressed in java

I've been looking for a while and could only fine semi-useful code and people nothing useful I know I need mouse event but don't know how to declare it.我一直在寻找一段时间,只能找到半有用的代码,而人们没有任何用处我知道我需要鼠标事件,但不知道如何声明它。

I am going to provide you a very simple and organized sample program that you can build on.我将为您提供一个非常简单且有组织的示例程序,您可以在此基础上进行构建。 Make sure to check out the main Java website if you need help understanding what each method does, Also in the program I have included a very simple main function for a frame.如果您需要帮助了解每种方法的作用,请务必查看主要的 Java 网站,此外,在程序中我还包含了一个非常简单的主要 function 框架。 you are welcome to add onto it and test it.欢迎您添加到它并进行测试。 I hope this helps!我希望这有帮助! =) =)

A sample program:示例程序:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class Mouse extends Frame implements MouseListener{
    Mouse(){} // Empty constructor.

    public static void main(String[] args){
        // The frame for the program, you can add onto it.
        JFrame frame = new JFrame("MouseListener");
        frame.setSize(400, 200);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Mouse mou = new Mouse();
        frame.addMouseListener(mou);
    }

    // All different events you can use!
    public void mousePressed(MouseEvent e) {}
    public void mouseReleased(MouseEvent e) {}
    public void mouseExited(MouseEvent e) {}
    public void mouseEntered(MouseEvent e) {} 
    public void mouseClicked(MouseEvent e) {} 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM