Unicode特殊字符出现在Java控制台中,但不在Swing中 [英] Unicode special characters appearing in Java console, but not in Swing

查看:253
本文介绍了Unicode特殊字符出现在Java控制台中,但不在Swing中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试向JFrame内的JLabel添加一些高范围的unicode字符,但是尽管使用了已知的支持字体,但它们只能显示为框。但是,当我将这些相同的字符打印到Eclipse控制台时,它们显示得很好。这是我的代码,框架是我的JFrame和textField1是我的JLabel。 Monaco字体与Eclipse控制台使用的字体相同,并且知道支持这个unicode字符:

  JFrame frame = new的JFrame(); 
JLabel textField1 = new JLabel();
frame.add(textField1);
frame.setFocusable(true);
frame.setLayout(new BoxLayout(getContentPane(),BoxLayout.PAGE_AXIS));
frame.setPreferredSize(new Dimension(1000,500));
frame.requestFocusInWindow();
frame.textField1.setFont(new Font(Monaco,Font.PLAIN,11));
frame.textField1.setText(\\\�\\\�);
frame.pack();
frame.setVisible(true);

我已经尝试将JLabel设置为许多不同的字体,但所有更改都是缺少的字符框。但是,如果我打印这个:
$ b $ pre $ System.out.println(\\\�\\\�);

字符在控制台中按预期打印。是否有一些编码问题,防止这些符号在Swing中工作? 。这是它可能被用来创建支持文本的字体的组合。

  String [] fonts = GraphicsEnvironment。 getLocalGraphicsEnvironment()。 
getAvailableFontFamilyNames();
System.out.println(fonts.length +安装的字体系列);
Vector< String> supportedFonts = new Vector<>(); (字符串fontName:字体){
字体f =新字体(fontName,Font.PLAIN,1)
;
if(f.canDisplayUpTo(text)< 0){
System.out.println(fontName);
supportedFonts.add(fontName);
}
}
fontComboBox = new JComboBox(supportedFonts);

这显示了这台计算机上安装的字体的数量,后面是显示的字体列表文本(热狗的字符)。

 安装了225个字体系列
Segoe UI Emoji
Segoe UI符号

这是产生上述输出的源代码,允许用户在所有支持给定的Unicode字符的字体之间进行选择:

  import java.awt。*; 
import java.awt.event。*;
import javax.swing。*;
import javax.swing.border.EmptyBorder;
import java.util.Vector;

public class FontCheck {

private JComponent ui = null;

private final String text =\\\�\\\�;
private JComboBox fontComboBox;
private JTextField outputField = new JTextField(text,5);

FontCheck(){
initUI();

$ b $ public void initUI(){
if(ui!= null)return;

ui = new JPanel(新的BorderLayout(4,4));
ui.setBorder(new EmptyBorder(4,4,4,4));

ui.add(outputField);
ui.add(getToolBar(),BorderLayout.PAGE_START);

refreshFont();


JToolBar getToolBar(){
JToolBar tb = new JToolBar();
tb.setLayout(new FlowLayout());

String [] fonts = GraphicsEnvironment.getLocalGraphicsEnvironment()。
getAvailableFontFamilyNames();
System.out.println(fonts.length +安装的字体系列);
Vector< String> supportedFonts = new Vector<>(); (字符串fontName:字体){
字体f =新字体(fontName,Font.PLAIN,1)
;
if(f.canDisplayUpTo(text)< 0){
System.out.println(fontName);
supportedFonts.add(fontName);
}
}
fontComboBox = new JComboBox(supportedFonts);
ActionListener refreshListener = new ActionListener(){

@Override
public void actionPerformed(ActionEvent e){
refreshFont();
}
};
fontComboBox.addActionListener(refreshListener);

tb.add(fontComboBox);
返回tb;

$ b $ private void refreshFont(){
String fontName = fontComboBox.getSelectedItem()。toString();
Font f = new Font(fontName,Font.PLAIN,60);
outputField.setFont(f);
}

public JComponent getUI(){
return ui;


public static void main(String [] args){
Runnable r = new Runnable(){
@Override
public void run ){
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch(Exception useDefault){
}
FontCheck o = new FontCheck();

JFrame f = new JFrame(o.getClass()。getSimpleName());
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setLocationByPlatform(true);

f.setContentPane(o.getUI());
f.pack();
f.setMinimumSize(f.getSize());

f.setVisible(true);
}
};
SwingUtilities.invokeLater(r);
}
}


I'm attempting to add some high-range unicode characters to a JLabel inside of a JFrame, but they only show up as boxes despite using a known supported font. However, when I print these same characters to the Eclipse console, they show up just fine. Here is my code, with "frame" being my JFrame and "textField1" being my JLabel. The Monaco font is the same font that the Eclipse console uses, and is know to support this unicode character:

JFrame frame = new JFrame();
JLabel textField1 = new JLabel();
frame.add(textField1);
frame.setFocusable(true);
frame.setLayout(new BoxLayout(getContentPane(), BoxLayout.PAGE_AXIS));
frame.setPreferredSize(new Dimension(1000, 500));
frame.requestFocusInWindow();
frame.textField1.setFont(new Font("Monaco", Font.PLAIN, 11 ));
frame.textField1.setText("\uD83C\uDF2D"); 
frame.pack();
frame.setVisible(true);

I've tried setting the JLabel to many different fonts, but all that changes is the relative shape of the "missing character" box. However, if I print this:

System.out.println("\uD83C\uDF2D");

the character prints as expected in the console. Is there some encoding issue that prevents these symbols from working in Swing?

解决方案

The Font.canDisplayUpTo(String) method is your friend here. This is how it might be used to create a combo of the fonts that support the text.

        String[] fonts = GraphicsEnvironment.getLocalGraphicsEnvironment().
                getAvailableFontFamilyNames();
        System.out.println(fonts.length + " font families installed");
        Vector<String> supportedFonts = new Vector<>();
        for (String fontName : fonts) {
            Font f = new Font(fontName, Font.PLAIN, 1);
            if (f.canDisplayUpTo(text)<0) {
                System.out.println(fontName);
                supportedFonts.add(fontName);
            }
        }
        fontComboBox = new JComboBox(supportedFonts);

This shows the number of fonts installed on this computer, followed by a listing of the fonts that will display the text (character for a hot-dog).

225 font families installed
Segoe UI Emoji
Segoe UI Symbol

This is the source code that produces the above output, as well as displaying a GUI that allows the user to choose between all the fonts that support the given Unicode character(s):

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.util.Vector;

public class FontCheck {

    private JComponent ui = null;

    private final String text = "\uD83C\uDF2D"; 
    private JComboBox fontComboBox;
    private JTextField outputField = new JTextField(text, 5);

    FontCheck() {
        initUI();
    }

    public void initUI() {
        if (ui!=null) return;

        ui = new JPanel(new BorderLayout(4,4));
        ui.setBorder(new EmptyBorder(4,4,4,4));

        ui.add(outputField);
        ui.add(getToolBar(), BorderLayout.PAGE_START);

        refreshFont();
    }

    private JToolBar getToolBar() {
        JToolBar tb = new JToolBar();
        tb.setLayout(new FlowLayout());

        String[] fonts = GraphicsEnvironment.getLocalGraphicsEnvironment().
                getAvailableFontFamilyNames();
        System.out.println(fonts.length + " font families installed");
        Vector<String> supportedFonts = new Vector<>();
        for (String fontName : fonts) {
            Font f = new Font(fontName, Font.PLAIN, 1);
            if (f.canDisplayUpTo(text)<0) {
                System.out.println(fontName);
                supportedFonts.add(fontName);
            }
        }
        fontComboBox = new JComboBox(supportedFonts);
        ActionListener refreshListener = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                refreshFont();
            }
        };
        fontComboBox.addActionListener(refreshListener);

        tb.add(fontComboBox);
        return tb;
    }

    private void refreshFont() {
        String fontName = fontComboBox.getSelectedItem().toString();
        Font f = new Font(fontName, Font.PLAIN, 60);
        outputField.setFont(f);
    }

    public JComponent getUI() {
        return ui;
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception useDefault) {
                }
                FontCheck o = new FontCheck();

                JFrame f = new JFrame(o.getClass().getSimpleName());
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                f.setLocationByPlatform(true);

                f.setContentPane(o.getUI());
                f.pack();
                f.setMinimumSize(f.getSize());

                f.setVisible(true);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}

这篇关于Unicode特殊字符出现在Java控制台中,但不在Swing中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆