效果:
代码:
package
azure.ui.swing.components.tooltip;
import
java.awt.AlphaComposite;
import
java.awt.Color;
import
java.awt.FlowLayout;
import
java.awt.Graphics;
import
java.awt.Graphics2D;
import
java.awt.Point;
import
java.awt.RenderingHints;
import
java.awt.event.MouseEvent;
import
java.awt.event.MouseListener;
import
java.awt.geom.RoundRectangle2D;
import
java.util.StringTokenizer;
import
javax.swing.JButton;
import
javax.swing.JFrame;
import
javax.swing.JLayeredPane;
import
javax.swing.JPanel;
import
javax.swing.SwingUtilities;
import
javax.swing.border.LineBorder;
/**
*
@author
Azure
*
@version
1.0 13/03/07
*/
public
class
AzHOpaqueToolTipExample
extends
JFrame {
public
AzHOpaqueToolTipExample() {
init();
}
public
void
init() {
HalfOpaqueToolTip tooltip
=
new
HalfOpaqueToolTip(
"
我们还能不能能不能再见面\n我在佛前苦苦求了几千年\n当我在踏过这条奈何桥之前\n让我再吻一吻你的脸
"
,
new
Color(
250
,
250
,
200
), Color.RED, Color.BLACK, OPAQUE,
this
);
JButton button
=
new
JButton(
"
swing
"
);
button.addMouseListener(tooltip);
this
.setLayout(
new
FlowLayout());
this
.add(
new
JButton(
"
金
"
));
this
.add(
new
JButton(
"
木
"
));
this
.add(
new
JButton(
"
水
"
));
this
.add(
new
JButton(
"
火
"
));
this
.add(
new
JButton(
"
土
"
));
this
.add(button);
this
.add(
new
JButton(
"
风
"
));
this
.add(
new
JButton(
"
雨
"
));
this
.add(
new
JButton(
"
雷
"
));
this
.add(
new
JButton(
"
电
"
));
this
.setSize(
360
,
200
);
this
.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this
.setLocationRelativeTo(
null
);
this
.setVisible(
true
);
}
public
static
void
main(String args[]) {
new
AzHOpaqueToolTipExample();
}
public
final
static
int
NOT_OPAQUE
=
0
;
//
不透明
public
final
static
int
HALF_OPAQUE
=
1
;
//
半透明
public
final
static
int
OPAQUE
=
2
;
//
透明(有边)
public
final
static
int
COMPLETE_OPAQUE
=
3
;
//
完全透明
class
HalfOpaqueToolTip
extends
JPanel
implements
MouseListener {
private
Color backGroundCol
=
null
;
private
Color foregroundColorCol
=
null
;
private
JFrame frame
=
null
;
private
String content
=
null
;
private
int
opaqueT
=
0
;
public
HalfOpaqueToolTip(String tooltipContent, Color backGroundColor,
Color foregroundColor, Color borderColor,
int
opaqueType,
JFrame frame) {
content
=
tooltipContent;
backGroundCol
=
backGroundColor;
foregroundColorCol
=
foregroundColor;
opaqueT
=
opaqueType;
/*
* 调整tooltip的大小
*/
int
fristRowIndex
=
tooltipContent.indexOf(
"
\n
"
);
String tooltipStr
=
""
;
if
(fristRowIndex
>
0
) {
tooltipStr
=
tooltipContent.substring(
0
, tooltipContent
.indexOf(
"
\n
"
));
}
else
{
tooltipStr
=
tooltipContent;
}
this
.setSize(tooltipStr.length()
*
this
.getFont().getSize()
+
20
,
new
StringTokenizer(tooltipContent,
"
\n
"
)
.countTokens()
*
20
+
15
);
//
左右各留10的空白空间
this
.setOpaque(
false
);
if
(opaqueType
!=
COMPLETE_OPAQUE) {
this
.setBorder(
new
LineBorder(borderColor));
}
this
.setVisible(
false
);
//
将本控件置于界面的最顶层
frame.getLayeredPane().add(
this
, JLayeredPane.POPUP_LAYER);
this
.frame
=
frame;
}
protected
void
paintComponent(Graphics g) {
super
.paintComponent(g);
Graphics2D g2d
=
(Graphics2D) g;
//
开始画平滑的图形
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setColor(backGroundCol);
if
(opaqueT
==
HALF_OPAQUE) {
//
将控件的透明度设置为60%
AlphaComposite composite
=
AlphaComposite.getInstance(
AlphaComposite.SRC_OVER,
60
/
100.0F
);
g2d.setComposite(composite);
g2d.fill(
new
RoundRectangle2D.Float(
0
,
0
,
this
.getWidth(),
this
.getHeight(),
0
,
0
));
}
else
if
(opaqueT
==
NOT_OPAQUE) {
//
将控件的透明度设置为不透明
AlphaComposite composite
=
AlphaComposite.getInstance(
AlphaComposite.SRC_OVER,
100
/
100.0F
);
g2d.setComposite(composite);
g2d.fill(
new
RoundRectangle2D.Float(
0
,
0
,
this
.getWidth(),
this
.getHeight(),
0
,
0
));
}
g2d.setColor(foregroundColorCol);
//
恢复到默认情况
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_OFF);
/*
* 画上字符内容
*/
StringTokenizer contentTokenizer
=
new
StringTokenizer(content,
"
\n
"
);
int
contentStartY
=
20
;
while
(contentTokenizer.hasMoreElements()) {
g2d.drawString(contentTokenizer.nextToken(),
10
, contentStartY);
contentStartY
=
contentStartY
+
20
;
}
}
public
void
mouseEntered(MouseEvent e) {
Point p
=
e.getLocationOnScreen();
SwingUtilities.convertPointFromScreen(p, e.getComponent()
.getParent());
if
(p.x
+
this
.getWidth()
>
frame.getX()) {
this
.setLocation(
new
Point(p.x
-
this
.getWidth(), p.y));
}
else
{
this
.setLocation(
new
Point(p.x, p.y));
}
this
.setVisible(
true
);
}
public
void
mouseExited(MouseEvent e) {
this
.setVisible(
false
);
}
public
void
mouseClicked(MouseEvent e) {
}
public
void
mousePressed(MouseEvent e) {
}
public
void
mouseReleased(MouseEvent e) {
}
}
}