Java 如何在JLabel中使字母的宽度相同?

Java 如何在JLabel中使字母的宽度相同?,java,text,fonts,jlabel,Java,Text,Fonts,Jlabel,我正在开发刽子手游戏,我遇到了一些困难。游戏还没有完成,我只是想让布局和框架正常运行。我的问题是,当我打印绞刑架时,它看起来都是弯曲和错位的。当我在控制台(使用eclipse)和终端上打印相同的内容时,它工作正常(如下)。在我的窗口中,我希望在所有猜测都已完成时,它的外观如下所示: |¯¯¯¯¯| | O | /|\ | / \ ___|___ 目前看起来是这样的: 我用一个GridLayout制作了一个带有jlabel的绞刑架。我尝试通过添加额外的

我正在开发刽子手游戏,我遇到了一些困难。游戏还没有完成,我只是想让布局和框架正常运行。我的问题是,当我打印绞刑架时,它看起来都是弯曲和错位的。当我在控制台(使用eclipse)和终端上打印相同的内容时,它工作正常(如下)。在我的窗口中,我希望在所有猜测都已完成时,它的外观如下所示:

   |¯¯¯¯¯|
   |     O
   |    /|\
   |    / \
___|___
目前看起来是这样的:

我用一个
GridLayout
制作了一个带有jlabel的绞刑架。我尝试通过添加额外的空格来对齐它,但它仍然有点弯曲。有没有办法让所有字符(“|”、“/”、“?”、“\”、“\”、“\”和“”)都占据相同的宽度?有什么字体我可以用吗?以下是我的节目:

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

如果将JLabel上的字体更改为等距字体,则应将其修复。但更好的方法是使用JPanel、paintComponent和Graphics(2D)来绘制刽子手游戏。

这非常有效!我从来都不知道单间距字体。非常感谢。