java的鼠標(biāo)移動(dòng)事件
java的鼠標(biāo)移動(dòng)事件
愛學(xué)習(xí)的小伙伴們,可能都可能都學(xué)過(guò)Java,但是你知道java的鼠標(biāo)移動(dòng)事件是什么嗎?不知道的話跟著學(xué)習(xí)啦小編一起來(lái)學(xué)習(xí)了解java的鼠標(biāo)移動(dòng)事件。
java的鼠標(biāo)移動(dòng)事件代碼介紹
import java.awt.BorderLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class mymouse {
static JFrame frm;
static JTextField tf;
static JButton btn;
static int x, y,px, py,bx,by,offx,offy; //
public static void main(String[] args) {
frm = new JFrame("dialog");
frm.setBounds(400, 100, 500, 400);
frm.setLayout(null);
tf = new JTextField();
tf.setBounds(60,200,200,20);
btn = new JButton("move me");
btn.setBounds(60,40,100,50);
frm.add(tf);
frm.add(btn);
btn.addMouseMotionListener(new mymousemotion());
btn.addMouseListener(new mymouselistener());
frm.setVisible(true);
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
static class mymousemotion implements MouseMotionListener{
public void mouseDragged(MouseEvent e) {
//
x=e.getX()-px+bx;
y=e.getY()-py+by;
btn.setLocation(x, y);
tf.setText("button location :"+x+","+y);
bx=x;
by=y;
}
public void mouseMoved(MouseEvent e) {
}
}
static class mymouselistener extends MouseAdapter{
public void mousePressed(MouseEvent e){
//鼠標(biāo)點(diǎn)擊位置相對(duì) btn 的坐標(biāo).
px=e.getX();
py=e.getY();
//獲取 btn 相對(duì) frm 的坐標(biāo)
bx=btn.getLocation().x;
by=btn.getLocation().y;
String str=px+","+py+" . "+bx+","+by;
tf.setText(str);
}
}
}
java的鼠標(biāo)移動(dòng)事件相關(guān)文章:
1.怎么使用jquery實(shí)現(xiàn)鼠標(biāo)停止移動(dòng)事件