【问题标题】:Java - How to change font/add border to only part of a JLabelJava - 如何将字体/添加边框仅更改为 JLabel 的一部分
【发布时间】:2017-03-17 01:35:47
【问题描述】:

在这种情况下,die1 是一个 Integer,它是从别处的计算中得出的。我希望 die1 的值通过边框和/或更大的文本/不同的颜色在视觉上进行区分。有没有办法做到这一点,不涉及拥有 2 个单独的JLabels?谢谢你。

firstJLabel.setText("Die 1: " + die1);

【问题讨论】:

    标签: java html swing jframe jlabel


    【解决方案1】:

    有没有办法做到这一点而不涉及拥有 2 个单独的 JLabel?

    您可以在标签中使用 HTML:

    firstJLabel.setText("<html><font color=\"red\">Die 1: </font>" + die1 + "</html>");
    

    或者您可以使用JTextPane 并使其看起来像一个标签。它支持属性:

    JTextPane textPane = new JTextPane();
    textPane.setBorder( null );
    textPane.setOpaque( false );
    
    SimpleAttributeSet green = new SimpleAttributeSet();
    StyleConstants.setForeground(green, Color.GREEN);
    
    //  Add some text
    
    try
    {
        StyledDocument doc = textPane.getStyledDocument();
        doc.insertString(0, die1, null);
        doc.insertString(0, "Die 1: ", green);
    }
    catch(Exception) {}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-12-13
      • 1970-01-01
      • 2017-06-26
      • 2014-10-18
      • 1970-01-01
      • 1970-01-01
      • 2012-06-21
      相关资源
      最近更新 更多