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

时间:2017-05-18 00:52:35

标签: java text fonts jlabel

我正在开发Hangman游戏,我遇到了一些困难。游戏尚未完成(我)只是想让布局和框架正常运行。我的问题是,当我打印刽子手时,它看起来都是弯曲的和未对齐的。当我在控制台(使用eclipse)和终端上打印相同的东西时它工作正常(下面)。在我的窗口中,当所有的猜测都出现时,我希望它看起来像这样:

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

目前看起来像这样:

Current result

我使用GridLayout制作刽子手,使用JLabels。我尝试通过添加额外的空格来对齐它,但它仍然略微弯曲。有没有办法让所有的字符(“|”,“/”,“¯”,“\”,“_”和“”)都占用相同的宽度空间?我可以使用某种字体吗?这是我的计划:

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

1 个答案:

答案 0 :(得分:2)

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