JLabel HTML 上标混淆了格式。有什么技巧可以解决这个问题吗?

JLabel HTML superscript is messing with formatting. Any tricks to get around this?

我正在尝试使用 HTML 格式来在我的 JLabel 上显示上标,但它似乎比标准字体占用更多空间,所以所有 [=12=带有上标的下方的 ] 被向下推到不合适的位置。每个 JLabel 都应该与 JTextField 对齐,这就是为什么它看起来有点乱。

我的描述可能有点绕,但是可以编译下面的代码,这样您就可以明白我在说什么了。第5个标签是带上标的标签,导致其余标签越线。

import javax.swing.*;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;

class JLabelExample extends JFrame {

    private JPanel wrapper, configInputPanel;
    private GridBagConstraints gbc;

    JLabelExample() {
        configInputPanel();
        wrapper();
        add(wrapper);

        pack();
        setVisible(true);
    }

    private void wrapper() {
        wrapper = new JPanel(new GridBagLayout());
        wrapper.setBorder(BorderFactory.createEmptyBorder(25, 25, 20, 25));
        JPanel inner = new JPanel(new GridBagLayout());
        inner.setBorder(BorderFactory.createCompoundBorder(
            BorderFactory.createTitledBorder("Configuration"),
            BorderFactory.createEmptyBorder(15, 15, 15, 15)));

        gbc = new GridBagConstraints();
        gbc.insets = new Insets(5, 5, 5, 5);
        inner.add(configInputPanel, gbc);

        gbc.gridx = 0;
        wrapper.add(inner, gbc);
    }

    private void configInputPanel() {
        configInputPanel = new JPanel(new GridBagLayout());
        gbc = new GridBagConstraints();
        gbc.anchor = GridBagConstraints.WEST;
        gbc.insets = new Insets(0, 5, 0, 5);
        gbc.gridx = 0;
        configInputPanel.add(new JLabel("Fuselage Length (m)"), gbc);
        configInputPanel.add(new JLabel("Fuselage Width (m)"), gbc);
        configInputPanel.add(new JLabel("Fuselage Height (m)"), gbc);
        configInputPanel.add(new JLabel("Wing Span (m)"), gbc);
        configInputPanel.add(new JLabel("<html>Wing Area (m<sup>2</sup>)</html>"), gbc);
        configInputPanel.add(new JLabel("Wing Sweep (degree)"), gbc);
        configInputPanel.add(new JLabel("Nose Gear to Fuselage Tip (m)"), gbc);
        configInputPanel.add(new JLabel("Main Gear to Nose Gear (m)"), gbc);
        configInputPanel.add(new JLabel("Main Gear Separation (m)"), gbc);
        configInputPanel.add(new JLabel("Body Gear to Nose Gear (m)"), gbc);
        configInputPanel.add(new JLabel("Body Gear Separation (m)"), gbc);
        configInputPanel.add(new JLabel("Engine to Centerline (m)"), gbc);

        gbc.gridx = 1;
        for (int i = 0; i < 12; i++) {
            JTextField textfield = new JTextField(5);
            textfield.setHorizontalAlignment(JTextField.RIGHT);
            configInputPanel.add(textfield, gbc);
        }
    }

    public static void main(String[] args) {
        new JLabelExample();
    }
}

既然你在寻找技巧,我允许自己提出这个建议。它不完全是上标,但更易读。

private void configInputPanel() {

    JPanel trickPanel = new JPanel(new FlowLayout(FlowLayout.LEADING, 0, 0));
    JLabel label1 = new JLabel("Wing Area (m");
    JLabel supLabel = new JLabel("2");
    Font font = label1.getFont();
    supLabel.setFont(font.deriveFont(font.getSize() / 1.5f));
    trickPanel.add(label1);
    trickPanel.add(supLabel);
    trickPanel.add(new JLabel(")"));

    configInputPanel = new JPanel(new GridBagLayout());
    gbc = new GridBagConstraints();
    gbc.anchor = GridBagConstraints.WEST;
    gbc.insets = new Insets(0, 5, 0, 5);
    gbc.gridx = 0;
    configInputPanel.add(new JLabel("Fuselage Length (m)"), gbc);
    configInputPanel.add(new JLabel("Fuselage Width (m)"), gbc);
    configInputPanel.add(new JLabel("Fuselage Height (m)"), gbc);
    configInputPanel.add(new JLabel("Wing Span (m)"), gbc);
    configInputPanel.add(trickPanel, gbc);
    configInputPanel.add(new JLabel("Wing Sweep (degree)"), gbc);
    configInputPanel.add(new JLabel("Nose Gear to Fuselage Tip (m)"), gbc);
    configInputPanel.add(new JLabel("Main Gear to Nose Gear (m)"), gbc);
    configInputPanel.add(new JLabel("Main Gear Separation (m)"), gbc);
    configInputPanel.add(new JLabel("Body Gear to Nose Gear (m)"), gbc);
    configInputPanel.add(new JLabel("Body Gear Separation (m)"), gbc);
    configInputPanel.add(new JLabel("Engine to Centerline (m)"), gbc);

    gbc.gridx = 1;
    for (int i = 0; i < 12; i++) {
        JTextField textfield = new JTextField(5);
        textfield.setHorizontalAlignment(JTextField.RIGHT);
        configInputPanel.add(textfield, gbc);
    }
}

gap-less LEADING 对齐的 FlowLayout 给人的印象是连续的单个标签。较小的字体大小是上标大小的近似值。对于不同的字体,它可能需要调整,但由于它是从它的姊妹标签字体派生出来的,所以在任何情况下都应该是合理的。