⽤Swing实现的java⽓泡提⽰效果效果超炫,看附件截图
package monitor.ico;
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;
// 设定循环的步长
private int _step = 30;
// 每步时间
private int _stepTime = 30;
// 显⽰时间
private int _displayTime = 6000;
// ⽬前申请的⽓泡提⽰数量
private int _countOfToolTip = 0;
// 当前最⼤⽓泡数
private int _maxToolTip = 0;
// 在屏幕上显⽰的最⼤⽓泡提⽰数量
private int _maxToolTipSceen;
/
/ 字体
private Font _font;
// 边框颜⾊
private Color _bgColor;
// 背景颜⾊
private Color _border;
// 消息颜⾊
private Color _messageColor;
// 差值设定
int _gap;
// 是否要求⾄顶(jre1.5以上版本⽅可执⾏)
boolean _useTop = true;
* 构造函数,初始化默认⽓泡提⽰设置
*
*/
public ToolTip() {
// 设定字体
_font = new Font("宋体", 0, 12);
// 设定边框颜⾊
_bgColor = new Color(255, 255, 225);
_border = Color.BLACK;
_messageColor = Color.BLACK;
_useTop = true;
// 通过调⽤⽅法,强制获知是否⽀持⾃动窗体置顶
try {
Method("setAlwaysOnTop",
new Class[] { Boolean.class });
} catch (Exception e) {
_useTop = false;
}
}
/**
* 重构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);
// 通过设定⽔平与垂直差值获得内部⾯板
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);
// 创建具有指定⾼亮和阴影颜⾊的阴刻浮雕化边框
EtchedBorder etchedBorder = (EtchedBorder) BorderFactory
.createEtchedBorder();
// 设定外部⾯板内容边框为风化效果
externalPanel.setBorder(etchedBorder);
// 加载内部⾯板
externalPanel.add(innerPanel);
_message.setForeground(getMessageColor());
innerPanel.add(_iconLabel, BorderLayout.WEST);
innerPanel.add(_message, BorderLayout.CENTER);
getContentPane().add(externalPanel);
}
/**
* 动画开始
*
public void animate() {
new Animation(this).start();
}
}
/**
* 此类处则动画处理
*
*/
javaswing实现购买class Animation extends Thread {
ToolTipSingle _single;
public Animation(ToolTipSingle single) {
this._single = single;
}
/**
* 调⽤动画效果,移动窗体坐标
*
* @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);
}
/**
* 开始动画处理
*/
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);
}
}
}
/**
* 设定显⽰的图⽚及信息
*
* @param icon
* @param msg
*/
public void setToolTip(Icon icon, String msg) {
try {
// 多个⽂件变化时,每个间隔2秒
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
ToolTipSingle single = new ToolTipSingle();
if (icon != null) {
single._iconLabel.setIcon(icon);
}
single._message.setText(msg);
single.animate();
}
/**
* 设定显⽰的信息
*
* @param msg
*/
public void setToolTip(String msg) {
setToolTip(null, msg);
}
/**
* 获得当前消息字体
*
* @return
*/
public Font getMessageFont() {
return _font;
}
/**
* 设置当前消息字体
*
* @param font
*/
public void setMessageFont(Font font) {
_font = font;
}
/**
* 获得边框颜⾊
*
* @return
*/
public Color getBorderColor() {
return _border;
}
/**
* 设置边框颜⾊
*
* @param _bgColor
*/
public void setBorderColor(Color borderColor) { this._border = borderColor;
}
/**
* 获得显⽰时间
*
* @return
*/
public int getDisplayTime() {
return _displayTime;
}
/**
* 设置显⽰时间
*
* @param displayTime
*/
public void setDisplayTime(int displayTime) { this._displayTime = displayTime;
}
/**
* 获得差值
*
* @return
*/
public int getGap() {
return _gap;
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论