java实现桌面右下角弹窗效果

 更新时间:2019年07月31日 10:02:10   投稿:lijiao  
这篇文章主要为大家详细介绍了java实现桌面右下角弹窗效果,模仿类似于qq消息弹窗,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

最近需要一个java实现桌面弹窗的小功能,类似于电脑桌面右下角的小广告一样的功能,在csdn上找到一个很好的一个,功能很多,我去除了一点不需要的代码,改了下外观等。

修改后的代码如下:

InfoUtil.java

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Desktop;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingConstants;
import java.awt.Insets;
import java.awt.Toolkit;
import javax.swing.JDialog;
 
/**
 * @author Administrator 此工具类用法:实例化出对象,调用 void show("标题","内容") 方法. InfoUtil tool
 *     = new InfoUtil(); tool.show("标题","内容")
 */
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() {
    // 为更新按钮增加相应的事件
    desktop = Desktop.getDesktop();
    sure.addMouseListener(new MouseAdapter() {
      public void mouseClicked(MouseEvent e) {
        //注释代码为点击确认之后跳转到指定网页
        // try {
        // desktop.browse(new URI("https://www.baidu.com"));
        // } catch (Exception e1) {
        // e1.printStackTrace();
        // }
        tw.close();
      }
 
      public void mouseEntered(MouseEvent e) {
        sure.setBorder(BorderFactory.createLineBorder(Color.gray));
      }
 
      public void mouseExited(MouseEvent e) {
        sure.setBorder(null);
      }
    });
    // 右上角关闭按钮事件
    close.addMouseListener(new MouseAdapter() {
      public void mouseClicked(MouseEvent e) {
        tw.close();
      }
 
      public void mouseEntered(MouseEvent e) {
        close.setBorder(BorderFactory.createLineBorder(Color.gray));
      }
 
      public void mouseExited(MouseEvent e) {
        close.setBorder(null);
      }
    });
  }
 
  public void show(String titleT, String word) {
    this.titleT = titleT;
    this.word = word;
    // time = sdf.format(new Date());
    init();
    handle();
    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) {
      }
    }
    // 此处代码用来实现让消息提示框6秒后自动消失
    try {
      Thread.sleep(6000);
    } 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();
  }
 
}

main.java

public class main {
 private final static String TITLE="弹窗";
 public static void main(String[] args) {
 InfoUtil test = new InfoUtil();
 test.show(TITLE, "这是一个弹窗测试!");
 }
 
}

效果如下:


特此纪录!

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Spring createBeanInstance实例化Bean

    Spring createBeanInstance实例化Bean

    这篇文章主要为大家介绍了Spring createBeanInstance实例化Bean源码解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-03-03
  • 如何通过jstack命令dump线程信息

    如何通过jstack命令dump线程信息

    这篇文章主要介绍了如何通过jstack命令dump线程信息,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-07-07
  • SpringMVC文件上传 多文件上传实例

    SpringMVC文件上传 多文件上传实例

    这篇文章主要介绍了SpringMVC文件上传 多文件上传实例,有需要的朋友可以参考一下
    2014-01-01
  • 解决Spring或SpringBoot开启事务以后无法返回自增主键的问题

    解决Spring或SpringBoot开启事务以后无法返回自增主键的问题

    这篇文章主要介绍了解决Spring或SpringBoot开启事务以后无法返回自增主键的问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-07-07
  • 通过RedisTemplate连接多个Redis过程解析

    通过RedisTemplate连接多个Redis过程解析

    这篇文章主要介绍了通过RedisTemplate连接多个Redis过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-08-08
  • SpringBoot Security实现单点登出并清除所有token

    SpringBoot Security实现单点登出并清除所有token

    Spring Security是一个功能强大且高度可定制的身份验证和访问控制框架。提供了完善的认证机制和方法级的授权功能。是一款非常优秀的权限管理框架。它的核心是一组过滤器链,不同的功能经由不同的过滤器
    2023-01-01
  • Lombok如何快速构建JavaBean与日志输出

    Lombok如何快速构建JavaBean与日志输出

    这篇文章主要介绍了Lombok如何快速构建JavaBean与日志输出,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-03-03
  • Java实现stream的三个常用方式(toMap,groupingBy,findFirst)

    Java实现stream的三个常用方式(toMap,groupingBy,findFirst)

    本文主要介绍了Java实现stream的三个常用方式,主要包括toMap,groupingBy,findFirst,具有一定的参考价值,感兴趣的可以了解一下
    2023-10-10
  • java多线程:基础详解

    java多线程:基础详解

    这篇文章主要介绍了java多线程编程实例,分享了几则多线程的实例代码,具有一定参考价值,加深多线程编程的理解还是很有帮助的,需要的朋友可以参考下。
    2021-08-08
  • Springboot根据配置文件动态注入接口实现类详解

    Springboot根据配置文件动态注入接口实现类详解

    这篇文章主要介绍了Springboot根据配置文件动态注入接口实现类详解,具有很好的参考价值,希望对大家有所帮助,需要的朋友可以参考下,如有错误或未考虑完全的地方,望不吝赐教
    2023-10-10

最新评论