How can I make letters the same width in JLabel?

I am developing a Hangman game and am having some difficulties. The game is not over (yet), I'm just trying to get my layout and frame set up correctly. My problem is when I type the hangman it looks all crooked and inconsistent. When I print the same to the console (with eclipse) and terminal, it works fine (below). In my window, I want it to look like this when all the guesswork is:

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

      

It now looks like this:

Current result

I used GridLayout

to make the hangman, with JLabels. I tried to align it by adding extra spaces, but it's still a little crooked. Is there a way to make all characters ("|", "/", "¯", "\", "_" and "") occupy the same width? Is there a specific 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();
            }
        });
    }
}

      

+3


source to share


1 answer


If you change the font from JLabels to monospaced it should fix it. But the best way to do it is to draw the hangman game using JPanel, paintComponent and Graphics (2D).



+2


source







All Articles