介紹
java作為目前最為流行的計(jì)算機(jī)開發(fā)語言之一,學(xué)習(xí)java是高薪就業(yè)的好選擇,本文介紹的這個(gè)小工具主要是使用MouseInfo類實(shí)時(shí)獲取鼠標(biāo)的信息,然后再JDialog上顯示出來。希望下面的內(nèi)容介紹,能夠讓大家對于如何獲取鼠標(biāo)在屏幕上的坐標(biāo)更加了解,一起來學(xué)習(xí)下吧。
代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
import java.awt.BorderLayout; import java.awt.FlowLayout; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; import javax.swing.JLabel; import java.awt.Font; import java.awt.Point; import java.util.Timer; import java.util.TimerTask; import java.awt.Color; public class MouseInfo extends JFrame { private final JPanel contentPanel = new JPanel(); JLabel value_x = null ; JLabel value_y = null ; /** * Launch the application. */ public static void main(String[] args) { try { MouseInfo info_frame = new MouseInfo(); info_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); info_frame.setVisible( true ); info_frame.setAlwaysOnTop( true ); Timer timer = new Timer(); timer.schedule( new TimerTask() { @Override public void run() { Point point = java.awt.MouseInfo.getPointerInfo().getLocation(); // System.out.println("Location:x=" + point.x + ", y=" + // point.y); info_frame.value_x.setText( "" + point.x); info_frame.value_y.setText( "" + point.y); } }, 100 , 100 ); } catch (Exception e) { e.printStackTrace(); } } /** * Create the dialog. */ public MouseInfo() { setTitle( "\u9F20\u6807\u5750\u6807\u83B7\u53D6\u5668" ); setBounds( 100 , 100 , 217 , 156 ); getContentPane().setLayout( new BorderLayout()); contentPanel.setBorder( new EmptyBorder( 5 , 5 , 5 , 5 )); getContentPane().add(contentPanel, BorderLayout.CENTER); contentPanel.setLayout( null ); JLabel lblx = new JLabel( "\u5750\u6807x:" ); lblx.setFont( new Font( "宋體" , Font.PLAIN, 15 )); lblx.setBounds( 22 , 27 , 66 , 31 ); contentPanel.add(lblx); JLabel lbly = new JLabel( "\u5750\u6807y:" ); lbly.setFont( new Font( "宋體" , Font.PLAIN, 15 )); lbly.setBounds( 22 , 68 , 66 , 31 ); contentPanel.add(lbly); value_x = new JLabel( "0" ); value_x.setForeground(Color.BLUE); value_x.setFont( new Font( "宋體" , Font.PLAIN, 20 )); value_x.setBounds( 82 , 27 , 66 , 31 ); contentPanel.add(value_x); value_y = new JLabel( "0" ); value_y.setForeground(Color.BLUE); value_y.setFont( new Font( "宋體" , Font.PLAIN, 20 )); value_y.setBounds( 82 , 68 , 66 , 31 ); contentPanel.add(value_y); } } |
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流。