这个用MouseListener里的mouseEntered(MouseEvent
e)方法.
鼠标进入区域就产生事件反应.
你总要说说是什么问题吧.这么多一点点的看多不好找,有了错误,找起来方便点
public?class?BtnText1?extends?JFrame?implements?MouseMotionListener
不需要实现MouseMotionListener接口,你已经用了addMouseMotionListener方法
MouseAdapter类已经是实现了MouseMotionListener接口的.
改成
public?class?BtnText1?extends?JFrame
可以运行成功
这样子写比较好
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class MyPicture extends JFrame
{
JFrame frame;
JPanel panel;
String msg="坐标:";
String setX,setY;
public MyPicture() //程序界面
frame=new JFrame("图形学实验");
a1=new JButton("直线");
panel=new JPanel();
panel.add(a1);
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
addMouseListener(new MouseMonitor());
}
public void paint(Graphics g) //显示鼠标坐标
g.drawString(msg,sh,ss);
class MouseMonitor implements MouseListener {
public void mouseEntered(MouseEvent me) //鼠标进入事件
mouseX=getX();
mouseY=getY();
setX=mouseX+"";
setY=mouseY+"";
msg=msg+setX+" "+setY;
repaint();
public void mousePressed(MouseEvent me){}
public void mouseReleased(MouseEvent me) {}
public void mouseExited(MouseEvent me) {}
public void mouseClicked(MouseEvent me){}
public static void main(String[] args)
MyPicture org=new MyPicture();
你如果要监听某个控件的鼠标动作,可以调用这个控件的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) {
private void showPopup(java.awt.event.MouseEvent e) {
getJPopupMenuConsole().show(e.getComponent(), e.getX(),
e.getY());
});
你用错了侦听器,给你改了一下.
--------------------------------------------------------------------
import java.awt.Button;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class MouseEvent {
private static Frame f;
private static Button b;
private static int index = 1;
public static void test() {
f = new Frame("鼠标事件监听窗口");
b = new Button("转到");
f.setLayout(new FlowLayout());
b.setBackground(Color.white);
f.add(b);
f.setVisible(true);
// 添加监听器
public static void myEvent() {
// 添加窗口关闭监听
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent w) {
try {
Thread t = new Thread();
} catch (Exception e) {
System.out.println("关闭窗口时错误!");
e.printStackTrace();
System.exit(0);
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("鼠标进入当前按键" + index++ + "次");
// 添加鼠标监听
b.addMouseListener(new MouseAdapter() {
int i = 1;
int j = 1;
// 重写鼠标进入按键方法
public void mouseEntered() {
System.out.println("鼠标进入当前按键" + i++ + "次");
public void mousePressed() {
public static void main(String args[]) {
test();
myEvent();
以上就是土嘎嘎小编为大家整理的java代码鼠标监听代码相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!