Java AttributedString,粗体和上标 [英] Java AttributedString, both bold and superscript

查看:200
本文介绍了Java AttributedString,粗体和上标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试渲染一个 java.text.AttributedString ,它是粗体和上标。虽然它可以使某些范围为粗体或上标,但渲染似乎无法处理粗体和上标范围。

I am trying to render a java.text.AttributedString which is both bold and superscript. While it works to make some range either bold or superscripted, the rendering can't seem to handle a range that is both bold and superscript.

以下SSCCE显示渲染这使用带有HTML文本的JLabel工作正常。有没有办法在没有JLabel的情况下获得这种行为?

The following SSCCE shows that rendering this using a JLabel with HTML text works fine. Is there a way to get this behaviour without a JLabel?

顺便说一下,我查看了创建的AttributedString属性,它们看起来没问题,所以它最终是一个渲染问题。

Btw, I had a look into the created AttributedString properties and they look okay by me, so it is definitively a rendering problem.

package funky.chart;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.font.TextAttribute;
import java.text.AttributedString;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;

public class AttributedStringTest
{
    public static void main(String[] args) {
        // prevent using the default UI manager who renders in bold by default for the HTML label
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception ex) {
            System.err.println("Could not set look and feel: " + ex);
        }

        JFrame frame = new JFrame("AttributedString superscript with font");

        frame.getContentPane().add(new JPanel() {
            @Override
            public void paint(Graphics gfx) {
                super.paint(gfx);

                Font bold = gfx.getFont().deriveFont(Font.BOLD);

                // superscript and bold only works fine
                AttributedString test1 = new AttributedString("test superscript and bold");
                test1.addAttribute(TextAttribute.SUPERSCRIPT, TextAttribute.SUPERSCRIPT_SUPER, 5, 16);
                test1.addAttribute(TextAttribute.FONT, bold, 21, 25);

                // both superscript and bold is only rendered as bold
                AttributedString test2 = new AttributedString("test superscript and bold");
                test2.addAttribute(TextAttribute.SUPERSCRIPT, TextAttribute.SUPERSCRIPT_SUPER, 5, 25);
                test2.addAttribute(TextAttribute.FONT, bold, 5, 25);

                gfx.drawString(test1.getIterator(), 5, 20);
                gfx.drawString(test2.getIterator(), 5, 40);
            }

            @Override
            public Dimension getPreferredSize() {
                return new Dimension(200, 70);
            }
        });

        // HTML label works fine
        frame.getContentPane().add(
                new JLabel("<html>test <b>bold</b>, <sup>super</sup> and <b><sup>both</sup></b>"),
                BorderLayout.SOUTH);

        frame.pack();
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setVisible(true);
    }
}


推荐答案

一些完整示例说明:

使用 TextAttribute.SIZE 指定字体大小。

Use TextAttribute.SIZE to specify font size.

使用 TextLayout 计算首选大小,如此处所示。

应该在仅的Swing GUI对象/tutorial/uiswing/concurrency/initial.htmlrel =nofollow也没有eferrer>事件发送线程

Swing GUI objects should be constructed and manipulated only on the event dispatch thread.

Swing程序应覆盖 paintComponent()而不是覆盖 paint()。 - 在AWT和Swing中绘画:绘画方法

"Swing programs should override paintComponent() instead of overriding paint()."—Painting in AWT and Swing: The Paint Methods.

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.font.TextAttribute;
import java.text.AttributedString;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

/** @see https://stackoverflow.com/a/31360762/230513 */
public class AttributedStringTest {

    public static void main(String[] args) {
        EventQueue.invokeLater(new AttributedStringTest()::display);
    }

    private void display() {
        JFrame frame = new JFrame("AttributedString superscript with font");
        frame.getContentPane().add(new JPanel() {
            @Override
            public void paintComponent(Graphics g) {
                super.paintComponent(g);

                // superscript and bold only works fine
                AttributedString test1 = new AttributedString("test superscript and bold");
                test1.addAttribute(TextAttribute.SIZE, 28, 0, 25);
                test1.addAttribute(TextAttribute.SUPERSCRIPT, TextAttribute.SUPERSCRIPT_SUPER, 5, 16);
                test1.addAttribute(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD, 21, 25);

                // both superscript and bold is only rendered as bold
                AttributedString test2 = new AttributedString("test superscript and bold");
                test2.addAttribute(TextAttribute.SIZE, 28, 0, 25);
                test2.addAttribute(TextAttribute.SUPERSCRIPT, TextAttribute.SUPERSCRIPT_SUPER, 5, 25);
                test2.addAttribute(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD, 5, 25);

                g.drawString(test1.getIterator(), 5, 35);
                g.drawString(test2.getIterator(), 5, 70);
            }

            @Override
            public Dimension getPreferredSize() {
                return new Dimension(300, 80);
            }
        });

        // HTML label works fine
        frame.getContentPane().add(new JLabel(
            "<html>test <b>bold</b>, <sup>super</sup> and <b><sup>both</sup></b>", JLabel .CENTER),
            BorderLayout.SOUTH);

        frame.pack();
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setVisible(true);
    }
}

这篇关于Java AttributedString,粗体和上标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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