简体   繁体   English

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

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

我要更改的输出图片

So I am creating a project that is a skeleton of a Java GUI but I am having some alignment issues.所以我正在创建一个项目,它是 Java GUI 的骨架,但我遇到了一些 alignment 问题。 When I run my code the centered top text that says "Help Page" is pushed to the left side, while the help string is shifted downwards a little bit but also pushed to the right.当我运行我的代码时,显示“帮助页面”的居中顶部文本被推到左侧,而帮助字符串向下移动了一点,但也被推到了右侧。 My goal is to have the top text centered and underlined with the other text below it and also centered.我的目标是使顶部文本居中并在其下方的其他文本下加下划线并居中。 I have tried using multiple panels but still nothing has worked, Im guessing it's the mismatching font size by I dont know.我尝试过使用多个面板,但仍然没有任何效果,我猜这是我不知道的字体大小不匹配。 Any help is appreciated!任何帮助表示赞赏!

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());
    
}

It really depends on what you are trying to achieve (for instance, what other components is that JPanel supposed to contain. Is it just those two labels? You show a button in your code as well. Where is that supposed to be added?).这实际上取决于您要实现的目标(例如,JPanel 应该包含哪些其他组件。仅仅是这两个标签吗?您也在代码中显示一个按钮。应该在哪里添加?) . Regardless, for that specific panel with the two texts on the top, you could useBoxLayout for adding your JLabels vertically, and use setAlignmentX() to set the horizontal alignment of the texts.无论如何,对于顶部有两个文本的特定面板,您可以使用BoxLayout垂直添加JLabels ,并使用setAlignmentX()设置文本的水平 alignment。 Example below:下面的例子:

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();
            }
        });
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM