首页 > 编程语言 >java实现桌面右下角弹窗效果

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

时间:2022-09-02 17:35:24浏览次数:98  
标签:java 右下角 private import close new JLabel 弹窗

http://www.3qphp.com/java/framework/3542.html

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, "这是一个弹窗测试!");
 }
 
}

效果如下:

标签:java,右下角,private,import,close,new,JLabel,弹窗
From: https://www.cnblogs.com/hanby/p/16650661.html

相关文章

  • 防止JAVA程序重复启动的一个另类解决办法
    http://blog.itpub.net/443058/viewspace-915510/我们项目中有一个后台任务处理程序,是java开发application,用以处理网站提交的一些批量数据文件,因为这些数据文件数据量一......
  • Java 在Word文档中添加艺术字
    艺术字是以普通文字为基础,经过专业的字体设计师艺术加工的变形字体。字体特点符合文字含义、具有美观有趣、易认易识、醒目张扬等特性,是一种有图案意味或装饰意味的字体变......
  • java下载csv乱码问题
    一.问题。用EXCEL打开文件时,总是产生乱码,但是用NOTEPAD++打开时,显示正常。然后,在NOTEPADD++的“格式”工具栏中查了一下文件编码,发现是“以UTF-8格式编码”。二.解决......
  • 【校招VIP】[Java][一本][6分]按照真实的技能点进行业务描述
    关注【校招VIP】公众号,回复【简历】,添加校招顾问微信,即可获取简历指导!本份简历是一位21届一本java同学的简历,简历评分6分。一、学员简历  二、指导意见简历版式问......
  • 【校招VIP】[Java][211][7分]重点展开与java开发有关的项目
    关注【校招VIP】公众号,回复【简历】,添加校招顾问微信,即可获取简历指导!本份简历是一位21届211硕java同学的简历,简历评分7分。一、学员简历  二、指导意见简历版式......
  • vue3项目在页面退出时弹窗确认--用vant组件Dialog弹窗在路由改变的时候不生效问题
    写vue3的H5项目的时候有个需求是回退时弹窗确认是否退出当前页面一、第一个办法------使用onbeforeRouteLeave路由钩子constformRouteAbi=localStorage.getItem("fo......
  • java 上传读取 excel
    implementationgroup:'org.apache.poi',name:'poi',version:'5.2.2' implementationgroup:'org.apache.poi',name:'poi-ooxml',version:'5.2.2'//exce......
  • java中List的详细用法
    目录:list中添加,获取,删除元素;list中是否包含某个元素;list中根据索引将元素数值改变(替换);list中查看(判断)元素的索引;根据元素索引位置进行的判断;利用list中索引位置重新生成......
  • Java try catch与throws区别
    原则:如果该功能内部可以将问题处理,用try,如果处理不了,交由调用者处理,这时用throws区别:当前程序需要继续运行就try当前程序不需要继续运行就throws举例:感冒了......
  • Java中具有继承的对象序列化
    在序列化中,当引入继承时,则根据超类和子类定义了某些情况,这使对每种情况下的序列化的理解变得更加简单。应遵循的基本规则如下。1.当超类实现时,可序列化接口而子类则不。......