Search code examples
javatextfontsjlabel

How can I make letters the same width in JLabel?


I'm developing Hangman game, and I am running into some difficulties. The game isn't completed (yet), I'm just trying to get the layout and frame running properly. My problem is when I print the hangman, it appears all crooked and misaligned. When I print the same thing on console(with eclipse) and terminal it works fine(below). In my window, I want it to look like this when all of the guesses are up:

   |¯¯¯¯¯|
   |     O
   |    /|\
   |    / \
___|___

It currently looks like this:

Current result

I used a GridLayout to make the hangman, with JLabels. I have tried aligning it by adding extra spaces, but it was still slightly crooked. Is there a way I can make all of the characters("|", "/", "¯", "\", "_", and " ") all take up the same amount of space in width? Is there a certain font I can use? Here's my program:

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

import mycomponents.TitleLabel;

public class Hangman extends JFrame {
    private static final long serialVersionUID = 1L;
    private GridLayout layout = new GridLayout(5,0);
    private Random rand = new Random();
    private String randWord;
    private int wrongGuesses = 6;
    JLabel top = new JLabel();
    JLabel body1 = new JLabel();
    JLabel body2 = new JLabel();
    JLabel body3 = new JLabel();
    JLabel bottom = new JLabel();

    public Hangman() {
        initGUI();
        setTitle("Hangman");
        pack(); 
        setLocationRelativeTo(null);
        setVisible(true);
        setResizable(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    private void initGUI() {
        //TitleLabel is from a different class which I created
        TitleLabel titleLabel = new TitleLabel("Hangman");
        add(titleLabel, BorderLayout.NORTH);
        Font font = new Font(Font.SANS_SERIF, Font.PLAIN, 20);
        JPanel center = new JPanel();
        center.setLayout(layout);
        center.setSize(200,200);
        add(center, BorderLayout.CENTER);
        top.setText("    |¯¯¯¯¯¯|");
        body1.setText("    |");
        body2.setText("    |");
        body3.setText("    |");
        bottom.setText(" __|__ ");
        top.setFont(font);
        body1.setFont(font);
        body2.setFont(font);
        body3.setFont(font);
        bottom.setFont(font);
        center.add(top);
        center.add(body1);
        center.add(body2);
        center.add(body3);
        center.add(bottom);
        JTextPane guess = new JTextPane();
        add(guess, BorderLayout.SOUTH);
        redraw();
    }

    private void redraw() {
        if (wrongGuesses == 1) {
            top.setText("    |¯¯¯¯¯¯|");
            body1.setText("    |    O");
            body2.setText("    |");
            body3.setText("    |");
            bottom.setText("___|___");
        } else if (wrongGuesses == 2) {
            top.setText("    |¯¯¯¯¯¯|");
            body1.setText("    |    O");
            body2.setText("    |    |");
            body3.setText("    |     ");
            bottom.setText("___|___");
        } else if (wrongGuesses == 3) {
            top.setText("    |¯¯¯¯¯¯|");
            body1.setText("    |    O");
            body2.setText("    |    |");
            body3.setText("    |   /");
            bottom.setText("___|___");
        } else if (wrongGuesses == 4) {
            top.setText("    |¯¯¯¯¯¯|");
            body1.setText("    |    O");
            body2.setText("    |    |");
            body3.setText("    |   / \\");
            bottom.setText("___|___");
        } else if (wrongGuesses == 5) {
            top.setText("    |¯¯¯¯¯¯|");
            body1.setText("    |    O");
            body2.setText("    |   /|");
            body3.setText("    |   / \\");
            bottom.setText("___|___");
        } else if (wrongGuesses == 6){
            top.setText("    |¯¯¯¯¯¯|");
            body1.setText("    |    O");
            body2.setText("    |   /|\\");
            body3.setText("    |   / \\");
            bottom.setText("___|___");
        }
    }

    public static void main(String[] args) {
        try {
            String className = UIManager.getCrossPlatformLookAndFeelClassName();
            UIManager.setLookAndFeel(className);
        }
        catch (Exception e) {}

        EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Hangman();
            }
        });
    }
}

Solution

  • If you change the font on your JLabels to monospaced it should fix it. But the better way to do it is to paint the hangman game all using a JPanel, paintComponent, and Graphics(2D)