java中鼠标进入某块区域触发事件的方法,比如我想在鼠标进入面板的300,300,100,100这
java中鼠标进入某块区域触发事件的方法,比如我想在鼠标进入面板的300,300,100,100这个矩形区域时触发v事件!mouseEntered中的代码应该怎么写?...
java中鼠标进入某块区域触发事件的方法,比如我想在鼠标进入面板的300,300,100,100这个矩形区域时触发v事件!mouseEntered中的代码应该怎么写?
展开
2个回答
展开全部
你可以这样改
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MyPractice extends JFrame {
int x=100,y=100;
public MyPractice(){
addMouseMotionListener(new MouseMotionListener() {
@Override
public void mouseMoved(MouseEvent e) {
// TODO Auto-generated method stub
x=e.getX();
y=e.getY();
repaint();
}
@Override
public void mouseDragged(MouseEvent e) {
// TODO Auto-generated method stub
}
});
setSize(300,400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public void paint(Graphics g){
super.paint(g);
//初始化小球
g.fillOval(x, y, 30, 30);
}
public static void main(String args[]){
new MyPractice();
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MyPractice extends JFrame {
int x=100,y=100;
public MyPractice(){
addMouseMotionListener(new MouseMotionListener() {
@Override
public void mouseMoved(MouseEvent e) {
// TODO Auto-generated method stub
x=e.getX();
y=e.getY();
repaint();
}
@Override
public void mouseDragged(MouseEvent e) {
// TODO Auto-generated method stub
}
});
setSize(300,400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public void paint(Graphics g){
super.paint(g);
//初始化小球
g.fillOval(x, y, 30, 30);
}
public static void main(String args[]){
new MyPractice();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询