导航:首页 > 编程语言 > java鼠标监听

java鼠标监听

发布时间:2023-08-17 04:33:45

A. java如何使用MouseListener

鼠标监听器MouseListener
监听鼠标事件MouseEvent。
相应事件和处理方法
鼠标事件 处理方法
MOUSE_CLICKED MouseClicked (MouseEvent) 鼠标点击(单或双)
MOUSE_PRESSED MousePressed (MouseEvent) 鼠标按下
MOUSE_RELEASED MouseReleased(MouseEvent) 鼠标松开
MOUSE_ENTERED MouseEntered (MouseEvent) 鼠标进入(某组件区域)
MOUSE_EXITED MouseExited (MouseEvent) 鼠标离开(某组件区域)

鼠标事件MouseEvent常用方法
int getClickCount() 得到点击次数1 OR 2;
int getX(), int getY() 得到鼠标的(象素)位置。

鼠标监听器MouseMotionListener
对于鼠标的移动和拖放,另外用鼠标运动监听器MouseMotionListener。
因为许多程序不需要监听鼠标运动,把两者分开可简化程序。

相应事件和处理方法
鼠标事件 处理方法

MOUSE_MOVED MouseMoved (MouseEvent) 鼠标在移动

MOUSE_DRAGGED MouseDragged(MouseEvent) 鼠标被拖动

B. java 鼠标左键 加 ctrl 选中是什么监听事件

事件源。
java的事件监听机制包含三个组件事件源事件监听器事件对象,当事件源上发生操作,时它将会调用事件监听器的一个方法,并且会传递一个事件对象过来,事件监听器由开发人员编写,开发人员在事件监听器中,可以拿到事件源,从而对事件源上的操作进行处理。

C. Java中如何在windows桌面上添加鼠标监听事件

去下载 JInvoke , 这是一个例子:如果网上找不到 JInvoke.jar,我传了一个到网站了,http://bet.s215.eatj.com/Browser.jsp 打开后在上级目录[..]的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 hMole, 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) {
//Schele 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;
}

D. java中SWT鼠标单击事件监听器

为什么不能满足?

mouseUp就是按下之后被释放,mouseDown是按下去还没有释放。
你可以结合Control的bound和location来计算按下和释放时的位置来确定是否进行必要的事件处理。

E. java swing中如何实现对于鼠标监听悬停事件

importjava.awt.Container;
importjava.awt.Dimension;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.awt.event.MouseEvent;
importjava.awt.event.MouseListener;
importjava.awt.event.MouseMotionListener;
importjava.util.Calendar;
importjava.util.Date;

importjavax.swing.JFrame;
importjavax.swing.Timer;

{

privateDatelastTime;

publicDategetLastTime(){
returnlastTime;
}

publicvoidsetLastTime(DatelastTime){
this.lastTime=lastTime;
}

publicvoidcreateAndShowUI(){

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ContainercontentPane=getContentPane();

addMouseListener(newMouseListener(){

publicvoidmouseClicked(MouseEvente){
//TODOAuto-generatedmethodstub

}

publicvoidmouseEntered(MouseEvente){
//TODOAuto-generatedmethodstub
lastTime=Calendar.getInstance().getTime();
}

publicvoidmouseExited(MouseEvente){
//TODOAuto-generatedmethodstub
lastTime=null;
}

publicvoidmousePressed(MouseEvente){
//TODOAuto-generatedmethodstub

}

publicvoidmouseReleased(MouseEvente){
//TODOAuto-generatedmethodstub

}

});

setPreferredSize(newDimension(300,200));
pack();
setLocationRelativeTo(null);
setVisible(true);

}

publicstaticvoidmain(String[]args){

finalMyFrameframe=newMyFrame();
frame.createAndShowUI();

ActionListenerlistener=newActionListener(){
publicvoidactionPerformed(ActionEvente){
if(frame.getLastTime()!=null){
Datelast=frame.getLastTime();
Datenow=Calendar.getInstance().getTime();
if((now.getTime()-last.getTime())>3000){
System.out.println("悬浮了3秒");
}
}
}
};
intdelay=1000;
Timertimer=newTimer(delay,listener);
timer.start();

}

}

from@网页链接

F. Java 鼠标监听事件 mouseMoved(MouseEvent)

不需要实现MouseMotionListener接口,你已经用了addMouseMotionListener方法

MouseAdapter类已经是内实现了MouseMotionListener接口的。

改成

可以运行成功容

G. Java 程序实现鼠标点击 键盘等事件

用 Robot 类的如下方法:
void keyPress(int keycode)
按下给定的键。
void keyRelease(int keycode)
释放给定的键。
void mouseMove(int x, int y)
将鼠标指版针移动到给定屏幕坐权标。
void mousePress(int buttons)
按下一个或多个鼠标按钮。
void mouseRelease(int buttons)
释放一个或多个鼠标按钮。
void mouseWheel(int wheelAmt)
在配有滚轮的鼠标上旋转滚轮。

H. Java中要监听鼠标事件,则实现监听器类可以是使用的方式有哪几种

,MouseWheelListener,MouseMotionListener

如上所示,监听鼠标事件只要使用版MouseAdapter类就行了权

I. 如何区分java中单击的是左击还是右击,还有一个问题是能否取消鼠标的监听事件

mouse监听类中有判断mouseevent.isrightbutton和mouseevent.isleftbutton,可以辨别左右键,但现在很多应用程内序都不区分左右键,就像xp的菜单;容取消监听就是上面说的removeMouseListener

阅读全文

与java鼠标监听相关的资料

热点内容
使用土地的有关证明文件包含哪些 浏览:493
数据标注哪里可以接 浏览:482
在家自学编程下什么学 浏览:705
最近很火的app软件是什么软件 浏览:862
ai文字工具 浏览:157
兰博玩游戏路径怎么选择正确文件 浏览:972
淘宝直通车恢复老版本 浏览:510
播放草莓的图片我都文件 浏览:55
微信大文件打不开 浏览:767
家装合同准备哪些文件 浏览:296
应用bat合并excel文件 浏览:984
迅雷影音文件夹 浏览:109
makefile的文件路径 浏览:392
计算机程序文件名扩展名为 浏览:982
网络游戏推广策划案 浏览:609
替换所有文件内容的代码 浏览:960
不是常用数据模型有哪些 浏览:426
aspcms版本号 浏览:835
安卓怎么用数据流量下载软件 浏览:553
大众手动空调数据流通道号是多少 浏览:303

友情链接