有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java空间在GUI中排列不正确

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

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

}

**谢谢您的编辑,快蜗牛


共 (1) 个答案

  1. # 1 楼答案

    JTextArea使用的字体使用的是可变宽度字体。为了确保空格和字符占用的空间相等,您需要使用monospaced font,比如Courier New。您可以在JTextArea上设置该选项,也可以使用该选项对图示符进行栅格化。这应该是你在^{中所需要的全部

    public void actionPerformed(ActionEvent e) {
        Font f = new Font("Courier New", Font.BOLD, 20);
        tA.setFont(f);
    

    或者,您可以为字体创建一个static变量,只将其分配给JTextArea一次,并在actionPerformed中重复使用

    它在控制台中工作的原因是,它使用的是单间距字体,这在控制台输出中非常常见

    我应该注意到,用于光栅化字形的字体不必与JTextArea中用于输出的字体匹配。可以使用非等距字体来呈现glyph,但是JTextArea中类似ASCII的输出应该使用等距字体