从jar运行时,为什么字体无法正确加载?

时间:2020-06-08 16:30:46

标签: java fonts

我编写了一个Java类来将System.out定向到JTextComponent

import java.awt.Color;
import java.awt.Font;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;

import javax.swing.SwingUtilities;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.JTextComponent;

public class RedirectOutput {

    private JTextComponent textComponent;

    public static void sendTo(JTextComponent textComponent) {
        new RedirectOutput(textComponent).redirectSystemStreams();
    }

    private RedirectOutput(JTextComponent textComponent) {
        this.textComponent = textComponent;
        textComponent.setEditable( false );
        textComponent.setBackground(new Color(255, 255, 204));
        textComponent.setFont(new Font("Tahoma", Font.PLAIN, 14));
    }

    private void updateTextComponent(final String text) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                Document doc = textComponent.getDocument();
                try {
                    doc.insertString(doc.getLength(), text, null);
                } catch (BadLocationException e) {
                    throw new RuntimeException(e);
                }
                textComponent.setCaretPosition(doc.getLength() - 1);
            }
        });
    }

    private void redirectSystemStreams() {
        OutputStream out = new OutputStream() {
            @Override
            public void write(final int b) throws IOException {
                updateTextComponent(String.valueOf((char) b));
            }

            @Override
            public void write(byte[] b, int off, int len) throws IOException {
                updateTextComponent(new String(b, off, len));
            }

            @Override
            public void write(byte[] b) throws IOException {
                write(b, 0, b.length);
            }
        };

        try {
            PrintStream out2 = new PrintStream(out, true, "UTF-8");
            System.setOut(out2);
            System.setErr(out2);
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
//      System.setOut(new PrintStream(out, true));
//      System.setErr(new PrintStream(out, true));
    }

}

当我在Eclipse中调试所有东西时,它们都可以正常工作。但是从jar运行时字体无法正确加载,这是一个屏幕截图: enter image description here

有人可以帮助我吗? P / s:我尝试打印越南语,所以我在PrintStream中将字符集设置为UTF-8。

0 个答案:

没有答案