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

Time:02-03

Picture of the output that I want to change

So I am creating a project that is a skeleton of a Java GUI but I am having some alignment issues. 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());
    
}

CodePudding user response:

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?). Regardless, for that specific panel with the two texts on the top, you could use BoxLayout for adding your JLabels vertically, and use setAlignmentX() to set the horizontal alignment of the texts. Example below:

Edit: Alternatively (regarding underlying and centering the text), you can use the following in the example below:

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

}
  •  Tags:  
  • Related