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);
}
} 展开
1个回答
展开全部
这样子写比较好
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();
}
}
微测检测5.10
2023-05-10 广告
2023-05-10 广告
您好!建议咨 深圳市微测检测有限公司,已建立起十余个专业实验室,企业通过微测检测就可以获得一站式的测试与认 证解决方案;(EMC、RF、MFi、BQB、QI、USB、安全、锂电池、快充、汽车电子EMC、汽车手机互 联、语音通话质量),认证遇...
点击进入详情页
本回答由微测检测5.10提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询