JTextArea组件中的Unicode支持 [英] Unicode Support in JTextArea Component

查看:85
本文介绍了JTextArea组件中的Unicode支持的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个简单的程序,其中我想在JTextArea中显示Unicode符文字符\ u16e6.该符号显示在JFrame的顶部,而不显示在JTextArea中.我已经将JTextArea的字体设置为与JFrame相同,但是它仍只会将\ u16e6显示为一个空框.如何在JTextArea中显示此字符或任何任意unicode字符?

I have made this simple program in which I would like to display the unicode rune character \u16e6 in a JTextArea. This symbol is displayed at the top of the JFrame, but not in the JTextArea. I have set the font of the JTextArea to be the same as the JFrame, but it will still only display \u16e6 as an empty box. How can I display this, or any arbitrary unicode character, in a JTextArea?

import java.awt.*;
import javax.swing.*;


public class WTextArea{
   public static void main(String[] args){
      JFrame frame = new JFrame("\u16e6");
               frame.setSize(1000,1000);
               frame.setVisible(true);
               frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      System.out.println(frame.getFont().toString());
      Container content = frame.getContentPane();

      JTextArea jta = new JTextArea();
      jta.setFont(frame.getFont());
      content.add(jta);

      jta.setText("\u16e6");

   }
}

推荐答案

我将JTextArea的字体设置为与JFrame相同,-

I have set the font of the JTextArea to be the same as the JFrame, -

JFrame的标题栏不是Swing组件,而是OS窗口小部件.因此,操作系统使用的字体与框架的getFont()方法中返回的Font不同.因此,文本区域的字体不是您认为应该的字体,这就是为什么文本区域无法呈现字符的原因.

The title bar of a JFrame is not a Swing component, it is an OS widget. So the font used by the OS is not the same as the Font returned in the getFont() method of the frame. Therefore the Font of the text area is not the Font you think it should be which is why the text area can't render the character.

我不知道如何确定OS框架使用的字体是什么.即使我们可以确定,Swing也可能无法使用它.

I have no ideas how to determine what the Font used by the OS frame is. Even if we could determine that, it may not be available to Swing.

因此,您需要找到一种呈现"\ u16e6"字符的字体.下面的程序是找到这种字体的蛮力方法.它显示所有可用于Swing的字体.因此,我一次只选择了Font,直到找到可以显示您符号的Font.

So you need to find a font that renders the "\u16e6" character. The following program is a brute force method to find such a Font. It displays all the Fonts available to Swing. So I simply selected on Font at a time until I found a Font that display your symbol.

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.plaf.basic.*;

public class ComboBoxFonts extends JFrame implements ItemListener
{
    JTextArea textArea;
    JComboBox comboBox;

    public ComboBoxFonts()
    {
        Font font = new Font("Courier New", Font.PLAIN, 16);

        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment ();
        Font [] fonts = ge.getAllFonts ();

        comboBox = new JComboBox( fonts );
        comboBox.setFont( font);
        comboBox.addItemListener( this );
        add( comboBox, BorderLayout.SOUTH );

        textArea= new JTextArea("Some text - \u16e6 -", 3, 20);
        textArea.setFont( font.deriveFont( 24.0f) );
        add( new JScrollPane( textArea ) );
    }

    public void itemStateChanged(ItemEvent e)
    {
        Font font = (Font)e.getItem();
        textArea.setFont( font.deriveFont( 24.0f ) );
    }

    public static void main(String[] args)
    {
        ComboBoxFonts frame = new ComboBoxFonts();
        frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
        frame.pack();
        frame.setLocationRelativeTo( null );
        frame.setVisible( true );
    }
}

在我的Windows平台上,唯一的字体显示为:

On my Windows platform the only Font appears to be:

//jta.setFont(frame.getFont());
jta.setFont(new Font("Segoe UI Symbol", Font.PLAIN, 18) );

这篇关于JTextArea组件中的Unicode支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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