-
Notifications
You must be signed in to change notification settings - Fork 1
/
LinkLabel.java
70 lines (61 loc) · 1.88 KB
/
LinkLabel.java
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
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Desktop;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import javax.swing.JLabel;
public class LinkLabel extends JLabel {
/** 超链接显示的文字 */
private String text;
/** 保存连接 */
private URL link = null;
/** 保存标签的默认颜色 */
private Color preColor = null;
/** * 构造一个超链接 * vText 显示的文字 * vLink 连接地址 */
public LinkLabel(String vText, String vLink) {
super("<html>" + vText + "</html>");
this.text = vText;
LinkLabel.this.setFont(new Font("楷体",Font.BOLD,15));
LinkLabel.this.setForeground(Color.red);
try {
if (!vLink.startsWith("http://"))
vLink = "http://" + vLink;
this.link = new URL(vLink);
} catch (MalformedURLException err) {
err.printStackTrace();
}
this.addMouseListener(new MouseAdapter() {
@Override
public void mouseExited(MouseEvent e) {
LinkLabel.this.setCursor(Cursor
.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
if (preColor != null)
LinkLabel.this.setForeground(preColor);
LinkLabel.this.setText("<html>" + text + "</html>");
}
@Override
public void mouseEntered(MouseEvent e) {
LinkLabel.this.setCursor(Cursor
.getPredefinedCursor(Cursor.HAND_CURSOR));
preColor = LinkLabel.this.getForeground();
LinkLabel.this.setForeground(Color.BLUE);
LinkLabel.this.setText("<html><u>" + text + "</u></html>");
}
@Override
public void mouseClicked(MouseEvent e) {
try {
Desktop.getDesktop().browse(link.toURI());
} catch (IOException err) {
err.printStackTrace();
} catch (URISyntaxException err) {
err.printStackTrace();
}
}
});
}
}