JAVA 这个程序中该怎么添加鼠标事件的监听器?
publicclassMyPictureextendsJFrameimplementsMouseListener{JFrameframe;JButtona1,a2,a3;...
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);
}
} 展开
{
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.*;
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);
}
}
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);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询