请教:如何用Swing实现右下角缓缓上升的提示框? 忘各位不吝赐教;谢谢. 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 用一个线程慢慢改变window的location 使用JInternalFrame作为提示框,使用线程设置Location 请问有类似的代码吗?因为对swing没有考究,比较陌生.谢谢. 请参考《swing hacks》一书中Hack 46. Slide Notes Out from the Taskbar 网上有例子,用着还行。以下代码为本人转载,所有权利属原作者所有。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 java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;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; ToolTipSingle single = new ToolTipSingle(); private boolean isDisplay = false; //是否显示 SendOffFrame sOF; /** * 构造函数,初始化默认气泡提示设置 * */ public ToolTip() { // 设定字体 _font = new Font("宋体", 0, 24); // 设定边框颜色 _bgColor = new Color(255, 255, 225); _border = Color.BLACK; _messageColor = Color.BLACK; _useTop = true; // 通过调用方法,强制获知是否支持自动窗体置顶 try { JWindow.class.getMethod("setAlwaysOnTop", new Class[] { Boolean.class }); } catch (Exception e) { _useTop = false; } } public void setFrame(SendOffFrame sOF){ this.sOF=sOF; } /** * 重构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(new Font("宋体", 0, 24));//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.addMouseListener(new MouseAdapter(){ @Override public void mouseReleased(MouseEvent e){ if(e.getClickCount() == 1){ //animateVertically(posx, stopYPosition, startYPosition); sOF.onShow(); sOF.setVisible(true); _displayTime=2; setVisible(false); dispose(); isDisplay=false; _countOfToolTip--; } } }); _message.setForeground(getMessageColor()); innerPanel.add(_iconLabel, BorderLayout.WEST); innerPanel.add(_message, BorderLayout.CENTER); getContentPane().add(externalPanel); } /** * 动画开始 * */ public void animate() { new Animation(this).start(); } } /** * 此类处则动画处理 * */ 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*100); _countOfToolTip--; _single.setVisible(false); _single.dispose(); */ } catch (Exception e) { throw new RuntimeException(e); } } } /** * 设定显示的图片及信息 * * @param icon * @param msg */ public void setToolTip(Icon icon, String msg) { if (icon != null) { single._iconLabel.setIcon(icon); } single._message.setText(msg); if (!isDisplay){ single.animate(); isDisplay=true; } } /** * 设定显示的信息 * * @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; } /** * 设定差值 * * @param gap */ public void setGap(int gap) { this._gap = gap; } /** * 获得信息颜色 * * @return */ public Color getMessageColor() { return _messageColor; } /** * 设定信息颜色 * * @param messageColor */ public void setMessageColor(Color messageColor) { this._messageColor = messageColor; } /** * 获得循环步长 * * @return */ public int getStep() { return _step; } /** * 设定循环步长 * * @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");// tip.setToolTip(new ImageIcon("test.jpg"),"“程序员”就是特指越写程序身材越“圆”那群人 -- cping1982");// tip.setToolTip(new ImageIcon("test.jpg"),"“程序员”就是特指越写程序身材越“圆”那群人 -- cping1982"); }} SendOffFrame sOF; 请问下 这是什么类 求助:写两个线程,交替打印数字和字母。 final 关键字 正在期中考试急需帮助!!! Timer一问 把什么什么放在内存里。。。 对JAVA心向往之 怎样隐藏表格的列 根类Object的一个很奇怪的问题,哪位大虾给解答一下?谢谢 anonymouse inner class 到底有什么作用? mybatis 返回结果map中columnPrefix不生效 键盘响应事件问题 请各位大大帮忙看看这代码怎么改?
使用JInternalFrame作为提示框,使用线程设置Location
请问有类似的代码吗?
因为对swing没有考究,比较陌生.
谢谢.
以下代码为本人转载,所有权利属原作者所有。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 java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
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;
ToolTipSingle single = new ToolTipSingle(); private boolean isDisplay = false; //是否显示
SendOffFrame sOF; /**
* 构造函数,初始化默认气泡提示设置
*
*/
public ToolTip() {
// 设定字体
_font = new Font("宋体", 0, 24);
// 设定边框颜色
_bgColor = new Color(255, 255, 225);
_border = Color.BLACK;
_messageColor = Color.BLACK;
_useTop = true;
// 通过调用方法,强制获知是否支持自动窗体置顶
try {
JWindow.class.getMethod("setAlwaysOnTop",
new Class[] { Boolean.class });
} catch (Exception e) {
_useTop = false;
} }
public void setFrame(SendOffFrame sOF){
this.sOF=sOF;
} /**
* 重构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(new Font("宋体", 0, 24));//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.addMouseListener(new MouseAdapter(){
@Override
public void mouseReleased(MouseEvent e){
if(e.getClickCount() == 1){
//animateVertically(posx, stopYPosition, startYPosition);
sOF.onShow();
sOF.setVisible(true);
_displayTime=2;
setVisible(false);
dispose();
isDisplay=false;
_countOfToolTip--;
}
}
});
_message.setForeground(getMessageColor());
innerPanel.add(_iconLabel, BorderLayout.WEST);
innerPanel.add(_message, BorderLayout.CENTER);
getContentPane().add(externalPanel); } /**
* 动画开始
*
*/
public void animate() {
new Animation(this).start();
} } /**
* 此类处则动画处理
*
*/
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*100); _countOfToolTip--;
_single.setVisible(false);
_single.dispose();
*/
} catch (Exception e) {
throw new RuntimeException(e);
}
}
} /**
* 设定显示的图片及信息
*
* @param icon
* @param msg
*/
public void setToolTip(Icon icon, String msg) {
if (icon != null) {
single._iconLabel.setIcon(icon);
}
single._message.setText(msg);
if (!isDisplay){
single.animate();
isDisplay=true;
}
} /**
* 设定显示的信息
*
* @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;
} /**
* 设定差值
*
* @param gap
*/
public void setGap(int gap) {
this._gap = gap;
} /**
* 获得信息颜色
*
* @return
*/
public Color getMessageColor() {
return _messageColor;
} /**
* 设定信息颜色
*
* @param messageColor
*/
public void setMessageColor(Color messageColor) {
this._messageColor = messageColor;
} /**
* 获得循环步长
*
* @return
*/
public int getStep() {
return _step;
} /**
* 设定循环步长
*
* @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");
// tip.setToolTip(new ImageIcon("test.jpg"),"“程序员”就是特指越写程序身材越“圆”那群人 -- cping1982");
// tip.setToolTip(new ImageIcon("test.jpg"),"“程序员”就是特指越写程序身材越“圆”那群人 -- cping1982"); }}