2import java.applet.*import java.awt.*
3import java.awt.event.*
4public class Example18_1 extends Applet implements MouseListener
5{ TextField text
6 public void init()
7 { text=new TextField(40)add(text)
8 addMouseListener(this) //向小程序增加鼠标事件监视器。
9 }
10 public void mousePressed(MouseEvent e)
11 { text.setText("鼠标键按下了,位置是"+e.getX()+","+e.getY() )
12 }
13 public void mouseReleased(MouseEvent e)
14 { text.setText(" 鼠标松开了,位置是"+e.getX()+","+e.getY() )
15 }
16 public void mouseEntered(MouseEvent e)
17 { text.setText(" 鼠标进来了,位置是"+e.getX()+","+e.getY() )
18 }
19 public void mouseExited(MouseEvent e)
20 { text.setText(" 鼠标走开了")
21 }
22 public void mouseClicked(MouseEvent e)
23 { if(e.getClickCount()==2)
24 { text.setText("鼠标键双击,位置:"+e.getX()+","+e.getY())
25 }
26 else {}
27 }
28}
29
30//例子2
31import java.awt.*import java.awt.event.*
32class MyCanvas extends Canvas implements MouseListener
33{ int left=-1,right=-1//记录左、右键用的变量。
34 int x=-1,y=-1 //记录鼠标位置用的变量。
35 MyCanvas()
36 { setSize(100,100)
37 setBackground(Color.cyan)
38 addMouseListener(this)
39}
40 public void paint(Graphics g)
41 { if(left==1)
42 { g.drawOval(x-10,y-10,20,20)
43 }
44 else if(right==1)
45 { g.drawRect(x-8,y-8,16,16)
46 }
47 }
48 public void mousePressed(MouseEvent e)
49 { x=e.getX()y=e.getY()
50 if(e.getModifiers()==InputEvent.BUTTON1_MASK)
51{ left=1right=-1
52 repaint()
53}
54 else if(e.getModifiers()==InputEvent.BUTTON3_MASK)
55 { right=1left=-1
56 repaint()
57}
58 }
59 public void mouseReleased(MouseEvent e){}
60 public void mouseEntered(MouseEvent e){}
61 public void mouseExited(MouseEvent e)
62 { left=-1right=-1
63 repaint()
64 }
65 public void mouseClicked(MouseEvent e){}
66 public void update(Graphics g)
67 { if(left==1||right==1)
68{ paint(g)
69}
70 else
71{ super.update(g)
72}
73 }
74}
75public class Example18_2
76{ public static void main(String args[])
77 { Frame f=new Frame()
78 f.setBounds(100,100,200,200)f.setVisible(true)
79 f.addWindowListener(new WindowAdapter() //适配器
80 {public void windowClosing(WindowEvent e)
81{System.exit(0)
82}
83 })
84 f.add(new MyCanvas(),BorderLayout.CENTER)//添加画布。
85 f.validate()
86 }
87}
88
89//例子3
90import java.awt.*import java.awt.event.*
91import java.applet.*
92public class Example18_3 extends Applet implements MouseListener
93{ TextField textButton button
94 TextArea textArea
95 public void init()
96 { text=new TextField(10)text.addMouseListener(this)
97 button=new Button("按钮")button.addMouseListener(this)
98 addMouseListener(this)
99 textArea=new TextArea(8,28)
100 add(button)add(text)add(textArea)
101 }
102 public void mousePressed(MouseEvent e)
103 { if(e.getSource()==button)
104 {textArea.append("\n在按钮上鼠标按下,位置:"+"("+e.getX()+","+e.getY()+")")
105 }
106 else if(e.getSource()==text)
107 {textArea.append("\n在文本框上鼠标按下,位置:"+"("+e.getX()+","+e.getY()+")")
108 }
109else if(e.getSource()==this)
110 {textArea.append("\n在容器上鼠标按下,位置:"+"("+e.getX()+","+e.getY()+")")
111 }
112 }
113 public void mouseReleased(MouseEvent e) {}
114 public void mouseEntered(MouseEvent e) {}
115 public void mouseExited(MouseEvent e) {}
116 public void mouseClicked(MouseEvent e)
117 { if(e.getClickCount()>=2)
118 textArea.setText("鼠标连击,位置:"+"("+e.getX()+","+e.getY()+")")
119 }
120}
给你代码,------------------------------------------------------------------------------------------
import java.awt.MouseInfo
public class App extends JFrame implements KeyListener {
private JLabel lblNewLabel
private String str = ""
public App() {
setDefaultCloseOperation(EXIT_ON_CLOSE)
setLocationRelativeTo(null)
setSize(400, 300)
setResizable(false)
getContentPane().setLayout(null)
lblNewLabel = new JLabel("")
lblNewLabel.setBounds(0, 259, 394, 13)
getContentPane().add(lblNewLabel)
setVisible(true)
new Thread() {
public void run() {
while (true) {
try {
Point mainPoint = getLocation()
Point point = MouseInfo.getPointerInfo().getLocation()
int x = point.x - mainPoint.x
int y = point.y - mainPoint.y
if (x <0 || x >400) {
sleep(10)
continue
}
if (y <0 || y >300) {
sleep(10)
continue
}
lblNewLabel.setText(str + "x : " + x + " y : " + y)
sleep(10)
} catch (Exception e) {
}
}
}
}.start()
this.addKeyListener(this)
}
public static void main(String[] args) {
new App()
}
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_CONTROL) {
str = "C "
return
}
if (e.getKeyCode() == KeyEvent.VK_SHIFT) {
str = "S "
return
}
str = "D "
}
public void keyReleased(KeyEvent e) {
str = "U "
}
public void keyTyped(KeyEvent e) {
}
}