简体   繁体   English

当文本是一个字段时,如何在文本“文本”下划线?

[英]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.文本 object 将在另一个 class 中创建。在该 class 中,我需要使用 showUnderline 方法为其添加下划线。 The method seems incomplete.该方法似乎不完整。
I'm shooting for the java exclusive approach, meaning no HTML.我正在为 java 独家方法拍摄,意思是没有 HTML。
How do I link the text to the showUnderline method?如何将文本链接到 showUnderline 方法?

What do you mean 'java exclusive approach, meaning no HTML'?你是什么意思'java 独占方法,意味着没有 HTML'? You probably are looking for a JLabel, and you can put very simple html in it.您可能正在寻找 JLabel,您可以将非常简单的 html 放入其中。 Here's the first result on google:这是谷歌的第一个结果:

http://www.apl.jhu.edu/~hall/java/Swing-Tutorial/Swing-Tutorial-JLabel.html 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.它有一个使文本不同的示例 colors、fonts 以及粗体或斜体。 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.从那里,您可以像放置任何其他 JComponent 一样放置它。

If you really don't want HTML, you could use a JTextPane.如果你真的不想要 HTML,你可以使用 JTextPane。 Here's an example:这是一个例子:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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