默認(rèn)的,F(xiàn)rame或者JFrame自身已經(jīng)實(shí)現(xiàn)了鼠標(biāo)拖拽標(biāo)題欄移動(dòng)窗口的功能。
只是,當(dāng)你不滿意java的JFrame樣式,隱藏了標(biāo)題欄和邊框,又或者干脆直接使用JWindow,那你又該怎么實(shí)現(xiàn)鼠標(biāo)拖拽移動(dòng)窗口的目的呢?最開(kāi)始,我簡(jiǎn)單的在mouseDragged方法里frame.setLocation(e.getX(), e.getY()),結(jié)果,frame拖拽的時(shí)候不停地閃爍,位置在屏幕上不斷跳動(dòng)。后來(lái)網(wǎng)上查資料,找到了答案。
這里給一個(gè)簡(jiǎn)單的示例,一看就明白:
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
package com.jebysun.test.globalhotkey; import java.awt.Color; import java.awt.Cursor; import java.awt.Point; import java.awt.event.MouseEvent; import javax.swing.JLabel; import javax.swing.JWindow; import javax.swing.event.MouseInputListener; /** * 自定義程序窗口,鼠標(biāo)可拖拽移動(dòng)其位置。 * @author Jeby Sun * */ public class MyFrame extends JWindow { private static final long serialVersionUID = 1L; JLabel titleLbl; public MyFrame() { //設(shè)置背景顏色不能直接調(diào)用其setBackground方法,而要設(shè)置其ContentPane的背景顏色。 this .getContentPane().setBackground( new Color( 0x99FF66 )); this .setBounds( 100 , 100 , 600 , 400 ); this .setLayout( null ); titleLbl = new JLabel( " 自定義窗口標(biāo)題欄" ); titleLbl.setOpaque( true ); titleLbl.setBackground( new Color( 0x66CC00 )); titleLbl.setBounds( 0 , 0 , 600 , 30 ); this .add(titleLbl); //鼠標(biāo)事件處理類 MouseEventListener mouseListener = new MouseEventListener( this ); titleLbl.addMouseListener(mouseListener); titleLbl.addMouseMotionListener(mouseListener); this .setVisible( true ); } /** * 鼠標(biāo)事件處理 * @author Jeby Sun * */ class MouseEventListener implements MouseInputListener { Point origin; //鼠標(biāo)拖拽想要移動(dòng)的目標(biāo)組件 MyFrame frame; public MouseEventListener(MyFrame frame) { this .frame = frame; origin = new Point(); } @Override public void mouseClicked(MouseEvent e) {} /** * 記錄鼠標(biāo)按下時(shí)的點(diǎn) */ @Override public void mousePressed(MouseEvent e) { origin.x = e.getX(); origin.y = e.getY(); } @Override public void mouseReleased(MouseEvent e) {} /** * 鼠標(biāo)移進(jìn)標(biāo)題欄時(shí),設(shè)置鼠標(biāo)圖標(biāo)為移動(dòng)圖標(biāo) */ @Override public void mouseEntered(MouseEvent e) { this .frame.setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR)); } /** * 鼠標(biāo)移出標(biāo)題欄時(shí),設(shè)置鼠標(biāo)圖標(biāo)為默認(rèn)指針 */ @Override public void mouseExited(MouseEvent e) { this .frame.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); } /** * 鼠標(biāo)在標(biāo)題欄拖拽時(shí),設(shè)置窗口的坐標(biāo)位置 * 窗口新的坐標(biāo)位置 = 移動(dòng)前坐標(biāo)位置+(鼠標(biāo)指針當(dāng)前坐標(biāo)-鼠標(biāo)按下時(shí)指針的位置) */ @Override public void mouseDragged(MouseEvent e) { Point p = this .frame.getLocation(); this .frame.setLocation( p.x + (e.getX() - origin.x), p.y + (e.getY() - origin.y)); } @Override public void mouseMoved(MouseEvent e) {} } public static void main(String[] args) { new MyFrame(); } } |