01.package com.technology.lanxj.frame;
02.
03.import java.awt.BorderLayout;
04.import java.awt.Dimension;
05.import java.awt.Toolkit;
06.import java.awt.event.MouseAdapter;
07.import java.awt.event.MouseEvent;
08.import java.io.IOException;
09.
10.import javax.swing.JEditorPane;
11.import javax.swing.JWindow;
12.import javax.swing.event.HyperlinkEvent;
13.import javax.swing.event.HyperlinkListener;
14.import javax.swing.event.HyperlinkEvent.EventType;
15.
16./**
17. * 消息窗口实例
18. * @author <a href="mailto:nanjifengchen@163.com">LanXJ</a>
19. */
20.public class WindowMessage extends JWindow implements HyperlinkListener,Runnable{//继承JWindow是为了实现window,实现Runnable是为了线程控制窗口位置
21.
22. //定义枚举值
23. public enum MOUSEOVER_TYPE{
24. FAST("当鼠标移动到窗口上时候,迅速将窗口的位置调整到初始化完成的结果位置(极速效果)")
25. ,SLOWLY("当鼠标移动到窗口上时候,系统慢慢将窗口的位置调整到初始化完成的结果位置(滑出效果)");
26. String info;
27. MOUSEOVER_TYPE(String info){
28. this.info=info;
29. }
30. public String toString(){
31. return info;
32. }
33. }
34. public enum MESSAGE_WINDOW_STATE{
35. NOINIT,OPENNING,OPEN,CLOSING,CLOSED
36. }
37.
38. private MESSAGE_WINDOW_STATE messageWindowState;
39. private MOUSEOVER_TYPE mouseOverType;//默认是极速效果
40. private Integer screenWidth,screenHeight,msgWindowWidth,msgWindowHeight,stateBarHeight;
41. private String title,msg;
42. private JEditorPane jEditorPane=new JEditorPane();//一款可以支持HTML的组件
43. boolean canHidden=true;
44.
45.
46. private void initValue(String width,String height,String title,String msg){
47. this.messageWindowState=MESSAGE_WINDOW_STATE.NOINIT;
48.// this.mouseOverType=MOUSEOVER_TYPE.FAST;//默认是极速效果 `P
49. this.mouseOverType=MOUSEOVER_TYPE.SLOWLY;//默认是滑出效果
50.
51. Dimension dimension=Toolkit.getDefaultToolkit().getScreenSize();//获取屏幕大小
52. this.screenWidth=dimension.width;//屏幕宽度
53. this.screenHeight=dimension.height;//屏幕高度
54. this.msgWindowWidth=Integer.parseInt(width);//消息窗体高度
55. this.msgWindowHeight=Integer.parseInt(height);//消息窗体宽度
56. this.stateBarHeight=Toolkit.getDefaultToolkit().getScreenInsets(getGraphicsConfiguration()).bottom;//获取任务栏高度
57. this.title=title;//消息窗体标题头
58. this.msg=msg;//消息内容
59. }
60.
61. /**
62. * 初始化window
63. */
64. private void initWindow(){
65. this.setSize(this.msgWindowWidth, this.msgWindowHeight);//设置窗口大小
66. this.setLayout(new BorderLayout());//设置布局
67.
68. jEditorPane.setEditable(false);//设置jEditorPane不可编辑
69. jEditorPane.setSize(this.msgWindowWidth, this.msgWindowHeight);//设置jEditorPane大小
70. jEditorPane.setContentType("text/html");//设置jEditorPane的现实格式
71. //设置窗口内容
72. jEditorPane.setText(
73. "<html>" +
74. "<head></head>" +
75. "<body>" +
76. "<table border=0 cellspacing=0 width=100% height=100%>" +
77. "<tr style='height:30;background-color:#81A6C8;'>" +
78. "<td align=left>"+
79. this.title+
80. "</td>" +
81. "<td align=right><a href="http://closeMsgWindow" style="cursor:hand;">关闭</a></td>" +
82. "</tr>" +
83. "<tr style="background-color:#E0F2FF;" +
84. "<td valign=top align=left colspan=2 height="+(this.msgWindowHeight-30)+">"+
85. this.msg+
86. "<br><br><center>友情链接:<a href="http://tieba.baidu.com">百度贴吧</a></center>" +
87. "</td>" +
88. "</tr>" +
89. "</table>" +
90. "</body>");
91.
92. this.getContentPane().add(jEditorPane,BorderLayout.CENTER);//将jEditorPane加入window中
93. this.setAlwaysOnTop(true);//设置窗体总在最前
94. this.setLocation(this.screenWidth-this.msgWindowWidth, this.screenHeight);//初始化窗体位置
95. }
96. private void addListener(){
97. /** 为jEditorPane加事件监听,实现鼠标进入后暂时还原消息窗体的功能 */
98. jEditorPane.addMouseListener(new MouseAdapter(){
99. public void mouseEntered(MouseEvent e) {
100. if(mouseOverType==MOUSEOVER_TYPE.FAST){//根据配置快速显示消息窗体
101. System.err.println("mouse in");
102. WindowMessage.this.setLocation(WindowMessage.this.screenWidth-WindowMessage.this.msgWindowWidth,
103. WindowMessage.this.screenHeight-WindowMessage.this.msgWindowHeight-WindowMessage.this.stateBarHeight);
104. }
105. WindowMessage.this.canHidden=false;//鼠标进入 修改隐藏条件为不成立
106. }
107.
108. public void mouseExited(MouseEvent e) {
109. System.err.println("mouse out");
110. WindowMessage.this.canHidden=true;
111. }
112. });
113. /** 为jEditorPane加Hyperlink事件监听,实现点击链接的功能(包括关闭窗体的功能) */
114. jEditorPane.addHyperlinkListener(this);
115. }
116.
117. public WindowMessage(String width,String height,String title,String msg){
118. initValue(width, height, title, msg);
119. initWindow();
120. addListener();
121. this.setVisible(true);
122. new Thread(this).run();
123. }
124.
125. public void hyperlinkUpdate(HyperlinkEvent e) {
126. if(e.getEventType()==EventType.ACTIVATED){
127. if(e.getURL().toString().equals("http://closeMsgWindow")){
128. System.exit(0);//关闭程序窗体
129. }else{
130. try {
131. Runtime.getRuntime().exec("explorer.exe "+e.getURL());//打开链接
132. } catch (IOException e1) {
133. e1.printStackTrace();
134. }
135. }
136. }
137. }
138. public void run() {
139. messageWindowState=MESSAGE_WINDOW_STATE.OPENNING;
140. begin:while(true){
141. //窗体显示
142. while(this.getLocation().y>this.screenHeight-this.msgWindowHeight-this.stateBarHeight){
143. this.setLocation(this.getLocation().x, this.getLocation().y-1);
144. try {
145. Thread.sleep(10);
146. } catch (Exception e) {
147. e.printStackTrace();
148. }
149. }
150. messageWindowState=MESSAGE_WINDOW_STATE.OPEN;
151. try {
152. Thread.sleep(5*1000);//窗体显示5秒钟
153. } catch (Exception e) {
154. e.printStackTrace();
155. }
156. messageWindowState=MESSAGE_WINDOW_STATE.CLOSING;
157. while(this.getLocation().y<this.screenHeight){
158. if(canHidden){
159. this.setLocation(this.getLocation().x, this.getLocation().y+1);
160. }else{//如果canHidden窗体隐藏条件不成立,就把窗体还原
161. continue begin;
162. }
163. try {
164. Thread.sleep(10);
165. } catch (Exception e) {
166. e.printStackTrace();
167. }
168. }
169. messageWindowState=MESSAGE_WINDOW_STATE.CLOSED;
170. System.exit(0);
171. break;
172. }
173. }
174.
175. public static void main(String[] args) {
176. new WindowMessage("250","150","标题","消息:来自消息窗体的提示信息,你可以定义自己的消息体(包括HTML代码体,将会将代码解析到table中。)");
177. }
178.}
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/CalabashGourd/archive/2010/12/14/6075566.aspx