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