JAVA 该怎么添加鼠标事件的监听器
importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;importjavax.swing.event.*...
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class MyPicture extends JFrame implements MouseListener
{
JFrame frame;
JButton a1,a2,a3;
JPanel panel;
int mouseX,mouseY,sh=10,ss=290;
String msg="坐标:";
String setX,setY;
public MyPicture() //程序界面
{
frame=new JFrame("图形学实验");
a1=new JButton("直线");
a2=new JButton("弧");
a3=new JButton("裁剪");
panel=new JPanel();
panel.add(a1);
panel.add(a2);
panel.add(a3);
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,300);
frame.setVisible(true);
}
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){}
void paint(Graphics g) //显示鼠标坐标
{
g.drawString(msg,sh,ss);
}
public static void main(String[] args)
{
MyPicture org=new MyPicture();
addMouseListener(this);
}
} 展开
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class MyPicture extends JFrame implements MouseListener
{
JFrame frame;
JButton a1,a2,a3;
JPanel panel;
int mouseX,mouseY,sh=10,ss=290;
String msg="坐标:";
String setX,setY;
public MyPicture() //程序界面
{
frame=new JFrame("图形学实验");
a1=new JButton("直线");
a2=new JButton("弧");
a3=new JButton("裁剪");
panel=new JPanel();
panel.add(a1);
panel.add(a2);
panel.add(a3);
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,300);
frame.setVisible(true);
}
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){}
void paint(Graphics g) //显示鼠标坐标
{
g.drawString(msg,sh,ss);
}
public static void main(String[] args)
{
MyPicture org=new MyPicture();
addMouseListener(this);
}
} 展开
展开全部
这样子写比较好
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class MyPicture extends JFrame
{
JFrame frame;
JButton a1,a2,a3;
JPanel panel;
int mouseX,mouseY,sh=10,ss=290;
String msg="坐标:";
String setX,setY;
public MyPicture() //程序界面
{
frame=new JFrame("图形学实验");
a1=new JButton("直线");
a2=new JButton("弧");
a3=new JButton("裁剪");
panel=new JPanel();
panel.add(a1);
panel.add(a2);
panel.add(a3);
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,300);
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();
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class MyPicture extends JFrame
{
JFrame frame;
JButton a1,a2,a3;
JPanel panel;
int mouseX,mouseY,sh=10,ss=290;
String msg="坐标:";
String setX,setY;
public MyPicture() //程序界面
{
frame=new JFrame("图形学实验");
a1=new JButton("直线");
a2=new JButton("弧");
a3=new JButton("裁剪");
panel=new JPanel();
panel.add(a1);
panel.add(a2);
panel.add(a3);
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,300);
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();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询