简体   繁体   中英

How do I underline text "text" when text is a field?

public class Text extends JPanel {

private String text;


    public Text()
    {
    this.setPreferredSize(new Dimension(20,20));
    setFont (new Font(text, Font.PLAIN, 24));
    text = "";
    }

    public void showUnderline()
    {

    Hashtable<TextAttribute, Object> map = new Hashtable
    <TextAttribute, Object>();

    map.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);   
    }

the text object will be created within another class. in that class I will need to underline it with the showUnderline method. The method seems incomplete.
I'm shooting for the java exclusive approach, meaning no HTML.
How do I link the text to the showUnderline method?

What do you mean 'java exclusive approach, meaning no HTML'? You probably are looking for a JLabel, and you can put very simple html in it. Here's the first result on google:

http://www.apl.jhu.edu/~hall/java/Swing-Tutorial/Swing-Tutorial-JLabel.html

It has an example of making text different colors, fonts, and bold or italicized. You could probably just do something like:

JLabel label = new JLabel("<u>MY TEXT</u>",JLabel.CENTER);

From there, you can place it like you would place any other JComponent.

If you really don't want HTML, you could use a JTextPane. Here's an example:

http://www.exampledepot.com/egs/javax.swing.text/style_hilitewords2.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM