close

參考答案

import java.awt.*;
import java.awt.event.*;
class JDD02 extends Frame
implements MouseListener, MouseMotionListener {
    
    String msg="", info="";
    int mouseX=0, mouseY=0 ;
    
    public static void main(String arg[])
    {
        JDD02 mew=new JDD02();
    }
    
    //ctor
    JDD02() {
        super("滑鼠感應視窗");
        addMouseListener(this);
        addMouseMotionListener(this);
        addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent we) {
                dispose();
            }
        });
        setBackground(Color.white);
        setSize(250,200);
        //setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }
    public void mouseClicked(MouseEvent me) {
        
    }

    public void mouseEntered(MouseEvent me) {
         //請在此撰寫滑鼠移入事件
        this.setBackground(Color.yellow);
        this.repaint();
    }
    public void mouseExited(MouseEvent me) {
         //請在此撰寫滑鼠移出視窗事件
        this.setBackground(Color.white);
        mouseX=me.getX();
        mouseY=me.getY();
        info = "Mouse just left the window from ";
        repaint();

    }
    public void mousePressed(MouseEvent me) {
         //請在此撰寫滑鼠按下事件
        mouseX=me.getX();
        mouseY=me.getY();
        msg="Down";
        info="Mouse is pressed at ";
        repaint();
        
    }
    public void mouseReleased(MouseEvent me) {
         //請在此撰寫滑鼠放開事件
        mouseX=me.getX();
        mouseY=me.getY();
        msg="Up";
        info="Mouse is released from ";
        repaint();

    }
    public void mouseDragged(MouseEvent me) {
         //請在此撰寫滑鼠拖曳事件
        mouseX=me.getX();
        mouseY=me.getY();
        msg="Dragging";
        info="Mouse is Dragging at ";
        repaint();

    }
    public void mouseMoved(MouseEvent me) {
         //請在此撰寫滑鼠移動事件
        mouseX=me.getX();
        mouseY=me.getY();
        msg="";
        info="Mouse is moving to ";
        repaint();

    }
    public void paint(Graphics g) {
         //請在此撰寫撰寫程式取得滑鼠座標
        g.drawString(msg, mouseX, mouseY);
        g.drawString(info+mouseX+","+mouseY, 10, 40);

    }
}

 

執行結果

 

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 Dino 的頭像
    Dino

    Dino`s Note

    Dino 發表在 痞客邦 留言(0) 人氣()