tooltip 可以对一些操作的进一步说明,h5中常用的title属性属于最简单的tooltip,但是在更多的框架中为了美观,会有tooltip插件, 可以实现更漂亮的tooltip . swing可以扩展JToolTip类实现更美观的提示框.
swing实现tooltip
- 继承JTooltip类
- 重写updateUI方法
- 重写paintComponent 自定义形状和背景色
- 重写setTipText 自定义前景色
总结
- ToolTipManager 可以对tooltip显示的速度设置
- new JButton时 可以直接覆写默认方法
测试代码
import com.formdev.flatlaf.FlatLightLaf;
import com.mynote.core.ui.ColorBuilder;
import com.mynote.core.util.FrameUtil;
import com.mynote.example.demo.AbstractDefaultPanel;
import net.miginfocom.swing.MigLayout;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.plaf.basic.BasicButtonUI;
import java.awt.*;
import java.awt.event.HierarchyEvent;
import java.awt.event.HierarchyListener;
import java.awt.geom.Area;
import java.awt.geom.Path2D;
import java.awt.geom.RoundRectangle2D;
import java.util.Optional;
public class ToolTipTest extends AbstractDefaultPanel {
private JButton defaultToolTip;
private JButton defineToolTip;
@Override
protected void init() {
ToolTipManager toolTipManager = ToolTipManager.sharedInstance();
toolTipManager.setInitialDelay(100);
defaultToolTip = new JButton("defaultToolTip");
defaultToolTip.setToolTipText("默认提示");
defineToolTip = new JButton("defineToolTip") {
@Override
public JToolTip createToolTip() {
return new BalloonToolTip(ColorBuilder.BG_BLANK_COLOR1, Color.WHITE);
}
};
defineToolTip.setToolTipText("自定义提示样式");
}
@Override
protected void render() {
view.setLayout(new MigLayout("wrap 1,center"));
super.createViewRow(new JLabel("default tooltip 消息:"), defaultToolTip);
super.createViewRow(new JLabel("define tooltip 消息:"), defineToolTip);
super.add(view);
}
@Override
protected void bindEvents() {
}
public static void main(String[] args) {
FlatLightLaf.install();
FrameUtil.launchTest(new ToolTipTest());
}
private class BalloonToolTip extends JToolTip {
private static final long serialVersionUID = 1L;
private transient HierarchyListener listener;
private Color bgColor;
private Color fgColor;
public BalloonToolTip(Color bgColor, Color fgColor) {
this.bgColor = bgColor;
this.fgColor = fgColor;
}
@Override
public void updateUI() {
removeHierarchyListener(listener);
super.updateUI();
listener = e -> {
Component c = e.getComponent();
if ((e.getChangeFlags() & HierarchyEvent.SHOWING_CHANGED) != 0 && c.isShowing()) {
Optional.ofNullable(SwingUtilities.getRoot(c)).filter(JWindow.class::isInstance)
.map(JWindow.class::cast).ifPresent(w -> w.setBackground(new Color(0x0, true)));
}
};
addHierarchyListener(listener);
setOpaque(false);
setBorder(BorderFactory.createEmptyBorder(8, 5, 0, 5));
}
@Override
public Dimension getPreferredSize() {
Dimension d = super.getPreferredSize();
d.height = 35;
return d;
}
@Override
protected void paintComponent(Graphics g) {
Shape s = makeBalloonShape();
Graphics2D g2 = (Graphics2D) g.create();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setColor(bgColor);
g2.fill(s);
g2.setColor(bgColor);
g2.draw(s);
g2.dispose();
super.paintComponent(g);
}
private Shape makeBalloonShape() {
Insets i = getInsets();
float w = getWidth() - 1f;
float h = getHeight() - 1f;
float v = i.top * .5f;
Path2D triangle = new Path2D.Float();
triangle.moveTo(i.left + v + v, 0f);
triangle.lineTo(i.left + v, v);
triangle.lineTo(i.left + v + v + v, v);
Area area = new Area(new RoundRectangle2D.Float(0f, v, w, h - i.bottom - v, i.top, i.top));
area.add(new Area(triangle));
return area;
}
@Override
public void setTipText(String tipText) {
super.setForeground(fgColor);
super.setTipText(tipText);
}
}
}