com.sun.awt.AWTUtilities.setWindowOpacity相关说明
在eclipse中(jdk1.6.*)版本中出现编译不通过而报错,报错是因为这个包里面的方法不属于jdk正式版本,也就是不能保证下个版本还存在,所以编译器会拒绝,你可以在eclipse中如下设置: 选择Window->Preferences->Java->Compiler->Errors/Warnings;然后选择Deprecated and restricted API,将 Deprecated API从ERROR改为Warning即可!
1 package Com.SwingTest; 2 3 4 import java.awt.BorderLayout; 5 import java.awt.Font; 6 import java.awt.GraphicsEnvironment; 7 import java.awt.Rectangle; 8 import java.awt.Window; 9 import javax.swing.JButton; 10 import javax.swing.JWindow; 11 12 import com.sun.awt.AWTUtilities; 13 14 public class SwingText_08 { 15 16 private int _height = 30; // 气泡提示高 17 private int _step = 60; // 设定循环的步长 18 private int _stepTime = 60; // 每步时间 19 private int _displayTime = 2000; // 显示时间 20 private Font _font; // 字体 21 22 /** 23 * 构造函数,初始化默认气泡提示设置 24 * 25 */ 26 public SwingText_08() 27 { 28 _font = new Font("微软雅黑", 0, 12);// 设定字体 29 try { 30 JWindow.class.getMethod("setAlwaysOnTop", 31 new Class[] { Boolean.class }); 32 } catch (Exception e) { 33 } 34 } 35 36 /** 37 * 重构JWindow用于显示单一气泡提示框 38 * 39 */ 40 class ToolTipSingle extends Window 41 { 42 private static final long serialVersionUID = 1L; 43 private JButton _message = new JButton(){ 44 /** 45 * 46 */ 47 private static final long serialVersionUID = 1L; 48 49 @Override 50 public void repaint() 51 { 52 } 53 }; 54 55 56 @SuppressWarnings("deprecation") 57 public ToolTipSingle(String msg) { 58 super(null); 59 60 int cntText=msg.length(); 61 int with = cntText*11+40; 62 int height = _height; 63 64 Rectangle screenRect = GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds(); 65 int screenHeight = (int) screenRect.height; 66 int screenWidth = (int) screenRect.width; 67 68 int posx = screenWidth/2-with/2; 69 int posy = screenHeight/2-height/2; 70 71 setBounds(posx, posy, with, height); 72 _message.setFont(getMessageFont()); 73 _message.setText(msg); 74 75 _message.setFocusPainted(false); 76 //_message.setEnabled(false); 77 add(_message, BorderLayout.CENTER); 78 79 setAlwaysOnTop(true); 80 setLocationRelativeTo(null); 81 82 show(true); 83 } 84 85 /** 86 * 动画开始 87 * 88 */ 89 public void animate() { 90 new Animation(this).start(); 91 } 92 } 93 94 /** 95 * 此类处则动画处理 96 * 97 */ 98 class Animation extends Thread 99 { 100 101 ToolTipSingle _single; 102 103 public Animation(ToolTipSingle single) { 104 this._single = single; 105 } 106 107 /** 108 * 开始动画处理 109 */ 110 @SuppressWarnings("deprecation") 111 public void run() { 112 try { 113 float value = 0.0f; 114 while((value+0.02)<1.0f) 115 { 116 value+=0.02f; 117 com.sun.awt.AWTUtilities.setWindowOpacity(_single, value); 118 Thread.sleep(10); 119 } 120 121 while(_displayTime>50) 122 { 123 Thread.sleep(50); 124 _displayTime = _displayTime-50; 125 } 126 127 value=1; 128 while((value-0.02)>0f) 129 { 130 value-=0.02f; 131 com.sun.awt.AWTUtilities.setWindowOpacity(_single, value); 132 Thread.sleep(20); 133 } 134 135 } catch (Exception e) { 136 throw new RuntimeException(e); 137 } 138 139 _single.show(false); 140 } 141 } 142 143 /** 144 * 设定显示信息 145 * 146 * @param icon 147 * @param msg 148 */ 149 public void show(String msg) { 150 new ToolTipSingle(msg).animate(); 151 } 152 153 154 /** 155 * 获得当前消息字体 156 * 157 * @return 158 */ 159 public Font getMessageFont() { 160 return _font; 161 } 162 163 /** 164 * 设置当前消息字体 165 * 166 * @param font 167 */ 168 public void setMessageFont(Font font) { 169 _font = font; 170 } 171 172 /** 173 * 获得显示时间 174 * 175 * @return 176 */ 177 public int getDisplayTime() { 178 return _displayTime; 179 } 180 181 /** 182 * 设置显示时间 183 * 184 * @param displayTime 185 */ 186 public void setDisplayTime(int displayTime) { 187 this._displayTime = displayTime; 188 } 189 190 191 /** 192 * 获得循环步长 193 * 194 * @return 195 */ 196 public int getStep() { 197 return _step; 198 } 199 200 /** 201 * 设定循环步长 202 * 203 * @param _step 204 */ 205 public void setStep(int _step) { 206 this._step = _step; 207 } 208 209 public int getStepTime() { 210 return _stepTime; 211 } 212 213 public void setStepTime(int _stepTime) { 214 this._stepTime = _stepTime; 215 } 216 217 218 public static void main(String[] argv) 219 { 220 new SwingText_08().show("全部任务已经载入完成!"); 221 } 222 }