【问题标题】:How do I display unicode characters in the Eclipse console window?如何在 Eclipse 控制台窗口中显示 unicode 字符?
【发布时间】:2013-11-18 14:34:08
【问题描述】:

我正在创建一个小型 Java 应用程序,它使用 Unicode 字符在 Eclipse Keplar 的控制台窗口中创建框的行和列。我的代码工作得很好,但是我打印的每个 Unicode 字符的输出都是一个小框,而不是我要打印的 Unicode 字符。

我的代码如下。我有两节课。

我的主要课程:

package assign03;

import java.util.Scanner;

public class Assign03 {

private static int columns;
private static int cWidth;
private static int rows;
private static int rHeight;
private static Table table;

public static void main(String[] args) {
        table = new Table();
        Scanner input = new Scanner(System.in);

        System.out.print("Enter the number of columns: ");
        columns = input.nextInt();

        System.out.print("Enter the width of the columns: ");
        cWidth = input.nextInt();

        System.out.print("Enter the number of rows: ");
        rows = input.nextInt();

        System.out.print("Enter the width of the rows: ");
        rHeight = input.nextInt();

        table.printFirstLine(columns, cWidth);

        for (int r = 0; r < rows; r++) {
            for (int rh = 0; rh < rHeight; rh++) {
                table.printVerticalLine(columns, cWidth);
            }
            if (r < (rows - 1)
                table.printInternalLine(columns, cWidth);
        }

        table.printLastLine(columns, cWidth);
        input.close();
    }
}

还有我的 Table.java 类:

package assign03;

public class Table {

    private char firstLine[] = { '\u2501', '\u250F', '\u2533', '\u2513' };
    private char internalLine[] = { '\u2501', '\u2523', '\u254B', '\u252B' };
    private char lastLine[] = { '\u2501', '\u2517', '\u253B', '\u251B' };
    private char verticalLine[] = { '\u2503' };
    private char whiteSpace[] = { '\u2003' };

    public void printFirstLine(int columns, int cWidth) {

        System.out.print(firstLine[1]);

        for (int c = 0; c < columns; c++) {
            for (int cw = 0; cw < cWidth; cw++) {
                System.out.print(firstLine[0]);
            }
            if (c < (columns - 1))
                System.out.print(firstLine[2]);
        }

        System.out.println(firstLine[3]);
    }

    public void printVerticalLine(int columns, int cWidth) {
        for (int c = 0; c <= columns; c++) {
            System.out.print(verticalLine[0]);
            for (int cw = 0; cw < cWidth; cw++) {
                System.out.print(whiteSpace[0]);
            }
        }
        System.out.println();
    }

    public void printInternalLine(int columns, int cWidth) {
        System.out.print(internalLine[1]);

        for (int c = 0; c < columns; c++) {
            for (int w = 0; w < cWidth; w++) {
                System.out.print(internalLine[0]);
            }
            if (c < (columns - 1))
                System.out.print(internalLine[2]);
        }

        System.out.println(internalLine[3]);
    }

    public void printLastLine(int columns, int cWidth) {

        System.out.print(lastLine[1]);

        for (int c = 0; c < columns; c++) {
            for (int w = 0; w < cWidth; w++) {
                System.out.print(lastLine[0]);
            }
            if (c < (columns - 1))
                System.out.print(lastLine[2]);
        }

        System.out.println(lastLine[3]);
    }
}

应用程序运行时,输出如下。我对列、行、列宽和行宽使用 4 的输入。

我的控制台输出看起来像这样的任何原因?谁能帮我让 Unicode 正确显示?谢谢。

【问题讨论】:

  • 可以自定义Eclipse的控制台输出字体吗?如果是,您是否尝试过选择其他字体?
  • 我已经更改了控制台字体并将 -Dfile.encoding=UTF-8 添加到 eclipse.ini 中,我仍然得到相同的输出。

标签: java eclipse unicode


【解决方案1】:

小框表示当前字体没有这些符号。

您必须找到支持它们的字体并配置控制台字体(搜索“控制台”的首选项)才能使用它。

如果仍然无法正常工作,请确保您已将正在运行的 Java 应用程序(即打印到控制台)配置为发出 UTF-8。在 Eclipse 中,您可以通过打开启动配置并将“控制台编码”设置为 UTF-8(see this blog posthere)来做到这一点

【讨论】:

  • 我最终使用不同的 UTF-8 符号来创建我的盒子,并保持原来的设置。字体不喜欢我使用的字符,所以我使用了可比较但不同的字符,这解决了问题。
【解决方案2】:

对我有用的是转到工作区下的首选项,然后将“文本文件编码”更改为 UTF-8。

【讨论】:

  • 这也有效。您可以在 (Eclipse 2018-12) 下找到确切的设置:Window - Preferences - General - Workspace - Text file encoding(窗口左下角)。
  • 如果您找不到Workspace 部分,请使用搜索选项。
  • 我刚刚再次看到这篇文章:到目前为止写作例如.xml 文件在 Eclipse 的 (2020-06) UTF-8 设置下运行良好,但昨天一个 .txt 文件丢失了一堆符号,甚至控制台也没有正确打印它们。我尝试将StandardCharsets.UTF_8OutputStreamWriter 一起使用,但只有当我将Eclipse 的编码切换回默认值时它才开始正确写入(是的,仍然使用字符集)。看起来这取决于您的源(文件)的编码是什么。
猜你喜欢
  • 1970-01-01
  • 2012-12-13
  • 1970-01-01
  • 2012-06-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多