如何在 JLabel 中使字母的宽度相同?
How can I make letters the same width in JLabel?
我正在开发 Hangman 游戏,运行 遇到了一些困难。游戏还没有完成,我只是想正确地获得布局和框架 运行。我的问题是当我打印刽子手时,它看起来全都歪了而且没有对齐。当我在控制台(使用 eclipse)和终端上打印同样的东西时,它工作正常(如下)。在我的 window 中,当所有的猜测都成立时,我希望它看起来像这样:
|¯¯¯¯¯|
| O
| /|\
| / \
___|___
目前看起来是这样的:
我用 GridLayout
用 JLabel 制作刽子手。我已经尝试通过添加额外的 spaces 来对齐它,但它仍然有点弯曲。有没有办法让所有字符(“|”、“/”、“¯”、“\”、“_”和“”)都占用相同数量的 space 宽度?我可以使用某种字体吗?这是我的程序:
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 上的字体更改为 monospaced,它应该会修复它。但更好的方法是使用 JPanel、paintComponent 和 Graphics(2D)
绘制刽子手游戏
我正在开发 Hangman 游戏,运行 遇到了一些困难。游戏还没有完成,我只是想正确地获得布局和框架 运行。我的问题是当我打印刽子手时,它看起来全都歪了而且没有对齐。当我在控制台(使用 eclipse)和终端上打印同样的东西时,它工作正常(如下)。在我的 window 中,当所有的猜测都成立时,我希望它看起来像这样:
|¯¯¯¯¯|
| O
| /|\
| / \
___|___
目前看起来是这样的:
我用 GridLayout
用 JLabel 制作刽子手。我已经尝试通过添加额外的 spaces 来对齐它,但它仍然有点弯曲。有没有办法让所有字符(“|”、“/”、“¯”、“\”、“_”和“”)都占用相同数量的 space 宽度?我可以使用某种字体吗?这是我的程序:
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 上的字体更改为 monospaced,它应该会修复它。但更好的方法是使用 JPanel、paintComponent 和 Graphics(2D)
绘制刽子手游戏