欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java實(shí)現(xiàn)桌面右下角彈窗效果

 更新時(shí)間:2019年07月31日 10:02:10   投稿:lijiao  
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)桌面右下角彈窗效果,模仿類似于qq消息彈窗,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

最近需要一個(gè)java實(shí)現(xiàn)桌面彈窗的小功能,類似于電腦桌面右下角的小廣告一樣的功能,在csdn上找到一個(gè)很好的一個(gè),功能很多,我去除了一點(diǎn)不需要的代碼,改了下外觀等。

修改后的代碼如下:

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 此工具類用法:實(shí)例化出對(duì)象,調(diào)用 void show("標(biāo)題","內(nèi)容") 方法. InfoUtil tool
 *     = new InfoUtil(); tool.show("標(biāo)題","內(nèi)容")
 */
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; // 藍(lán)色標(biāo)題
  private JLabel close = null; // 關(guān)閉按鈕
  private JTextArea feature = null; // 內(nèi)容
  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("歡迎使用本系統(tǒng)");
    head = new JLabel(titleT);
    close = new JLabel(" x");
    feature = new JTextArea(word);
    jfeaPan = new JScrollPane(feature);
    sure = new JButton("確認(rèn)");
    sure.setHorizontalAlignment(SwingConstants.CENTER);
 
    // 設(shè)置提示框的邊框,寬度和顏色
    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("關(guān)閉");
 
    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));
    // 設(shè)置文本域自動(dòng)換行
    feature.setLineWrap(true);
 
    jfeaPan.setPreferredSize(new Dimension(260, 100));
    jfeaPan.setBorder(null);
    jfeaPan.setBackground(Color.black);
    tw.setBackground(Color.white);
 
    // 為了隱藏文本域,加個(gè)空的JLabel將他擠到下面去
    JLabel jsp = new JLabel();
    jsp.setPreferredSize(new Dimension(300, 15));
 
    sure.setPreferredSize(new Dimension(60, 30));
    // 設(shè)置標(biāo)簽鼠標(biāo)手形
    sure.setCursor(new Cursor(12));
    // 設(shè)置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() {
    // 為更新按鈕增加相應(yīng)的事件
    desktop = Desktop.getDesktop();
    sure.addMouseListener(new MouseAdapter() {
      public void mouseClicked(MouseEvent e) {
        //注釋代碼為點(diǎn)擊確認(rèn)之后跳轉(zhuǎn)到指定網(wǎng)頁(yè)
        // 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);
      }
    });
    // 右上角關(guān)閉按鈕事件
    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) {
      }
    }
    // 此處代碼用來(lái)實(shí)現(xiàn)讓消息提示框6秒后自動(dòng)消失
    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, "這是一個(gè)彈窗測(cè)試!");
 }
 
}

效果如下:


特此紀(jì)錄!

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Spring動(dòng)態(tài)多數(shù)據(jù)源配置實(shí)例Demo

    Spring動(dòng)態(tài)多數(shù)據(jù)源配置實(shí)例Demo

    本篇文章主要介紹了Spring動(dòng)態(tài)多數(shù)據(jù)源配置實(shí)例Demo,具有一定的參考價(jià)值,有興趣的可以了解一下。
    2017-01-01
  • Java畢業(yè)設(shè)計(jì)實(shí)戰(zhàn)之健身器材商城系統(tǒng)的實(shí)現(xiàn)

    Java畢業(yè)設(shè)計(jì)實(shí)戰(zhàn)之健身器材商城系統(tǒng)的實(shí)現(xiàn)

    只學(xué)書上的理論是遠(yuǎn)遠(yuǎn)不夠的,只有在實(shí)戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+Jdbc+Servlet+Ajax+Fileupload+mysql實(shí)現(xiàn)健身器材商城系統(tǒng),大家可以在過(guò)程中查缺補(bǔ)漏,提升水平
    2022-03-03
  • Java集合框架之Map詳解

    Java集合框架之Map詳解

    這篇文章主要為大家詳細(xì)介紹了Java集合框架之Map,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助
    2022-03-03
  • Java動(dòng)態(tài)編譯執(zhí)行代碼示例

    Java動(dòng)態(tài)編譯執(zhí)行代碼示例

    這篇文章主要介紹了Java動(dòng)態(tài)編譯執(zhí)行代碼示例,具有一定借鑒價(jià)值,需要的朋友可以參考下。
    2017-12-12
  • Java多線程中Callable和Future的解讀

    Java多線程中Callable和Future的解讀

    這篇文章主要介紹了Java多線程中Callable和Future的解讀,Callable接口類似于Runnable,從名字就可以看出來(lái)了,但是Runnable不會(huì)返回結(jié)果,并且無(wú)法拋出返回結(jié)果的異常,而Callable功能更強(qiáng)大一些,被線程執(zhí)行后,可以返回值,這個(gè)返回值可以被Future拿到,需要的朋友可以參考下
    2023-09-09
  • 解決線程池中ThreadGroup的坑

    解決線程池中ThreadGroup的坑

    這篇文章主要介紹了解決線程池中ThreadGroup的坑,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • Java9新特性Java.util.Optional優(yōu)化與增強(qiáng)解析

    Java9新特性Java.util.Optional優(yōu)化與增強(qiáng)解析

    這篇文章主要為大家介紹了Java9新特性Java.util.Optional優(yōu)化與增強(qiáng)使用說(shuō)明解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步
    2022-03-03
  • springboot環(huán)境下配置使用sqlite數(shù)據(jù)庫(kù)方式

    springboot環(huán)境下配置使用sqlite數(shù)據(jù)庫(kù)方式

    這篇文章主要介紹了springboot環(huán)境下配置使用sqlite數(shù)據(jù)庫(kù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-05-05
  • Spring Aop注解實(shí)現(xiàn)

    Spring Aop注解實(shí)現(xiàn)

    本文我們通過(guò)Spring AOP和Java的自定義注解來(lái)實(shí)現(xiàn)日志的插入功能,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友一起看看吧,希望對(duì)你有所幫助
    2021-07-07
  • 關(guān)于web項(xiàng)目讀取classpath下面文件的心得分享

    關(guān)于web項(xiàng)目讀取classpath下面文件的心得分享

    這篇文章主要介紹了關(guān)于web項(xiàng)目讀取classpath下面文件的心得,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-07-07

最新評(píng)論