java 小例子 桌面渐隐 tip

java 小例子 桌面渐隐 tip 话不多说 直接上代码


package service.com;


import java.awt.Dimension;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JRootPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import com.sun.awt.AWTUtilities;


@SuppressWarnings({ "restriction", "unused" })
public class TipWindow {


JFrame frame;
JLabel label;
JEditorPane editorPane;


private int width;// 窗体宽度
private int height;// 窗体高度
private int stayTime;// 休眠时间
private String title;// 消息标题
private String message;// 窗体内容
private int style;// 窗体样式


static {
try {
// 设置界面外观
UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (Exception e) {
e.printStackTrace();
}
}


/**
* @param width 提示框宽度
* @param height 提示框高度
* @param stayTime 提示框停留时间
* @param style 提示框的样式
* @param title 提示框标题
* @param message 提示框内容(支持HTML标签)
*/
public TipWindow(int width, int height, int stayTime, int style, String title, String message) {
this.width = width;
this.height = height;
this.stayTime = stayTime;
this.style = style;
this.title = title;
this.message = message;
}


public TipWindow(int stayTime, int style, String title, String message) {
this.width = 300;
this.height = 100;
this.stayTime = stayTime;
this.style = style;
this.title = title;
this.message = message;
}


public void initialize() {
frame = new JFrame();
editorPane = new JEditorPane();
editorPane.setEditable(false);
editorPane.setContentType("text/html");
editorPane.setText(message);
frame.add(editorPane);
frame.setTitle(title);
// 设置窗体的位置及大小
// Point location = MouseInfo.getPointerInfo().getLocation();
// frame.setBounds((int)location.getX(), (int)location.getY(), width,
// height);
Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize();
int w = (int) screensize.getWidth();
int h = (int) screensize.getHeight();
frame.setBounds((w - width - 20), (h - height - 100), width, height);
frame.setUndecorated(true);// 去掉窗口的装饰
frame.getRootPane().setWindowDecorationStyle(style);//设置窗体样式
// AWTUtilities.setWindowOpacity(frame, 0);//初始化透明度
frame.setVisible(true);
frame.setAlwaysOnTop(true);// 窗体置顶
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
hide();
}
});
}


// 窗体逐渐变清晰
public void show() {
for (int i = 1; i < 20; i++) {
try {
Thread.sleep(50);
} catch (Exception e) {
AWTUtilities.setWindowOpacity(frame, i * 0.05F);
}
}
}


// 窗体逐渐变淡甚至消失
public void hide() {
float opacity = 100;
while (true) {
if (opacity < 2) {
break;
}


opacity -= 2;
AWTUtilities.setWindowOpacity(frame, opacity / 100);
try {
Thread.sleep(150);
} catch (Exception e) {
e.printStackTrace();
}
}
frame.dispose();
}


public void run() {
initialize();
show();
try {
Thread.sleep(stayTime * 1000);
} catch (Exception e) {
e.printStackTrace();
}
hide();
}


public static void main(String[] args) {
String title = "友情提示!";
String message = "主人!<br />该休息了!";
TipWindow tipWindow = new TipWindow(2, JRootPane.QUESTION_DIALOG, title, message);
tipWindow.run();
}
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值