【发布时间】: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