Have a Label with underlined text Tag(s): AWT


[UnderlinedLabel.java]
import java.awt.*;

public class UnderlinedLabel extends Label  {
 public UnderlinedLabel(){
  this("");    
  }

 public UnderlinedLabel(String text){
  super(text);    
  }

 public void paint(Graphics g) {
  Rectangle r;
  super.paint(g);
  r = g.getClipBounds();
  g.drawLine
   (0,
    r.height - this.getFontMetrics(this.getFont()).getDescent(), 
    this.getFontMetrics(this.getFont()).stringWidth(this.getText()),  
    r.height - this.getFontMetrics(this.getFont()).getDescent());
  }
}
[TestUnderlinedLabel.java]
import java.applet.*;
import java.awt.*;

public class TestUnderlinedLabel extends Applet {
  public void init() {
    UnderlinedLabel ul1 = 
      new UnderlinedLabel
         ("Java How-to");
    add(ul1);
    }
}
[testapplet.html]
<HTML><HEAD></HEAD><BODY>
 <APPLET CODE="TestUnderlinedLabel.class"  
         NAME="myApplet" 
         HEIGHT=200 WIDTH=200>
</APPLET></BODY></HTML>
Check this How-to for underlined text in Swing.

blog comments powered by Disqus