亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

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

系統(tǒng) 2160 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)論
主站蜘蛛池模板: 国产精品99久久免费观看 | 国产亚洲精品久久yy5099 | 国产成人精品男人的天堂网站 | 夜夜操天天摸 | 在线综合+亚洲+欧美中文字幕 | 草草影院一级毛片a级 | 性猛交╳xxx乱大交 性猛交毛片 | 日本免费不卡在线一区二区三区 | 91成人在线 | 韩日精品在线 | 欧美另类亚洲 | 久久日本精品国产精品白 | 99视频免费播放 | 久久免费手机视频 | 欧亚在线视频 | 久久99国产一区二区三区 | 色久天堂网 | 久久久成人啪啪免费网站 | 久久91精品久久久久久水蜜桃 | 卡通动漫精选国产欧美 | 国产在线精品成人一区二区三区 | 亚洲成人看片 | 免费视频一区二区性色 | 国产欧美日韩在线播放 | 日日操夜夜爽 | 91久久精品一区二区三区 | 天天草夜夜骑 | 伊人久久久久久久久香港 | 香港aa三级久久三级老师 | 亚洲国产精品久久婷婷 | 特级黄aaaaaaaaa毛片 | 五月天婷婷免费视频观看 | 久久久久久久网站 | 久久国产精品免费视频 | 国产精品爱久久久久久久 | 国产亚洲午夜精品 | 欧美日韩精品一区二区在线线 | 国产一区二区在线视频观看 | 久久精品影院永久网址 | 性欧美一级毛片在线播放 | 色综合99|