首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在JLabel中使字母宽度相同?

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

Stack Overflow用户
提问于 2017-05-18 00:52:34
回答 1查看 155关注 0票数 3

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

代码语言:javascript
复制
   |¯¯¯¯¯|
   |     O
   |    /|\
   |    / \
___|___

目前的情况如下:

我用GridLayout做刽子手,用JLabels。我已经尝试通过增加额外的空间来调整它,但是它还是有点弯曲。有没有办法让所有的字符( "\“、"/”、“++”、“\”、"_“和”")都占用相同的宽度?有我可以用的字体吗?这是我的节目:

代码语言:javascript
复制
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();
            }
        });
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-05-18 00:58:47

如果您将JLabels上的字体更改为单步长,它应该会修复它。但更好的方法是使用JPanel、paintComponent和图形(2D)来绘制挂人游戏。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44036923

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档