這篇文章給大家分享的是有關(guān)java如何實(shí)現(xiàn)桌面右下角彈窗效果的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來看看吧。
創(chuàng)新互聯(lián)從2013年成立,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目成都網(wǎng)站建設(shè)、做網(wǎng)站網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢想脫穎而出為使命,1280元巴馬做網(wǎng)站,已為上家服務(wù),為巴馬各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:028-86922220
最近需要一個(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í)例化出對象,調(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)頁
// 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) {
}
}
// 此處代碼用來實(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è)彈窗測試!");
}
}效果如下:

感謝各位的閱讀!關(guān)于“java如何實(shí)現(xiàn)桌面右下角彈窗效果”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
標(biāo)題名稱:java如何實(shí)現(xiàn)桌面右下角彈窗效果
網(wǎng)頁路徑:http://www.chinadenli.net/article20/gissjo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供小程序開發(fā)、網(wǎng)站設(shè)計(jì)公司、移動(dòng)網(wǎng)站建設(shè)、營銷型網(wǎng)站建設(shè)、搜索引擎優(yōu)化、網(wǎng)站收錄
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)