黄色网页视频 I 影音先锋日日狠狠久久 I 秋霞午夜毛片 I 秋霞一二三区 I 国产成人片无码视频 I 国产 精品 自在自线 I av免费观看网站 I 日本精品久久久久中文字幕5 I 91看视频 I 看全色黄大色黄女片18 I 精品不卡一区 I 亚洲最新精品 I 欧美 激情 在线 I 人妻少妇精品久久 I 国产99视频精品免费专区 I 欧美影院 I 欧美精品在欧美一区二区少妇 I av大片网站 I 国产精品黄色片 I 888久久 I 狠狠干最新 I 看看黄色一级片 I 黄色精品久久 I 三级av在线 I 69色综合 I 国产日韩欧美91 I 亚洲精品偷拍 I 激情小说亚洲图片 I 久久国产视频精品 I 国产综合精品一区二区三区 I 色婷婷国产 I 最新成人av在线 I 国产私拍精品 I 日韩成人影音 I 日日夜夜天天综合

Java氣泡提示功能實(shí)現(xiàn)

系統(tǒng) 2565 0
一個(gè)用Swing實(shí)現(xiàn)的java氣泡提示效果。


運(yùn)行效果如下圖:

Java氣泡提示功能實(shí)現(xiàn)

package org.loon.swing.display;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.Insets;
import java.awt.Rectangle;
import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JWindow;
import javax.swing.border.EtchedBorder;
public class ToolTip {

// 氣泡提示寬
private int _width = 300;

// 氣泡提示高
private int _height = 100;

// 設(shè)定循環(huán)的步長(zhǎng)
private int _step = 30;

// 每步時(shí)間
private int _stepTime = 30;

// 顯示時(shí)間
private int _displayTime = 6000;

// 目前申請(qǐng)的氣泡提示數(shù)量
private int _countOfToolTip = 0;

// 當(dāng)前最大氣泡數(shù)
private int _maxToolTip = 0;

// 在屏幕上顯示的最大氣泡提示數(shù)量
private int _maxToolTipSceen;

// 字體
private Font _font;

// 邊框顏色
private Color _bgColor;

// 背景顏色
private Color _border;

// 消息顏色
private Color _messageColor;

// 差值設(shè)定
int _gap;

// 是否要求至頂(jre1.5以上版本方可執(zhí)行)
boolean _useTop = true;

/**
* 構(gòu)造函數(shù),初始化默認(rèn)氣泡提示設(shè)置
*
*/
public ToolTip() {
// 設(shè)定字體
_font = new Font("宋體", 0, 12);
// 設(shè)定邊框顏色
_bgColor = new Color(255, 255, 225);
_border = Color.BLACK;
_messageColor = Color.BLACK;
_useTop = true;
// 通過調(diào)用方法,強(qiáng)制獲知是否支持自動(dòng)窗體置頂
try {
JWindow.class.getMethod("setAlwaysOnTop",
new Class[] { Boolean.class });
} catch (Exception e) {
_useTop = false;
}

}

/**
* 重構(gòu)JWindow用于顯示單一氣泡提示框
*
*/
class ToolTipSingle extends JWindow {
private static final long serialVersionUID = 1L;

private JLabel _iconLabel = new JLabel();

private JTextArea _message = new JTextArea();

public ToolTipSingle() {
initComponents();
}

private void initComponents() {
setSize(_width, _height);
_message.setFont(getMessageFont());
JPanel externalPanel = new JPanel(new BorderLayout(1, 1));
externalPanel.setBackground(_bgColor);
// 通過設(shè)定水平與垂直差值獲得內(nèi)部面板
JPanel innerPanel = new JPanel(new BorderLayout(getGap(), getGap()));
innerPanel.setBackground(_bgColor);
_message.setBackground(_bgColor);
_message.setMargin(new Insets(4, 4, 4, 4));
_message.setLineWrap(true);
_message.setWrapStyleWord(true);
// 創(chuàng)建具有指定高亮和陰影顏色的陰刻浮雕化邊框
EtchedBorder etchedBorder = (EtchedBorder) BorderFactory
.createEtchedBorder();
// 設(shè)定外部面板內(nèi)容邊框?yàn)轱L(fēng)化效果
externalPanel.setBorder(etchedBorder);
// 加載內(nèi)部面板
externalPanel.add(innerPanel);
_message.setForeground(getMessageColor());
innerPanel.add(_iconLabel, BorderLayout.WEST);
innerPanel.add(_message, BorderLayout.CENTER);
getContentPane().add(externalPanel);
}

/**
* 動(dòng)畫開始
*
*/
public void animate() {
new Animation(this).start();
}

}

/**
* 此類處則動(dòng)畫處理
*
*/
class Animation extends Thread {

ToolTipSingle _single;

public Animation(ToolTipSingle single) {
this._single = single;
}

/**
* 調(diào)用動(dòng)畫效果,移動(dòng)窗體坐標(biāo)
*
* @param posx
* @param startY
* @param endY
* @throws InterruptedException
*/
private void animateVertically(int posx, int startY, int endY)
throws InterruptedException {
_single.setLocation(posx, startY);
if (endY < startY) {
for (int i = startY; i > endY; i -= _step) {
_single.setLocation(posx, i);
Thread.sleep(_stepTime);
}
} else {
for (int i = startY; i < endY; i += _step) {
_single.setLocation(posx, i);
Thread.sleep(_stepTime);
}
}
_single.setLocation(posx, endY);
}

/**
* 開始動(dòng)畫處理
*/
public void run() {
try {
boolean animate = true;
GraphicsEnvironment ge = GraphicsEnvironment
.getLocalGraphicsEnvironment();
Rectangle screenRect = ge.getMaximumWindowBounds();
int screenHeight = (int) screenRect.height;
int startYPosition;
int stopYPosition;
if (screenRect.y > 0) {
animate = false;
}
_maxToolTipSceen = screenHeight / _height;
int posx = (int) screenRect.width - _width - 1;
_single.setLocation(posx, screenHeight);
_single.setVisible(true);
if (_useTop) {
_single.setAlwaysOnTop(true);
}
if (animate) {
startYPosition = screenHeight;
stopYPosition = startYPosition - _height - 1;
if (_countOfToolTip > 0) {
stopYPosition = stopYPosition
- (_maxToolTip % _maxToolTipSceen * _height);
} else {
_maxToolTip = 0;
}
} else {
startYPosition = screenRect.y - _height;
stopYPosition = screenRect.y;

if (_countOfToolTip > 0) {
stopYPosition = stopYPosition
+ (_maxToolTip % _maxToolTipSceen * _height);
} else {
_maxToolTip = 0;
}
}

_countOfToolTip++;
_maxToolTip++;

animateVertically(posx, startYPosition, stopYPosition);
Thread.sleep(_displayTime);
animateVertically(posx, stopYPosition, startYPosition);

_countOfToolTip--;
_single.setVisible(false);
_single.dispose();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}

/**
* 設(shè)定顯示的圖片及信息
*
* @param icon
* @param msg
*/
public void setToolTip(Icon icon, String msg) {
ToolTipSingle single = new ToolTipSingle();
if (icon != null) {
single._iconLabel.setIcon(icon);
}
single._message.setText(msg);
single.animate();
}

/**
* 設(shè)定顯示的信息
*
* @param msg
*/
public void setToolTip(String msg) {
setToolTip(null, msg);
}

/**
* 獲得當(dāng)前消息字體
*
* @return
*/
public Font getMessageFont() {
return _font;
}

/**
* 設(shè)置當(dāng)前消息字體
*
* @param font
*/
public void setMessageFont(Font font) {
_font = font;
}

/**
* 獲得邊框顏色
*
* @return
*/
public Color getBorderColor() {
return _border;
}

/**
* 設(shè)置邊框顏色
*
* @param _bgColor
*/
public void setBorderColor(Color borderColor) {
this._border = borderColor;
}

/**
* 獲得顯示時(shí)間
*
* @return
*/
public int getDisplayTime() {
return _displayTime;
}

/**
* 設(shè)置顯示時(shí)間
*
* @param displayTime
*/
public void setDisplayTime(int displayTime) {
this._displayTime = displayTime;
}

/**
* 獲得差值
*
* @return
*/
public int getGap() {
return _gap;
}

/**
* 設(shè)定差值
*
* @param gap
*/
public void setGap(int gap) {
this._gap = gap;
}

/**
* 獲得信息顏色
*
* @return
*/
public Color getMessageColor() {
return _messageColor;
}

/**
* 設(shè)定信息顏色
*
* @param messageColor
*/
public void setMessageColor(Color messageColor) {
this._messageColor = messageColor;
}

/**
* 獲得循環(huán)步長(zhǎng)
*
* @return
*/
public int getStep() {
return _step;
}

/**
* 設(shè)定循環(huán)步長(zhǎng)
*
* @param _step
*/
public void setStep(int _step) {
this._step = _step;
}

public int getStepTime() {
return _stepTime;
}

public void setStepTime(int _stepTime) {
this._stepTime = _stepTime;
}

public Color getBackgroundColor() {
return _bgColor;
}

public void setBackgroundColor(Color bgColor) {
this._bgColor = bgColor;
}

public int getHeight() {
return _height;
}

public void setHeight(int height) {
this._height = height;
}

public int getWidth() {
return _width;
}

public void setWidth(int width) {
this._width = width;
}

public static void main(String[] args) {

ToolTip tip = new ToolTip();
tip.setToolTip(new ImageIcon("test.jpg"),"“程序員”就是特指越寫程序身材越“圓”那群人 -- cping1982");

}

}

Java氣泡提示功能實(shí)現(xiàn)


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦!!!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論