我怎样才能创建一个样式和自定义字体的Java / Swing文本组件? [英] How can I create a Java/Swing text component that is both styled and has a custom font?

查看:233
本文介绍了我怎样才能创建一个样式和自定义字体的Java / Swing文本组件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何创建一个既有样式又有自定义字体的Java / Swing文本组件?我想用红色突出显示文本的一小部分,但同时使用自定义(通过嵌入 Font.createFont )字体。 JLabel接受HTML文本,允许我突出显示部分文本,但在渲染HTML时忽略字体设置。其他文本组件(如JTextArea)将使用自定义字体,但不会呈现HTML。最简单的方法是什么?



以下是使用JTextPane失败的示例:

  JTextPane textPane = new JTextPane(); 
textPane.setFont(myCustomFont);
textPane.setText(text);
MutableAttributeSet attributes = new SimpleAttributeSet();
StyleConstants.setForeground(attributes,Color.RED);
textPane.getStyledDocument()。setCharacterAttributes(
text.indexOf(toHighlight),
toHighlight.length(),
attributes,true
);

成功显示红色高亮显示的toHighlight部分,但不使用myCustomFont。请注意,我可以使用 StyleConstants.setFontFamily()设置一个字体,但不能使用自定义字体。

解决方案

好吧,我现在看到的问题更好了。

在检查了一些Swing源代码之后,显然你不能使用 DefaultStyledDocument ,并使用一个物理字体(你自己用 createFont 创建的)。



然而,我认为你可以做的是用这种方式实现你自己的 StyleContext

  public class MyStyleContext extends javax.swing.text.StyleContext 
{
@Override public Font getFont(AttributeSet attr)
{
Font font = attr.getAttribute(MyFont);
if(font!= null)
返回字体;
else
return super.getFont(attr);


$ / code $ / pre

然后你必须:



  1. a new MyStyleContext()创建一个 DefaultStyledDocument
  2. 将其附加到 JTextPane

  3. 调用 attributes.addAttribute(MyFont,myCustomFont); 在上面的代码片段中

不要尝试,但我认为它应该工作,或者这可能是一个很好的调查路径。


How can I create a Java/Swing text component that is both styled and has a custom font? I want to highlight a small portion of the text in red, but at the same time use a custom (embedded via Font.createFont) font. JLabel accepts HTML text, which allows me to highlight a portion of the text, but it ignores font settings when rendering HTML. Other text components such as JTextArea will use the custom font, but they won't render HTML. What's the easiest way to do both?

Here's an example of using JTextPane unsuccessfully:

    JTextPane textPane = new JTextPane();
    textPane.setFont(myCustomFont);
    textPane.setText(text);
    MutableAttributeSet attributes = new SimpleAttributeSet();
    StyleConstants.setForeground(attributes, Color.RED);
    textPane.getStyledDocument().setCharacterAttributes(
        text.indexOf(toHighlight),
        toHighlight.length(),
        attributes, true
    );

This successfully displays the text with the "toHighlight" portion highlighted in red, but it doesn't use myCustomFont. Note that I could set a String font with StyleConstants.setFontFamily(), but not a custom font.

解决方案

OK, I see the problem better now.

After checking some Swing source code, it is clear you cannot use the DefaultStyledDocument and have it use a physical font (one you created yourself with createFont) out of the box.

However, what I think you could do is implement your own StyleContext this way:

public class MyStyleContext extends javax.swing.text.StyleContext
{
    @Override public Font getFont(AttributeSet attr)
    {
        Font font = attr.getAttribute("MyFont");
        if (font != null)
            return font;
        else
            return super.getFont(attr);
    }
}

Then you have to:

  1. create a DefaultStyledDocument with a new MyStyleContext()
  2. "attach" it to the JTextPane
  3. call attributes.addAttribute("MyFont", myCustomFont); in your snippet above

I did not try it but I think it should work or it might be a good path to investigate.

这篇关于我怎样才能创建一个样式和自定义字体的Java / Swing文本组件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆