当添加的文本是两种不同的字体大小时,如何在 JPanel 中开始换行

How can I start a newline in a JPanel when the added text are two different font sizes

所以我正在创建一个项目,该项目是 Java GUI 的骨架,但我遇到了一些对齐问题。当我 运行 我的代码时,显示“帮助页面”的居中顶部文本被推到左侧,而帮助字符串向下移动一点但也被推到右侧。 我的目标是让顶部文本居中并在其下方的其他文本下划线并居中。我试过使用多个面板,但仍然没有任何效果,我猜这是我不知道的不匹配的字体大小。感谢您的帮助!

private void helpGUI() {
    
    clearGUI();
    helpStr = "<html><br>This is the help page where the user can come for help<html/>";
    
    label = new JLabel("<html><u>Help Page</u></html>");
    label.setFont(new Font("Times", Font.PLAIN, 24));
    helpTxt = new JLabel(helpStr);
    helpTxt.setFont(new Font("Times", Font.PLAIN, 16));
    panel.add(label);   
    panel.add(helpTxt);
    panel.setAlignmentX(CENTER_ALIGNMENT);      
    
    
    button = new JButton("Previous");
    bttnPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
    bttnPanel.add(button);

    frame.add(panel);
    
    class previousButton implements ActionListener {
        public void actionPerformed (ActionEvent e) {
            GUIPG1(name);
        }
    }
    
    button.addActionListener(new previousButton());
    
}

这实际上取决于您要实现的目标(例如,JPanel 应该包含哪些其他组件。它只是这两个标签吗?您还在代码中显示了一个按钮。它应该在哪里要添加吗?)。无论如何,对于顶部有两个文本的特定面板,您可以使用 BoxLayout 垂直添加 JLabels,并使用 setAlignmentX() 设置文本的水平对齐方式。示例如下:

编辑: 或者(关于文本的底层和居中),您可以在下面的示例中使用以下内容:

titleLbl = new JLabel("<html><u>Help Page</u></html>", SwingConstants.CENTER);
titleLbl.setFont(new Font("Times New Roman", Font.PLAIN, 24));
titleLbl.setAlignmentX(JLabel.CENTER_ALIGNMENT);

App.java

import java.awt.*;
import java.awt.font.*;
import javax.swing.*;
import java.util.*;

public class App {

    private void addComponentsToPane(Container pane) {
        JLabel titleLbl = new JLabel("Help Page");

        // add text attributes (i.e., underline, font family, font size, etc)
        Font font = titleLbl.getFont();
        Map<TextAttribute, Object> attributes = new HashMap<>(font.getAttributes());
        attributes.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
        attributes.put(TextAttribute.FAMILY, "Times New Roman");
        attributes.put(TextAttribute.SIZE, 24);
        titleLbl.setFont(font.deriveFont(attributes));
        titleLbl.setAlignmentX(JLabel.CENTER_ALIGNMENT);

        JLabel infoLbl = new JLabel("This is the help page where the user can come for help");
        infoLbl.setAlignmentX(JLabel.CENTER_ALIGNMENT);
        infoLbl.setFont(new Font("Times New Roman", Font.PLAIN, 16));

        Box box = new Box(BoxLayout.Y_AXIS);
        box.add(titleLbl);
        box.add(Box.createRigidArea(new Dimension(0, 5)));// creates space between the JLabels
        box.add(infoLbl);

        pane.add(box, BorderLayout.NORTH);

    }

    private void createAndShowGUI() {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        addComponentsToPane(frame.getContentPane());
        frame.setSize(640, 480);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new App().createAndShowGUI();
            }
        });
    }

}