swing中Label添加下划线大致有下面三种实现方法:
- [一]、利用<HTML>标签实现
- [二]、利用Graphics 重写paint方法画出下划线
- [三]、完美实现下划线,支持Border设置和自定义Color
下面具体分析每种实现方式的优缺点:
[一]、利用<HTML>标签实现
优点:
- 原生态的Label,无需任何改造
- 支持自定义的Border的标签
缺点:
- 下划线的颜色和标签的颜色一致,无法单独自定义
代码示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
package michael.swing.label; import java.awt.Color; import java.awt.Dimension; import java.awt.FlowLayout; import javax.swing.BorderFactory; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.border.LineBorder; /** * @blog http://www.micmiu.com * @author Michael */ public class JLabelUnderLineHtml { /** * @param args */ public static void main(String[] args) { JFrame f = new JFrame("JLabe with Under Line"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setSize(new Dimension(300, 200)); f.setLayout(new FlowLayout()); JLabel label1 = new JLabel("<html><u>Label下划线</u><html>"); f.add(label1); JLabel label2 = new JLabel("<html><u>Label下划线</u><html>"); label2.setForeground(Color.BLUE); f.add(label2); // 有Border的标签 JLabel label3 = new JLabel("<html><u>Label(Border)下划线</u><html>"); label3.setBorder(BorderFactory.createCompoundBorder(new LineBorder( Color.RED), BorderFactory.createEmptyBorder(8, 8, 8, 8))); f.add(label3); // f.pack(); f.setVisible(true); } } |
效果图:
[二]、 利用Graphics 重写paint方法画出下划线
优点:
- 下划线的颜色可以单独自定义
缺点:
- 对于自定义的Border的标签支持不好
代码示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
package michael.swing.label; import java.awt.Color; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Graphics; import java.awt.Rectangle; import javax.swing.BorderFactory; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.border.LineBorder; /** * @blog http://www.micmiu.com * @author Michael */ public class JLabelUnderLineSimple extends JLabel { private Color underLineColor; public JLabelUnderLineSimple() { this(""); } public JLabelUnderLineSimple(String text) { super(text); } /** * @return the underLineColor */ public Color getUnderLineColor() { return underLineColor; } /** * @param pUnderLineColor the underLineColor to set */ public void setUnderLineColor(Color pUnderLineColor) { underLineColor = pUnderLineColor; } public void paint(Graphics g) { Rectangle r; super.paint(g); r = g.getClipBounds(); if (null != underLineColor) { g.setColor(underLineColor); } // 画出下划线 g.drawLine(0, r.height - getFontMetrics(getFont()).getDescent(), getFontMetrics(getFont()).stringWidth(getText()), r.height - getFontMetrics(getFont()).getDescent()); } /** * @param args */ public static void main(String[] args) { JFrame f = new JFrame("JLabe with Under Line"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setSize(new Dimension(300, 200)); f.setLayout(new FlowLayout()); JLabelUnderLineSimple label1 = new JLabelUnderLineSimple("Label下划线"); f.add(label1); JLabelUnderLineSimple label2 = new JLabelUnderLineSimple("Label下划线"); label2.setUnderLineColor(Color.BLUE); f.add(label2); // 有Border的标签 JLabelUnderLineSimple label3 = new JLabelUnderLineSimple( "Label(Border)下划线"); label3.setBorder(BorderFactory.createCompoundBorder(new LineBorder( Color.RED), BorderFactory.createEmptyBorder(8, 8, 8, 8))); f.add(label3); // f.pack(); f.setVisible(true); } } |
效果图:
从上面的截图明显可以看出该版本不能很好支持自定义Border的标签。
[三]、完美实现下划线
优点:
- 下划线的颜色可以单独自定义
- 支持自定义的Border的标签
缺点:
- 暂时没有发现 吼吼
代码示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
package michael.swing.label; import java.awt.Color; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Graphics; import java.awt.Insets; import java.awt.Rectangle; import javax.swing.BorderFactory; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.border.LineBorder; /** * @blog http://www.micmiu.com * @author Michael */ public class JLabelUnderLinePerfect extends JLabel { private Color underLineColor; public JLabelUnderLinePerfect() { this(""); } public JLabelUnderLinePerfect(String text) { super(text); } /** * @return the underLineColor */ public Color getUnderLineColor() { return underLineColor; } /** * @param pUnderLineColor the underLineColor to set */ public void setUnderLineColor(Color pUnderLineColor) { underLineColor = pUnderLineColor; } public void paint(Graphics g) { super.paint(g); Rectangle r = g.getClipBounds(); int xoffset = 0, yoffset = 0, pointX = 0, pointY = 0, point2X = 0, point2Y = 0; // 根据border的设置 计算出下划线的起止Point if (null != this.getBorder() && null != this.getBorder().getBorderInsets(this)) { Insets inserts = this.getBorder().getBorderInsets(this); // System.out.println(inserts); xoffset = inserts.left; yoffset = inserts.bottom; } pointX = xoffset; pointY = point2Y = r.height - yoffset - getFontMetrics(getFont()).getDescent(); point2X = pointX + getFontMetrics(getFont()).stringWidth(getText()); if (null != underLineColor) { g.setColor(underLineColor); } g.drawLine(pointX, pointY, point2X, point2Y); } /** * @param args */ public static void main(String[] args) { JFrame f = new JFrame("JLabe with Under Line"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setSize(new Dimension(300, 200)); f.setLayout(new FlowLayout()); JLabelUnderLinePerfect label1 = new JLabelUnderLinePerfect("Label下划线"); f.add(label1); JLabelUnderLinePerfect label2 = new JLabelUnderLinePerfect("Label下划线"); label2.setUnderLineColor(Color.BLUE); f.add(label2); JLabelUnderLinePerfect label3 = new JLabelUnderLinePerfect( "Label(Border)下划线"); label3.setBorder(BorderFactory.createCompoundBorder(new LineBorder( Color.RED), BorderFactory.createEmptyBorder(8, 18, 28, 38))); label3.setUnderLineColor(Color.BLUE); f.add(label3); // f.pack(); f.setVisible(true); } } |
效果图:
原创文章,转载请注明: 转载自micmiu – 软件开发+生活点滴[ http://www.micmiu.com/ ]
本文链接地址: http://www.micmiu.com/lang/java/swing-jlabel-under-line/
0 条评论。