【问题标题】:Overline text inside JButtonJButton 内的文字上划线
【发布时间】:2015-08-07 01:20:48
【问题描述】:

我知道,如果我想在 JButton 中为文本添加下划线,我必须这样做:

JButton myButton=new JButton("Test");
Font font = myButton.getFont();
Map attributes = font.getAttributes();
attributes.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
myButton.setFont(font.deriveFont(attributes));

而且,如果我要覆盖文本?我该怎么办?谁能帮帮我?

【问题讨论】:

    标签: java swing text fonts jbutton


    【解决方案1】:

    这里不需要Font 类。您可以使用HTML directly 并添加<u><strike> 标签:

    myButton = new JButton("<html><strike><u>Test</u></strike></html>");
    

    【讨论】:

    • 认为 OP 想要在文本上划一条线,而不是穿过中心...:)
    • @Reimeus 我不擅长 HTML,是否有任何标签负责在文本 over 上添加一行? :)
    • 不要认为 HTML 会在这里工作,唯一的解决方案是自定义一个,例如 camickr 的答案......
    【解决方案2】:

    只是添加到@maroun 的答案(我不允许发表评论)。尝试直接使用 HTML。

    myButton = new JButton ("<html><font style=\"text-decoration: overline;\">  Overline text</font></html>");
    

    【讨论】:

    • "..尝试直接使用 HTML。" JLabel中尝试过HTML/样式吗?我没有时间,但怀疑它是否会按预期呈现(当然,假设首先修复了编译错误)。
    • 感谢您的回答。实际上,JButton构造函数中作为参数传递的String中使用了“”,导致编译错误。
    • &lt;html&gt;&lt;div style="border-top: 1px solid #000000"&gt;Overline text&lt;/div&gt;&lt;/html&gt; 可以。
    【解决方案3】:

    你可以创建一个CompoundBorder:

    JButton button = new JButton("Using Compound Border");
    Border lineBorder = BorderFactory.createMatteBorder(2, 0, 0, 0, Color.BLACK);
    CompoundBorder border = new CompoundBorder(button.getBorder(), lineBorder);
    button.setBorder( border );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-07-25
      • 2011-08-12
      • 2016-02-16
      • 1970-01-01
      • 1970-01-01
      • 2015-02-18
      • 2012-03-24
      相关资源
      最近更新 更多