java中怎么实现桌面右下角弹窗效果
创始人
2024-12-22 07:23:37
0次

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

相关内容

热门资讯

空调连续工作多久需要停机休息 空调连续工作的时间取决于多个因素,包括空调的型号、功率、使用环境和维护情况等。一般来说,空调可以连续...
空调哪个符号是除湿的 空调除湿功能通常用一个水滴形状的符号表示。不同品牌和型号的空调遥控器可能会有所差异,但一般来说,除湿...
格力空调多长时间加一次氟利昂 1、一般情况下,空调对氟利昂的使用量是非常小的,所以在空调使用了四年左右的时候再考虑加也没问题。2、...
空调用多长时间需要加氟一次 一般情况下是五年。当出现以下这几种情况时,空调才需要加氟。1、空调的室内机和室外机各有两个接口,当接...
格调生活·C标思享荟:卡萨帝在... 9月12日的郑州龙湖金融岛,夜风轻拂,灯光璀璨。“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑...
品牌价值破千亿、高端份额持续领... 9月12日,“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑州站”在郑州龙湖金融岛举办。2026...
空调主板坏了修800还是换新的 空调主板坏了是修还是换新,需要考虑多个因素,以下是一些建议:1. 维修成本:如果维修费用较低,比如 ...
空调开多久需要休息多长时间 问题:空调开多久需要休息多长时间回答:1、在使用空调的过程中,一般空调开7~8个小时左右需要休息2~...
中央空调多长时间清洗一次 1、一般空调开机前清洗一次即可,空调开机中间时段再清洗一次,空调关机时清洗一次,这样比较合理。2、空...
中央空调氟利昂多久加一次 看空调制热和制冷程度,很多人的观点认为氟利昂必须一年放一次,其实这个观点是错误的,理解上面有点偏差,...