【问题标题】:how to print a glyph of supplementary characters in java onto my JTextField when i just click the button当我只需单击按钮时,如何将 Java 中的补充字符字形打印到我的 JTextField 上
【发布时间】:2016-02-08 09:50:43
【问题描述】:

我有一个简单的程序,只需在单击按钮时在 JTextField 上设置其 Unicode 值大于字符数据类型(补充字符)的字符。告诉我我真的受够了,我将如何做。这个问题已经花了我4天的时间。

 //importing the packages
import java.awt.event.*;
import javax.swing.*;

import java.util.*;
import java.awt.*;

//My own custom class 
public class UnicodeTest implements ActionListener
{
JFrame jf;
JLabel jl;
JTextField jtf;
JButton jb;
UnicodeTest()
{
    jf=new JFrame();// making a frame
    jf.setLayout(null); //seting the layout null of this frame container
    jl=new JLabel("enter text"); //making the label 
    jtf=new JTextField();// making a textfied onto which a character will be shown
    jb=new JButton("enter");
//setting the bounds 
    jl.setBounds(50,50,100,50);
    jtf.setBounds(50,120,400,100);
    jb.setBounds(50, 230, 100, 100);
    jf.add(jl);jf.add(jtf);jf.add(jb);
    jf.setSize(400,400);
    jf.setVisible(true); //making frame visible
    jb.addActionListener(this); //  registering the listener object
}
public void actionPerformed(ActionEvent e) // event generated on the button click
{   try{

           int x=66560; //to print the character of this code point

           jtf.setText(""+(char)x);// i have to set the textfiled with a code point character which is supplementary in this case 

         }
     catch(Exception ee)// caughting the exception if arrived
        { ee.printStackTrace(); // it will trace the stack frame where exception arrive 
        }   
}
   // making the main method the starting point of our program
  public static void main(String[] args)
   {

    //creating and showing this application's GUI.
      new UnicodeTest();     
  }
}

【问题讨论】:

  • “告诉我我真的受够了..” 你真的受够了。 jf.setLayout(null); Java GUI 必须在不同的语言环境中使用不同的 PLAF 在不同的操作系统、屏幕尺寸、屏幕分辨率等上工作。因此,它们不利于像素完美布局。而是使用布局管理器,或 combinations of them 以及 white space 的布局填充和边框。
  • “我有一个简单的程序只需要..” 好的.. 到底是什么问题?当我使用那个确切的代码时,我得到不合逻辑的结果。当我将 setText 调用更改为使用实际的 Unicode 代码点时(而不是尝试将 int 强制转换为 char,这不符合您的预期)我得到了“缺少字形”字符,给出了唯一的字体这个将呈现该代码点的系统是 Segoe UI Symbol,文本区域的字体是 Arial(或其他一些不支持该字符的无衬线字体)。
  • 字符应该是this吗?
  • @user1803551 在您发表最新评论前 10 多分钟总结了最佳策略。但我的建议是:1)添加一个“?”提问。 2) 回答向您提出的问题,例如“到底是什么问题?” 3) 警惕提供的新解决方案,如果它们不适合您,请解释原因。
  • 如果您是一名软件工程师,您肯定有能力提出适当的问题,回答 cmets 中的问题并遵循给定的详细答案。您只是在 cmets 中重复自己。

标签: java swing unicode jtextfield supplementary


【解决方案1】:

由于您没有提供足够的信息来说明问题所在,我只能猜测其中一个或两个:

  1. 您没有使用可以显示该字符的字体。
  2. 您没有为文本字段提供正确的文本字符串表示形式。

设置可以显示字符的字体

并非所有字体都可以显示所有字符。您必须找到一个(或多个)可以并设置 Swing 组件以使用该字体。您可用的字体取决于系统,因此适合您的字体可能不适用于其他人。您可以在部署应用程序时捆绑字体以确保它适用于所有人。

为了在您的系统上找到可以显示您的角色的字体,我使用了

Font[] fonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts();
for (Font f : fonts) {
    if (f.canDisplay(66560)) {
        System.out.println(f);
        textField.setFont(f.deriveFont(20f));
    }
}

输出(对我来说)是单一字体,所以我允许自己在循环中设置它:

java.awt.Font[family=Segoe UI Symbol,name=Segoe UI Symbol,style=plain,size=1]

正如 Andrew Thompson 对问题的 cmets 所述。

为文本字段提供正确的字符串表示

文本字段需要 UTF-16。 UTF-16 中的补充字符被编码为两个代码单元(其中两个:\u12CD)。假设您从一个代码点开始,您可以将其转换为字符,然后从中创建一个字符串:

int x = 66560;
char[] chars = Character.toChars(x); // chars [0] is \uD801 and chars[1] is \uDC00
textField.setText(new String(chars)); // The string is "\uD801\uDC00"
// or just
textField.setText(new String(Character.toChars(x)));

正如 Andrew Thompson 在 cmets 中对此答案的注释(之前我使用了 StringBuilder)。

【讨论】:

  • @AndrewThompson 更好!已更新。
  • 谢谢!你让我的一天老兄!你摇滚!!!! @Andrew Thompson 的错误是我不知道必须将字体设置为 .非常感谢!
  • @AndrewThompson 我认为他的意思是“我的错误是我不知道......”。
  • 假设我已经完成了上面提到的两个步骤然后仍然没有显示正确的字符那么应该怎么办? @安德鲁
  • “那么应该怎么做?” 发布您的尝试的minimal reproducible exampleShort, Self Contained, Correct Example。添加有关“未显示正确字符”的含义的详细信息(可能与屏幕截图一起)。在一个新的问题线程上做所有这些。并且不要忘记提出具体问题,并添加“?”到最后。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-19
相关资源
最近更新 更多