java中给一个JLable数组添加鼠标监听?

java中给一个JLable数组添加鼠标监听?,第1张

for (int i = 0i <gt.lengthi++) {

final int index = i

gt[i].addMouseListener(new MouseAdapter() {

@Override

public void mouseEntered(MouseEvent e) {

gt[index].setIcon(new ImageIcon("newIcon.png"))

}

@Override

public void mouseExited(MouseEvent e) {

gt[index].setIcon(null)

}

})

}

在这个示例中,我们使用

for

循环遍历

gt

数组中的每个

JLabel

,并为其添加一个匿名的

MouseAdapter

对象作为鼠标监听器。在监听器中,我们使用

final

关键字定义了一个

index

变量,用于存储当前

JLabel

在数组中的索引。然后,我们在

mouseEntered

方法中使用

setIcon

方法将当前

JLabel

图标设置为新的图标。在

mouseExited

方法中,我们将当前

JLabel

的图标设置为

null

,以恢复其原始图标。

样例代码自己好好读读吧:

import java.awt.*

import java.awt.event.*

public class Test3 {

public static void main(String args[]) {

new Test2()

}

}

class Test2 extends Frame {

TextField tf1,tf2

public Test2(){

tf1 = new TextField(5)

tf2 = new TextField(5)

add(tf1)

add(tf2)

setLayout(new FlowLayout())

setBounds(100,100,400,300)

addMouseMotionListener(new MouseMotionAdapter(){//鼠标移动事件的侦听器

public void mouseMoved(MouseEvent e) {//鼠标按键在组件上移动(无按键按下)时调用。

tf1.setText(e.getX()+"" )

tf2.setText(e.getY()+"" )

}

})

addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e) {

System.exit(0)

}

})

setVisible(true)

}

}

去下载 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 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

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/11681471.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-17
下一篇 2023-05-17

发表评论

登录后才能评论

评论列表(0条)

保存