【转】Java实现屏幕右下角弹出类QQ提示消息
1
import
java.awt.BorderLayout;
2
import
java.awt.Color;
3
import
java.awt.Cursor;
4
import
java.awt.Dimension;
5
import
java.awt.FlowLayout;
6
import
java.awt.Font;
7
import
java.awt.Point;
8
import
java.awt.event.MouseAdapter;
9
import
java.awt.event.MouseEvent;
10
import
java.awt.event.MouseMotionAdapter;
11
import
java.text.SimpleDateFormat;
12
import
java.util.Date;
13
import
java.util.HashMap;
14
import
java.util.Map;
15
import
javax.swing.BorderFactory;
16
import
javax.swing.ImageIcon;
17
import
javax.swing.JLabel;
18
import
javax.swing.JOptionPane;
19
import
javax.swing.JPanel;
20
import
javax.swing.JScrollPane;
21
import
javax.swing.JTextArea;
22
import
javax.swing.SwingConstants;
23
import
java.awt.Insets;
24
import
java.awt.Toolkit;
25
import
javax.swing.JDialog;
26![]()
27![]()
public
class
VersionUtil
{
28
private Map<String, String> feaMap = null;
29
private Point oldP;//上一次坐标,拖动窗口时用
30
private TipWindow tw = null;//提示框
31
private ImageIcon img = null;//图像组件
32
private JLabel imgLabel = null; //背景图片标签
33
private JPanel headPan = null;
34
private JPanel feaPan =null;
35
private JPanel btnPan = null;
36
private JLabel title = null;
37
private JLabel head = null;
38
private JLabel close = null;//关闭按钮
39
private JTextArea feature = null;
40
private JScrollPane jfeaPan = null;
41
private JLabel releaseLabel = null;
42
private JLabel sure = null;
43
private SimpleDateFormat sdf=null;
44
45![]()
{
46
sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
47
feaMap = new HashMap<String, String>();
48
feaMap.put("name", "中国信息大学固定资产管理系统");
49
feaMap.put("release", sdf.format(new Date()));
50
feaMap.put("feature", "1.开发环境:windows\n2.开发语言:java\n3.开发工具:Eclipse3.2\n4.数据库类型:SQL Server2005\n5.开发人员:花新昌\n6.联系方式:15210477080");
51
}
52
53![]()
public VersionUtil()
{
54
init();
55
handle();
56
tw.setAlwaysOnTop(true);
57
tw.setUndecorated(true);
58
tw.setResizable(false);
59
tw.setVisible(true);
60
tw.run();
61
}
62![]()
public void init()
{
63
//新建300x220的消息提示框
64
tw = new TipWindow(300, 220);
65
img = new ImageIcon("background.gif");
66
imgLabel = new JLabel(img);
67
//设置各个面板的布局以及面板中控件的边界
68
headPan = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0));
69
feaPan = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0));
70
btnPan = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0));
71
title = new JLabel("欢迎使用本系统");
72
head = new JLabel(feaMap.get("name"));
73
close = new JLabel(" x");//关闭按钮
74
feature = new JTextArea(feaMap.get("feature"));
75
jfeaPan = new JScrollPane(feature);
76
releaseLabel = new JLabel("登录 " + feaMap.get("release"));
77
sure = new JLabel("确定");
78
sure.setHorizontalAlignment(SwingConstants.CENTER);
79
80
// 将各个面板设置为透明,否则看不到背景图片
81
((JPanel) tw.getContentPane()).setOpaque(false);
82
headPan.setOpaque(false);
83
feaPan.setOpaque(false);
84
btnPan.setOpaque(false);
85
86
//设置JDialog的整个背景图片
87
tw.getLayeredPane().add(imgLabel, new Integer(Integer.MIN_VALUE));
88
imgLabel.setBounds(0, 0, img.getIconWidth(), img.getIconHeight());
89
headPan.setPreferredSize(new Dimension(300, 60));
90
91
//设置提示框的边框,宽度和颜色
92
tw.getRootPane().setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.gray));
93
94
title.setPreferredSize(new Dimension(260, 26));
95
title.setVerticalTextPosition(JLabel.CENTER);
96
title.setHorizontalTextPosition(JLabel.CENTER);
97
title.setFont(new Font("宋体", Font.PLAIN, 12));
98
title.setForeground(Color.black);
99![]()
100
101
close.setFont(new Font("Arial", Font.BOLD, 15));
102
close.setPreferredSize(new Dimension(20, 20));
103
close.setVerticalTextPosition(JLabel.CENTER);
104
close.setHorizontalTextPosition(JLabel.CENTER);
105
close.setCursor(new Cursor(12));
106
close.setToolTipText("关闭");
107![]()
108
109
head.setPreferredSize(new Dimension(250, 35));
110
head.setVerticalTextPosition(JLabel.CENTER);
111
head.setHorizontalTextPosition(JLabel.CENTER);
112
head.setFont(new Font("宋体", Font.PLAIN, 12));
113
head.setForeground(Color.blue);
114![]()
115
116
feature.setEditable(false);
117
feature.setForeground(Color.red);
118
feature.setFont(new Font("宋体", Font.PLAIN, 13));
119
feature.setBackground(new Color(184, 230, 172));
120
//设置文本域自动换行
121
feature.setLineWrap(true);
122![]()
123
jfeaPan.setPreferredSize(new Dimension(250, 80));
124
jfeaPan.setBorder(null);
125
jfeaPan.setBackground(Color.black);
126
127
releaseLabel.setForeground(Color.DARK_GRAY);
128
releaseLabel.setFont(new Font("宋体", Font.PLAIN, 12));
129
130
//为了隐藏文本域,加个空的JLabel将他挤到下面去
131
JLabel jsp = new JLabel();
132
jsp.setPreferredSize(new Dimension(300, 25));
133
134
sure.setPreferredSize(new Dimension(110, 30));
135
//设置标签鼠标手形
136
sure.setCursor(new Cursor(12));
137![]()
138
headPan.add(title);
139
headPan.add(close);
140
headPan.add(head);
141
142
feaPan.add(jsp);
143
feaPan.add(jfeaPan);
144
feaPan.add(releaseLabel);
145
146
btnPan.add(sure);
147
148
tw.add(headPan, BorderLayout.NORTH);
149
tw.add(feaPan, BorderLayout.CENTER);
150
tw.add(btnPan, BorderLayout.SOUTH);
151
}
152
153![]()
public void handle()
{
154
//为更新按钮增加相应的事件
155![]()
sure.addMouseListener(new MouseAdapter()
{
156![]()
public void mouseClicked(MouseEvent e)
{
157
JOptionPane.showMessageDialog(tw, "谢谢,再见");
158
tw.close();
159
}
160![]()
public void mouseEntered(MouseEvent e)
{
161
sure.setBorder(BorderFactory.createLineBorder(Color.gray));
162
}
163![]()
public void mouseExited(MouseEvent e)
{
164
sure.setBorder(null);
165
}
166
});
167
//增加鼠标拖动事件
168![]()
title.addMouseMotionListener(new MouseMotionAdapter()
{
169![]()
public void mouseDragged(MouseEvent e)
{
170
// TODO Auto-generated method stub
171
Point newP = new Point(e.getXOnScreen(), e.getYOnScreen());
172
int x = tw.getX() + (newP.x - oldP.x);
173
int y = tw.getY() + (newP.y - oldP.y);
174
tw.setLocation(x, y);
175
oldP=newP;
176
177
}
178
});
179
//鼠标按下时初始坐标,供拖动时计算用
180![]()
title.addMouseListener(new MouseAdapter()
{
181![]()
public void mousePressed(MouseEvent e)
{
182
oldP = new Point(e.getXOnScreen(), e.getYOnScreen());
183
}
184
});
185
186
//右上角关闭按钮事件
187![]()
close.addMouseListener(new MouseAdapter()
{
188![]()
public void mouseClicked(MouseEvent e)
{
189
tw.close();
190
}
191![]()
192![]()
public void mouseEntered(MouseEvent e)
{
193
close.setBorder(BorderFactory.createLineBorder(Color.gray));
194
}
195![]()
196![]()
public void mouseExited(MouseEvent e)
{
197
close.setBorder(null);
198
}
199
});
200![]()
201
}
202![]()
public static void main(String args[])
{
203
new VersionUtil();
204
}
205
}
206![]()
class
TipWindow
extends
JDialog
{
207
private static final long serialVersionUID = 8541659783234673950L;
208
private static Dimension dim;
209
private int x, y;
210
private int width, height;
211
private static Insets screenInsets;
212
213
214![]()
public TipWindow(int width,int height)
{
215
this.width=width;
216
this.height=height;
217
dim = Toolkit.getDefaultToolkit().getScreenSize();
218
screenInsets = Toolkit.getDefaultToolkit().getScreenInsets(this.getGraphicsConfiguration());
219
x = (int) (dim.getWidth() - width-3);
220
y = (int) (dim.getHeight()-screenInsets.bottom-3);
221
initComponents();
222
}
223![]()
public void run()
{
224![]()
for (int i = 0; i <= height; i += 10)
{
225![]()
try
{
226
this.setLocation(x, y - i);
227
Thread.sleep(5);
228![]()
} catch (InterruptedException ex)
{
229
}
230
}
231
//此处代码用来实现让消息提示框5秒后自动消失
232![]()
try
{
233
Thread.sleep(5000);
234![]()
} catch (InterruptedException e)
{
235
// TODO Auto-generated catch block
236
e.printStackTrace();
237
}
238
close();
239
}
240![]()
private void initComponents()
{
241
this.setSize(width, height);
242
this.setLocation(x, y);
243
this.setBackground(Color.black);
244
}
245![]()
public void close()
{
246
x=this.getX();
247
y=this.getY();
248
int ybottom=(int)dim.getHeight()-screenInsets.bottom;
249![]()
for (int i = 0; i <= ybottom-y; i += 10)
{
250![]()
try
{
251
setLocation(x, y+i);
252
Thread.sleep(5);
253![]()
} catch (InterruptedException ex)
{
254
}
255
}
256
dispose();
257
}
258
}
259![]()
260
二。运行效果
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153

154
155

156

157
158
159
160

161
162
163

164
165
166
167
168

169

170
171
172
173
174
175
176
177
178
179
180

181

182
183
184
185
186
187

188

189
190
191
192

193
194
195
196

197
198
199
200
201
202

203
204
205
206

207
208
209
210
211
212
213
214

215
216
217
218
219
220
221
222
223

224

225

226
227
228

229
230
231
232

233
234

235
236
237
238
239
240

241
242
243
244
245

246
247
248
249

250

251
252
253

254
255
256
257
258
259
260
程序中使用的背景图如下: