
正文
java鼠标侦听器代码 js鼠标监听
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
关于java对JLabel使用settoolTipText();方法后鼠标点击监听没有效果。
实现MouseListener接口.
新增一个专门的"鼠标监听器"类,或者直接在你现有的类中实现"鼠标监听器".具体做法是在类声明语句中implements MouseListener.如下:
public class MouseAdp implements MouseListener{
public MouseAdp(){}
public void mouseClicked(MouseEvent e) {
/**鼠标点击事件(包括按下和弹起两个动作)处理方法.**/
System.out.println("你点了我!");
}
public void mouseEntered(MouseEvent e) {
/**鼠标移到组件上方法时事件处理方法.**/}
public void mouseExited(MouseEvent e) {
/**鼠标移开组件时事件处理方法.**/}
public void mousePressed(MouseEvent e) {
/**鼠标在组件上按下(但没弹起)时事件处理方法.**/}
public void mouseReleased(MouseEvent e) {
/**鼠标在组件上弹起事件处理方法.**/}
}
然后在你的JLabel实例上,作用这个监听器,如:
JLabel lab = new JLabel("点我");
lab.addMouseListener(new MouseAdp());
相关问答
Q1: Java中如何在windows桌面上添加鼠标监听事件
去下载 JInvoke , 这是一个例子:如果网上找不到 JInvoke.jar,我传了一个到网站了, 打开后在上级目录[..]的myfiles目录里能找到.ji.zip即是
import static com.jinvoke.win32.WinConstants.*;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import com.jinvoke.Callback;
import com.jinvoke.JInvoke;
import com.jinvoke.NativeImport;
import com.jinvoke.Util;
import com.jinvoke.win32.User32;
import com.jinvoke.win32.structs.Msg;
public class MouseHook extends JPanel{
static {
JInvoke.initialize();
}
@NativeImport(library = "user32")
public native static int SetWindowsHookEx (int idHook, Callback hookProc, int hModule, int dwThreadId);
@NativeImport(library = "user32")
public native static int UnhookWindowsHookEx (int idHook);
public static final int WH_MOUSE_LL = 14;
static JFrame frame;
static TextArea mouseEventArea = new TextArea();
static JButton setHookBtn;
static JButton removeHookBtn;
public MouseHook() {
super(new BorderLayout());
mouseEventArea.setText("1) Click the \"Set Mouse Hook\" button.\n" +
"2) Start clicking anywhere on the desktop. Mouse clicks will be captured here.\n" +
"3) Stop the mouse hook by clicking the \"Remove Mouse Hook\" button.\n\n");
JScrollPane MouseEventPane = new JScrollPane(mouseEventArea);
add(MouseEventPane, BorderLayout.CENTER);
JPanel buttonPanel = new JPanel();
buttonPanel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
setHookBtn = new JButton("Set Mouse Hook");
setHookBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
setMouseHook();
}} );
removeHookBtn = new JButton("Remove Mouse Hook");
removeHookBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
unsetMouseHook();
}} );
removeHookBtn.setEnabled(false);
buttonPanel.add(setHookBtn);
buttonPanel.add(removeHookBtn);
add(buttonPanel, BorderLayout.SOUTH);
}
private void setMouseHook() {
setHookBtn.setEnabled(false);
removeHookBtn.setEnabled(true);
// This hook is called in the context of the thread that installed it.
// The call is made by sending a message to the thread that installed the hook.
// Therefore, the thread that installed the hook must have a message loop.
//
// We crate a new thread as we don't want the AWT Event thread to be stuck running a message pump
// nor do we want the main thread to be stuck in running a message pump
Thread hookThread = new Thread(new Runnable(){
public void run() {
if (MouseProc.hookHandle == 0) {
int hInstance = User32.GetWindowLong(Util.getWindowHandle(frame), GWL_HINSTANCE);
MouseProc.hookHandle = SetWindowsHookEx(WH_MOUSE_LL,
new Callback(MouseProc.class, "lowLevelMouseProc"),
hInstance,
0);
// Standard message dispatch loop (message pump)
Msg msg = new Msg();
while (User32.GetMessage(msg, 0, 0, 0)) {
User32.TranslateMessage(msg);
User32.DispatchMessage(msg);
}
} else {
mouseEventArea.append("The Hook is already installed.\n");
}
}});
hookThread.start();
}
private void unsetMouseHook() {
setHookBtn.setEnabled(true);
removeHookBtn.setEnabled(false);
UnhookWindowsHookEx(MouseProc.hookHandle);
MouseProc.hookHandle = 0;
}
private static void createAndShowGUI() {
//Create and set up the window.
frame = new JFrame("Mouse Hook");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MouseHook MouseEventsWindow = new MouseHook();
MouseEventsWindow.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
//Add content to the window.
frame.add(MouseEventsWindow, BorderLayout.CENTER);
//Display the window.
frame.pack();
frame.setBounds(300, 200, 750, 600);
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
class MouseProc {
static int hookHandle;
@NativeImport(library = "user32")
public native static int CallNextHookEx (int idHook, int nCode, int wParam, int lParam);
static {
JInvoke.initialize();
}
public static int lowLevelMouseProc(int nCode, int wParam, int lParam ) {
if (nCode 0)
return CallNextHookEx(hookHandle, nCode, wParam, lParam);
if (nCode == HC_ACTION) {
MouseHookStruct mInfo = Util.ptrToStruct(lParam, MouseHookStruct.class);
String message = "Mouse pt: (" + mInfo.pt.x + ", " + mInfo.pt.y + ") ";
switch (wParam) {
case WM_LBUTTONDOWN:
message += "Left button down";
break;
case WM_LBUTTONUP:
message += "Left button up";
break;
case WM_MOUSEMOVE:
message += "Mouse moved";
break;
case WM_MOUSEWHEEL:
message += "Mouse wheel rotated";
break;
case WM_RBUTTONDOWN:
message += "Right button down";
break;
case WM_RBUTTONUP:
message += "Right button down";
break;
}
System.out.println(message);
// MouseHook.mouseEventArea.append(message+"\n");
}
return CallNextHookEx(hookHandle, nCode, wParam, lParam);
}
}
=============================================
import com.jinvoke.NativeStruct;
import com.jinvoke.win32.structs.Point;
@NativeStruct
public class MouseHookStruct {//MSLLHOOKSTRUCT
public Point pt = new Point();
public int mouseData;
public int flags;
public int time;
public int dwExtraInfo;
}
Q2: Java 鼠标监听事件 mouseMoved(MouseEvent)
public class BtnText1 extends JFrame implements MouseMotionListener
不需要实现MouseMotionListener接口java鼠标侦听器代码,java鼠标侦听器代码你已经用java鼠标侦听器代码了addMouseMotionListener方法
MouseAdapter类已经是实现java鼠标侦听器代码了MouseMotionListener接口java鼠标侦听器代码的。
改成
public class BtnText1 extends JFrame
可以运行成功
Q3: java,鼠标移动的问题,MouseMotionListener中的mouseMoved,可以帮我解释一下么,需要细节,在线等。
public interface MouseMotionListener extends EventListener
用于接收组件上的鼠标移动事件的侦听器接口。(对于单击和其他鼠标事件,请使用 MouseListener。)
旨在处理鼠标移动事件的类要么实现此接口(及其包含的所有方法),要么扩展抽象 MouseMotionAdapter 类(仅重写有用的方法)。
然后使用组件的 addMouseMotionListener 方法将从该类所创建的侦听器对象向该组件注册。移动或拖动鼠标时会生成鼠标移动事件。(将生成很多此类事件)。发生鼠标移动事件时,将调用该侦听器对象中的相应方法,并将 MouseEvent 传递给该方法。
mouseMoved
void mouseMoved(MouseEvent e)
鼠标光标移动到组件上但无按键按下时调用
Q4: java中addMouseListener(this)中的this是指啥?怎么好多方法都有this ,this具体指啥,求详解!!
this是指您正在编写的这个类,在软件运行时(运行时)所实例化的对象。
Swing程序可以说是基于“事件”的程序。所谓“事件”是指程序运行时所发生的事情,包括图形界面的用户交互操作、Swing组件内部对某事件的连续反应所产生的其他事件、某种情况下所发生的自定义事件。
运行时的事件是一个事件对象,它有三个要素:1、事件源是谁,也就是说是在哪个Swing组件中产生的这个事件;2、事件内部所包含的数据,我们可以将一些重要数据封装在事件中;3、有那些监听器对象负责处理这个事件。
总体来讲,一个事件只有一个事件源,可以有0或多个负责处理这个事件的监听器。
事件源负责向外抛出事件,抛出事件就是在告知添加到此事件源的所有监听器,这个事件已经被抛出,已被添加到此事件源负责处理该事件的监听器应该开始处理该事件了。
综上所述,只有将监听器添加到事件源上,当事件被事件源抛出后,才可能会有监听器处理此事件。从你的例子来看,这是在向当前类(事件源)添加一个鼠标事件监听器(此监听器还是当前类的实例)。
重复一下,只有将监听器添加到事件源上,当事件被事件源抛出后,才可能会有监听器处理此事件。
你必须清晰认识到:事件源、事件、监听器是围绕事件的三个不同概念。
作为初学者,很少有机会手动写抛出事件的代码。比如鼠标事件作为Java内部定义好的事件,当用户鼠标在界面中执行任何动作(如移位、点击、拖动等等等等),都会由Java已经封装好的、支持鼠标事件的组件作为事件源,向外抛出鼠标事件。组件(事件源)抛出事件后,应该由添加到此事件源的鼠标事件监听器处理此事件。
一般来讲,Java已经封装好了很多内置事件监听器的接口和实现类,要处理某事件,必须创建一个监听器类,使其实现处理此事件监听器的接口或继承处理此事件监听器的实现类。
Q5: java中监听鼠标
你如果要监听某个控件的鼠标动作,可以调用这个控件的addMouseListener(new MouseAdapter()
{
public void mouseOver(MouseEvent e)
{
.....函数体
}
//或其他的要用的函数,可参看jdk文档
}
);
下面的程序参考下:
jTextPaneIPList.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseReleased(java.awt.event.MouseEvent e) {
if (e.isPopupTrigger()) {
targetPane = jTextPaneIPList;
getJMenuItemPaste().setEnabled(true);
showPopup(e);
}
}
public void mousePressed(java.awt.event.MouseEvent e) {
if (e.isPopupTrigger()) {
targetPane = jTextPaneIPList;
getJMenuItemPaste().setEnabled(true);
showPopup(e);
}
}
private void showPopup(java.awt.event.MouseEvent e) {
getJPopupMenuConsole().show(e.getComponent(), e.getX(),
e.getY());
}
});
关于java鼠标侦听器代码和js鼠标监听的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。







