java鼠标监听
Ⅰ 如何区分java中单击的是左击还是右击,还有一个问题是能否取消鼠标的监听事件
鼠标事件要添加MouseListener,捕获MouseEvent。MouseListener里的几个事件的参数都是MouseEvent,MouseEvent提供了获得点击了哪个键的方法getButton。比如下面这个点击事件处理。 public void mouseClicked(MouseEvent arg0) {
if(arg0.getButton() == MouseEvent.BUTTON1) {
// 左键点击
} else if(arg0.getButton() == MouseEvent.BUTTON2) {
// 中键点击
} else if(arg0.getButton() == MouseEvent.BUTTON3) {
// 右键点击
}
} 添加监听是addMouseListener方法,取消监听当然就是removeMouseListener方法了。
Ⅱ Java中要监听鼠标事件,则实现监听器类可以是使用的方式有哪几种
,MouseWheelListener,MouseMotionListener
如上所示,监听鼠标事件只要使用MouseAdapter类就行了
Ⅲ 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;
}
Ⅳ 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 鼠标监听 无法使用doClick()
事件实现接口出错,死心即可
Ⅵ Java 鼠标监听事件 mouseMoved(MouseEvent)
不需要实现MouseMotionListener接口,你已经用了addMouseMotionListener方法
MouseAdapter类已经是实现了MouseMotionListener接口的。
改成
可以运行成功
Ⅶ java 鼠标左键 加 ctrl 选中是什么监听事件
事件源。
java的事件监听机制包含三个组件事件源事件监听器事件对象,当事件源上发生操作,时它将会调用事件监听器的一个方法,并且会传递一个事件对象过来,事件监听器由开发人员编写,开发人员在事件监听器中,可以拿到事件源,从而对事件源上的操作进行处理。
Ⅷ 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@网页链接
Ⅸ Java中如何消除鼠标监听
可以把监听删除或把监听动作里的代码都删掉
Ⅹ java鼠标点击panel键盘监听失效
先定义一个全局的静态变量 static bool canClick=ture;
1.鼠标右键点击的事件前加个if判断:
if(canClick){
鼠标点击事件事件
}
2.然后添加键盘事件implements KeyListener
在重写的方法的keyPressed中加入:
if(e.getKeyCode()==KeyEvent.VK_CONTROL){
canClick=false;
}
在重新的方法的keyReleased中加入:
if(e.getKeyCode()==KeyEvent.VK_CONTROL){
canClick=true;
}