ICE.ICE|

韩憨

园龄:4年7个月粉丝:42关注:47

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

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

效果如下:

本文作者:韩憨

本文链接:https://www.cnblogs.com/hanby/p/16650661.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   韩憨  阅读(157)  评论(0编辑  收藏  举报
//看板娘

哥伦布

评论
收藏
关注
推荐
深色
回顶
收起
  1. 1 隔离 (Studio Live Duet) 陈凯咏,林家谦
  2. 2 明知做戏 吴雨霏
  3. 3 残酷游戏 卫兰
  4. 4 你,好不好? 周兴哲
  5. 5 我可以 蔡旻佑
  6. 6 云烟成雨 房东的猫
  7. 7 说散就散 JC 陈咏桐
  8. 8 我配不上你 夏天Alex
  9. 9 不再联系 夏天Alex
  10. 10 等我先说 夏天Alex
  11. 11 我知道他爱你 夏天Alex
  12. 12 多想在平庸的生活拥抱你 隔壁老樊
  13. 13 这一生关于你的风景 隔壁老樊
  14. 14 我曾 隔壁老樊
  15. 15 关于孤独我想说的话 隔壁老樊
  16. 16 过客 周思涵
  17. 17 备爱 周思涵
  18. 18 嚣张 en
  19. 19 海口 后弦
明知做戏 - 吴雨霏
00:00 / 00:00
An audio error has occurred, player will skip forward in 2 seconds.

作词 : Xia Zhi

作曲 : Fong Man Leung

编曲 : 吴国恩

监制 : Gary Chan

等你的汽水喝一半给你加片薄冰

等你的桌面满泻我总会打理重整

不想纯情 不够聪明

你未发现我的身影

得我帮你依照编码整理家里电影

得我帮你依照编码整理家里电影

只会得我一个帮你选购喜爱铃声

天天如常 估你心情

等一个眼神求证 一闪擦过如流星

怎么我为我做过的感到惊怕

就像爱吗我也不肯定恐怕

我以为存在吗 千变万化

从来不肯开口可相信吗 离谱吗

请你不要阻我喜欢你

明明是爱但你未说话你扮作闪避

这个沉默冷静的你毫无办法处理

其实我亦怕是错摸心理

总有天会等到好天气

游行示爱大叫着你在某大片草地

等你无用退避不过仍然害羞的你

还是顾忌太不争气 明知做戏

即使你未太在意不感到惊讶

即使你未太在意不感到惊讶

现在要说爱你请准备招架

勇气还存在吗 不要害怕

随时真的胆敢亲手送花 离谱吗

请你不要阻我喜欢你

明明是爱但你未说话你扮作闪避

这个沉默冷静的你亳无办法处理

其实我亦怕是错摸心理

总有天会等到好天气

游行示爱大叫着你在某大片草地

等你无用退避不过仍然害羞的你

还是顾忌太不争气 明知做戏

不过不要阻我紧张你

如何令你愉快让我办妥为你准备

喜爱沉默冷静的你还是自信的你

仍愿意为你造一些惊喜

总有天会等到好天气

游行示爱大叫着你在某大片草地

等你无用退避不过途人目光不理

期待贴着你的手臂 无须做戏

等你喜爱等你不爱就凭摘毫验证

等你喜爱等你不爱就凭摘毫验证

想爱不爱偏爱不理亦同样难划清

天天如常 估你心情

不想扑索来求证 争取过趁还年青

终于你下决定来答应 太动听

点击右上角即可分享
微信分享提示