2011年3月26日
<Java> JVA306-滑鼠感應視窗
import java.awt.*;
import java.awt.event.*;
class JVA306 extends Frame
implements MouseListener, MouseMotionListener {
String msg="", info="";
int mouseX=0, mouseY=0 ;
public static void main(String arg[]){
JVA306 mew=new JVA306();
}
JVA306() {
super("滑鼠感應視窗");
addMouseListener(this);
addMouseMotionListener(this);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we) {
dispose();
}
});
setBackground(Color.white);
setSize(250,200);
setVisible(true);
}
public void mouseClicked(MouseEvent me) {
}
public void mouseEntered(MouseEvent me) {
//請在此撰寫滑鼠移入事件
setBackground(Color.yellow);
//info = "";
repaint();
//請在此撰寫滑鼠移入事件
}
public void mouseExited(MouseEvent me) {
//請在此撰寫滑鼠移出視窗事件
mouseX = me.getX();
mouseY = me.getY();
msg = "Mouse just left the window from " + mouseX +","+ mouseY;
setBackground(Color.white);
info = "";
repaint();
//請在此撰寫滑鼠移出視窗事件
}
public void mousePressed(MouseEvent me) {
//請在此撰寫滑鼠按下事件
mouseX = me.getX();
mouseY = me.getY();
msg = "Mouse is pressed at " + mouseX +"," +mouseY;
info = "DOWN";
repaint();
//請在此撰寫滑鼠按下事件
}
public void mouseReleased(MouseEvent me) {
//請在此撰寫滑鼠放開事件
mouseX = me.getX();
mouseY = me.getY();
msg = "Mouse is released from " + mouseX +","+ mouseY;
info = "UP";
repaint();
//請在此撰寫滑鼠放開事件
}
public void mouseDragged(MouseEvent me) {
//請在此撰寫滑鼠拖曳事件
mouseX = me.getX();
mouseY = me.getY();
msg = "Mouse is Dragging at " + mouseX +"," + mouseY;
info = "Dragging...";
repaint();
//請在此撰寫滑鼠拖曳事件
}
public void mouseMoved(MouseEvent me) {
//請在此撰寫滑鼠移動事件
mouseX = me.getX();
mouseY = me.getY();
msg = "Mouse is moving to " + mouseX +","+ mouseY;
info = "";
repaint();
//請在此撰寫滑鼠移動事件
}
public void paint(Graphics g) {
//請在此撰寫撰寫程式取得滑鼠座標
g.drawString(msg,10,40);
g.drawString(info,mouseX,mouseY);
//請在此撰寫撰寫程式取得滑鼠座標
}
}
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言