GUI 中的空格不正确排列

Spaces not lining up right in GUI

我在 this image 中的 JTextArea 输出中遇到了一个奇怪的问题。我的程序要做的是通过 GUI 接受输入字符串,将其保存为图像,并将值 > 0 的每个像素设置为“”。这完全可以在控制台中找到,但是当它打印到 JTextArea 时,空格似乎只有“”大小的一半。我知道这个问题不是我的代码的实际问题,而是 JTextAreas 的一些内置问题。我将包含下面的代码,但我觉得唯一相关的行就在最后:tA.setText(output);

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.font.FontRenderContext;
import java.awt.font.GlyphVector;
import java.awt.font.TextLayout;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.awt.image.Raster;
import javax.swing.*;

public class Project4 extends JPanel implements ActionListener {

public static Project4 p = new Project4();
public static JTextField input = new JTextField("Java 2D");
public static JTextArea tA = new JTextArea();

public static void main(String[] args) {
    JFrame frame = new JFrame("Project 4");
    frame.setSize(600, 400);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    JPanel panel = new JPanel();
    panel.add(p, BorderLayout.CENTER);
    input.setColumns(40);
    panel.add(input);       
    JButton print = new JButton("Print");
    print.addActionListener(p);
    panel.add(print);
    panel.add(tA);
    frame.add(panel);
    frame.setVisible(true);
}

@Override
public void actionPerformed(ActionEvent e) {
    Font f = new Font("Times New Roman", Font.BOLD, 20);
    FontRenderContext frc = new FontRenderContext(null, false, false);
    GlyphVector gv = f.createGlyphVector(frc, input.getText());
    Rectangle2D bounds = gv.getLogicalBounds();
    TextLayout tL = new TextLayout(input.getText(), f, frc);
    float ascent = tL.getAscent();
    BufferedImage b = new BufferedImage((int) bounds.getWidth(), 
        (int) bounds.getHeight(), BufferedImage.TYPE_BYTE_BINARY);
    Graphics2D g2 = b.createGraphics();
    g2.drawGlyphVector(gv, 0, ascent);
    g2.setPaint(Color.BLACK);
    g2.drawString(input.getText(), input.getAlignmentX(), input.getAlignmentY());

    String output = "", line = "";
    int spaceCount = 0;
    Raster r = b.getRaster();
    int[] i = new int[1];
    for (int y = 0; y < (int) bounds.getHeight() - 1; y++) {
        for (int x = 0; x < (int) bounds.getWidth() - 1; x++) {
            r.getPixel(x, y, i);
            int pixel = i[0];
            if (pixel > 0) {
                line += "*";
            } else {
                line += " ";
                spaceCount++;
            }
        }

        if (spaceCount != line.length())
            output += line + "\n";
        spaceCount = 0;
        line = "";
    }

    System.out.print(output);
    tA.setText(output);
}

}

** 感谢编辑,快蜗牛!

JTextArea 使用的字体是可变宽度字体。为了确保您的 space 和字符占用等量的 space,您需要使用 monospaced font,例如 Courier New。您可以在 JTextArea 上设置它,也可以将其用于光栅化字形。这应该是您 actionPerformed.

中所需要的全部内容
public void actionPerformed(ActionEvent e) {
    Font f = new Font("Courier New", Font.BOLD, 20);
    tA.setFont(f);

或者,您可以为字体创建一个 static 变量,只将其分配给 JTextArea 一次,然后在 actionPerformed.

中重复使用

它在您的控制台中工作的原因是因为它使用的是单色spaced 字体,这对于控制台输出来说很常见。

我应该注意,您用来栅格字形的字体不必与您在 JTextArea 中用于输出的字体相匹配。您可以使用非单声道spaced 字体来呈现您的字形,但您在 JTextArea 中的类 ASCII 输出应该使用单声道spaced 字体。