InfoUtil.java
7.37 KB
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
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
package com.cnlive.shenhe.utils;
import org.springframework.stereotype.Component;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.net.URI;
/**
* * @author Administrator 此工具类用法:实例化出对象,调用 void show("标题","内容") 方法. InfoUtil tool
* * = new InfoUtil(); tool.show("标题","内容")
*
*/
@Component
public class InfoUtil {
private TipWindow tw = null; // 提示框
private JPanel headPan = null;
private JPanel feaPan = null;
private JPanel btnPan = null;
private JLabel title = null; // 栏目名称
private JLabel head = null; // 蓝色标题
private JLabel close = null; // 关闭按钮
private JTextArea feature = null; // 内容
private JScrollPane jfeaPan = null;
private JButton sure = null;
private String titleT = null;
private String word = null;
private Desktop desktop = null;
// private SimpleDateFormat sdf = new
// SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public void init() {
// 新建300x180的消息提示框
tw = new TipWindow(300, 180);
headPan = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0));
feaPan = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0));
btnPan = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0));
title = new JLabel("欢迎使用本系统");
head = new JLabel(titleT);
close = new JLabel(" x");
feature = new JTextArea(word);
jfeaPan = new JScrollPane(feature);
sure = new JButton("查看");
sure.setHorizontalAlignment(SwingConstants.CENTER);
// 设置提示框的边框,宽度和颜色
tw.getRootPane().setBorder(
BorderFactory.createMatteBorder(1, 1, 1, 1, Color.white));
title.setPreferredSize(new Dimension(260, 26));
title.setVerticalTextPosition(JLabel.CENTER);
title.setHorizontalTextPosition(JLabel.CENTER);
title.setFont(new Font("宋体", Font.PLAIN, 12));
title.setForeground(Color.black);
close.setFont(new Font("Arial", Font.BOLD, 15));
close.setPreferredSize(new Dimension(20, 20));
close.setVerticalTextPosition(JLabel.CENTER);
close.setHorizontalTextPosition(JLabel.CENTER);
close.setCursor(new Cursor(12));
close.setToolTipText("关闭");
head.setPreferredSize(new Dimension(250, 35));
head.setVerticalTextPosition(JLabel.CENTER);
head.setHorizontalTextPosition(JLabel.CENTER);
head.setFont(new Font("宋体", Font.PLAIN, 14));
head.setForeground(Color.black);
feature.setEditable(false);
feature.setForeground(Color.BLACK);
feature.setFont(new Font("宋体", Font.PLAIN, 13));
feature.setBackground(new Color(255, 255, 255));
// 设置文本域自动换行
feature.setLineWrap(true);
jfeaPan.setPreferredSize(new Dimension(260, 100));
jfeaPan.setBorder(null);
jfeaPan.setBackground(Color.black);
tw.setBackground(Color.white);
// 为了隐藏文本域,加个空的JLabel将他挤到下面去
JLabel jsp = new JLabel();
jsp.setPreferredSize(new Dimension(300, 15));
sure.setPreferredSize(new Dimension(60, 30));
// 设置标签鼠标手形
sure.setCursor(new Cursor(12));
// 设置button外观
sure.setContentAreaFilled(false);
sure.setBorder(BorderFactory.createRaisedBevelBorder());
sure.setBackground(Color.gray);
// headPan.add(title);
headPan.add(head);
headPan.add(close);
feaPan.add(jsp);
feaPan.add(jfeaPan);
// feaPan.add(releaseLabel);
btnPan.add(sure);
headPan.setBackground(new Color(104, 141, 177));
feaPan.setBackground(Color.white);
btnPan.setBackground(Color.white);
tw.add(headPan, BorderLayout.NORTH);
tw.add(feaPan, BorderLayout.CENTER);
tw.add(btnPan, BorderLayout.SOUTH);
}
public void handle(String url) {
// 为更新按钮增加相应的事件
desktop = Desktop.getDesktop();
sure.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
//注释代码为点击确认之后跳转到指定网页
try {
desktop.browse(new URI(url));
} catch (Exception e1) {
e1.printStackTrace();
}
tw.close();
}
@Override
public void mouseEntered(MouseEvent e) {
sure.setBorder(BorderFactory.createLineBorder(Color.gray));
}
@Override
public void mouseExited(MouseEvent e) {
sure.setBorder(null);
}
});
// 右上角关闭按钮事件
close.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
tw.close();
}
@Override
public void mouseEntered(MouseEvent e) {
close.setBorder(BorderFactory.createLineBorder(Color.gray));
}
@Override
public void mouseExited(MouseEvent e) {
close.setBorder(null);
}
});
}
public void show(String titleT, String word, String url) {
this.titleT = titleT;
this.word = word;
// time = sdf.format(new Date());
init();
handle(url);
tw.setAlwaysOnTop(true);
tw.setUndecorated(true);
tw.setResizable(false);
tw.setVisible(true);
tw.run();
}
public void close() {
tw.close();
}
}
class TipWindow extends JDialog {
private static final long serialVersionUID = 8541659783234673950L;
private static Dimension dim;
private int x, y;
private int width, height;
private static Insets screenInsets;
public TipWindow(int width, int height) {
this.width = width;
this.height = height;
dim = Toolkit.getDefaultToolkit().getScreenSize();
screenInsets = Toolkit.getDefaultToolkit().getScreenInsets(
this.getGraphicsConfiguration());
x = (int) (dim.getWidth() - width - 3);
y = (int) (dim.getHeight() - screenInsets.bottom - 3);
initComponents();
}
public void run() {
for (int i = 0; i <= height; i += 10) {
try {
this.setLocation(x, y - i);
Thread.sleep(50);
} catch (InterruptedException ex) {
}
}
// 此处代码用来实现让消息提示框10秒后自动消失
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
close();
}
private void initComponents() {
this.setSize(width, height);
this.setLocation(x, y);
this.setBackground(Color.black);
}
public void close() {
x = this.getX();
y = this.getY();
int ybottom = (int) dim.getHeight() - screenInsets.bottom;
for (int i = 0; i <= ybottom - y; i += 10) {
try {
setLocation(x, y + i);
Thread.sleep(50);
} catch (InterruptedException ex) {
}
}
dispose();
}
}