JLabel在显示HTML时垂直向下移动文本 [英] JLabel shifts the text vertically down while displaying HTML

查看:149
本文介绍了JLabel在显示HTML时垂直向下移动文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想理解为什么呈现HTML的JLabel会移动它的输出文本的垂直位置,而呈现非HTML的JLabel则不会。

I would like to understand why a JLabel, rendering HTML, shifts the vertical position of it's output text, whereas a JLabel which renders non-HTML, does not.


  • 使用的Java版本:1.6.0_37

  • 使用Swing外观:Windows(com .sun.java.swing.plaf.windows.WindowsLookAndFeel)

  • 操作系统:Windows 7 64位

  • Java version used: 1.6.0_37
  • Swing Look and Feel used: Windows ("com.sun.java.swing.plaf.windows.WindowsLookAndFeel")
  • OS: Windows 7 64 Bit

由于代码非常简单,我没有把SSCCE放在一起。但如果它有帮助,请说出来。

I did not put a SSCCE together since the code is really trivial. But if it helps please say so.

我宁愿使用显示行为的图片给出一个例子:

I rather give an examples using an images displaying the behavior:

我将一个JPanel作为JLabel周围的容器来显示标签的边界。设置JLabel的字体和文本后,

I put a JPanel as the container around the JLabel to visualize the label's bounds. After setting the font and text for the JLabel, the

jLabel.getPreferredSize()

方法返回呈现的纯文本或HTML的边界(这是我为周围的JPanel设置的确切大小)。你可以清楚地看到,如果渲染HTML,整个文本会向下移动一小部分。

method returns the bounds of the rendered plain text or HTML (and this is the exact size I set for the surrounding JPanel). You can clearly see, that, if rendering HTML, the whole text is shifted a small amount down.

我想知道为什么会发生这种情况以及我能做些什么更正位置。

I would like to know why this happens and what I can do to correct the placement.

一种解决方法是翻译要渲染文本的Graphics2D,以补偿垂直移位,如下所示:

One workaround would be to translate the Graphics2D on which to render the text, to compensate the vertical shift, like this:

g2d.translate( 0, -20 );

但是我不知道与字体度量相关的正确y值(例如字体大小) 。无论如何,这种解决方法也感觉错误。

But I don't know the correct y value in relation to the font metrics (e.g. font size). Anyway, this workaround also feels "wrong".

我非常感谢你的回答,非常感谢!

I really appreciate your answers, thanks a lot!

推荐答案

似乎我们为HTML JLabel Font (系列,大小等) c $ c>使用 setFont(..)字体未呈现为 JLabel 的正确指标。

It seems that if we set the Font (family,size etc) for the HTML JLabel using setFont(..) the font is not rendered to the correct metrics of JLabel.

这是我演示的示例(显示的 JLabel 都使用HTML):

Here is an example I made to demonstrate (Both JLabels shown are using HTML):

一个简单的解决方法是HTML中的字体大小,族等。

A simple work around is to the the font size, family etc in HTML too.

我们可以看到 cyan HTML JLabel 使用 setFont(..)(并且错误地呈现)绿色 HTML JLabel 使用HTML设置字体并正确呈现:

As we can see the cyan HTML JLabel used setFont(..) (and was incorrectly rendered) while the green HTML JLabel used HTML to set the font and was rendered correctly:

JLabel labelHtml2 = new JLabel("<html><font size=10 family='Calibri'>" + text + "</font></html>");

Test.java:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static Font font = new Font("Calibri", Font.PLAIN, 38);

    public Test() {
        initComponents();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();

                }
                new Test();
            }
        });
    }

    private void initComponents() {
        final JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        String text = "Hello world";

        //this label will not render correctly due to setting font via setFont(..)
        JLabel labelHtml1 = new JLabel("<html>" + text + "</html>");
        labelHtml1.setBackground(Color.CYAN);
        labelHtml1.setOpaque(true);//so background will be painted
        labelHtml1.setFont(font);

        //this label will render correcty font is set via html
        JLabel labelHtml2 = new JLabel("<html><font size=10 family='Calibri'>" + text + "</font></html>");
        labelHtml2.setBackground(Color.GREEN);
        labelHtml2.setOpaque(true);
        //labelHtml2.setFont(font);

        frame.add(labelHtml1, BorderLayout.NORTH);
        frame.add(labelHtml2, BorderLayout.SOUTH);

        frame.pack();
        frame.setVisible(true);

    }
}

这篇关于JLabel在显示HTML时垂直向下移动文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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