當前位置:首頁 » 編程語言 » java滑鼠監聽

java滑鼠監聽

發布時間: 2022-10-11 01:06:54

Ⅰ 如何區分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;
}

熱點內容
怎樣搭建米家智能家居伺服器 發布:2024-12-22 11:20:36 瀏覽:995
我的世界工藝伺服器傳送點怎麼搞 發布:2024-12-22 10:56:30 瀏覽:909
exprlinux 發布:2024-12-22 10:55:19 瀏覽:698
你知道甲魚密碼是多少嗎 發布:2024-12-22 10:26:32 瀏覽:813
我的世界國服伺服器開服 發布:2024-12-22 10:09:55 瀏覽:544
標題編譯策略 發布:2024-12-22 10:04:45 瀏覽:223
android開發xml 發布:2024-12-22 10:00:20 瀏覽:65
sql伺服器名稱什麼時候能寫ip 發布:2024-12-22 09:53:19 瀏覽:130
域控制伺服器怎麼設置ip 發布:2024-12-22 09:43:23 瀏覽:884
csvreaderpython 發布:2024-12-22 09:43:13 瀏覽:770