Java 中的 Unicode 符号(箭头)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2183098/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unicode symbols (arrows) in Java
提问by cupakob
i want to use following symbols for buttons in my app:
我想对我的应用程序中的按钮使用以下符号:
arrows http://img402.imageshack.us/img402/3176/arrowso.jpg
箭头 http://img402.imageshack.us/img402/3176/arrowso.jpg
here my code:
这是我的代码:
Button goToFirstButton = new Button("\uE318");
Button prevPageButton = new Button("\uE312");
Button nextPageButton = new Button("\uE313");
Button goToLastButton = new Button("\uE319");
and the result is
结果是
result http://img693.imageshack.us/img693/9063/resultbu.jpg
结果 http://img693.imageshack.us/img693/9063/resultbu.jpg
It seems, that \uE318 and \uE313 are wrong. What should i use instead? For goToLastButton and goToFirstButton i prefer to use this images
看来,\uE318 和\uE313 是错误的。我应该用什么代替?对于 goToLastButton 和 goToFirstButton 我更喜欢使用这个图像
alt text http://img3.imageshack.us/img3/5724/singlearrow.jpg
替代文字 http://img3.imageshack.us/img3/5724/singlearrow.jpg
but i can't find, which code should i use.
但我找不到,我应该使用哪个代码。
回答by Artyom
I would suggest to use Icons on Buttons instead of special characters, because the ability to display may be strongly affected by availability of fonts on client workstation.
我建议在按钮上使用图标而不是特殊字符,因为显示能力可能会受到客户端工作站上字体可用性的强烈影响。
回答by nd.
The unicode codepoints you want to use are part of a private use area, i.e. every font manufacturer is free to put whatever character they like at whatever position. The font you used to look up the arrow characters is simply a different font than the one used for displaying the button text. If the button text maps \uE318 and \uE313 to some Chinese (?) graphem, then that's not wrong, just different.
您想要使用的 unicode 代码点是私人使用区域的一部分,即每个字体制造商都可以自由地将他们喜欢的任何字符放在任何位置。用于查找箭头字符的字体与用于显示按钮文本的字体完全不同。如果按钮文本将 \uE318 和 \uE313 映射到某些中文 (?) 字形,那么这没有错,只是不同。
回答by Conspicuous Compiler
Although multiple people have made the argument you should avoid using these codepoints because you can't rely on the users' systems having a font which displays these characters, the reason you're getting the wrong characters in your example case has been entirely missed. All of the symbols you are trying to draw are in the "private use area" which means that the symbols involved will potentially be different in every single font.
尽管有很多人提出了您应该避免使用这些代码点的论点,因为您不能依赖具有显示这些字符的字体的用户系统,但您在示例案例中得到错误字符的原因却完全被忽略了。您尝试绘制的所有符号都在“私人使用区域”中,这意味着所涉及的符号可能在每种字体中都不同。
Private Use Area (E000-F8FF) * The Private Use Area does not contain any character assignments, consequently no character code charts or namelists are provided for this area.
私人使用区 (E000-F8FF) * 私人使用区不包含任何字符分配,因此不提供此区域的字符代码表或名称列表。
If you embed the particular font you want to use to insure you can use these codepoints, that's fine. But that does mean you should, indeed, use the \u#### notation in your code, because embedding the characters as Unicode directly means the source won't make sense unless somebody views it in the correct font.
如果您嵌入要使用的特定字体以确保您可以使用这些代码点,那很好。但这确实意味着您确实应该在代码中使用 \u#### 符号,因为将字符直接嵌入为 Unicode 意味着除非有人以正确的字体查看源代码,否则源代码将毫无意义。
All in all, it's probably better to use icons unless you already have a symbol font you think is simply far superior to any graphical work you would otherwise do.
总而言之,使用图标可能会更好,除非您已经拥有一种符号字体,您认为它比其他方式所做的任何图形工作都好得多。
回答by McDowell
◀ ▶
◀ ▶
There are a couple of symbols close to what you want in the Geometric Shapes chart. However, as others have said, use icons and stay out of the private use area.
在几何形状图表中,有几个符号与您想要的很接近。然而,正如其他人所说,使用图标并远离私人使用区域。
回答by tmb
I would suggest that if you are worried about the client font's ability to display a particular character, use the following:
我建议如果您担心客户端字体显示特定字符的能力,请使用以下命令:
private static final HashMap<Character, Font> fontCache = new HashMap<Character, Font>();
public static Font getFontThatCanDisplay(char c, int style, float size) {
Font f = fontCache.get(c);
if (f == null) {
f = UIManager.getFont("Label.font");
for (Font font: GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts()) {
if (font.canDisplay(c)) {
f = font;
break;
}
}
fontCache.put(c, f);
}
return f.deriveFont(style, size);
}
回答by Steve McLeod
I also suggest using icons. Not all fonts have symbols for all unicode character points.
我也建议使用图标。并非所有字体都具有所有 unicode 字符点的符号。
You may get it working by specifying a particular font. But what if that font is found on Windows 7 computers, but not on Mac OS X or Linux or Windows XP? Then the system will choose another font, based on the browser defaults, and that default might not have the symbols you want.
您可以通过指定特定字体来使其工作。但是如果在 Windows 7 计算机上找到该字体,但在 Mac OS X、Linux 或 Windows XP 上找不到呢?然后系统将根据浏览器默认值选择另一种字体,而该默认值可能没有您想要的符号。
回答by Joonas Pulakka
Java source files are UTF-8 encoded, so you can put the symbols you want directly in the source code (just copy 'n paste from a font viewer or web), as long as you use a decent editor. No need to use this confusing "\uXXXX" notation. For instance, I've found this useful for Greek letters commonly used in scientific notation (δ, ρ, ψ...) - you can even use them as variable names.
Java 源文件采用 UTF-8 编码,因此您可以将所需的符号直接放入源代码中(只需从字体查看器或 Web 中复制粘贴即可),只要您使用合适的编辑器即可。无需使用这种令人困惑的“\uXXXX”符号。例如,我发现这对于科学记数法中常用的希腊字母(δ、ρ、ψ...)很有用 - 您甚至可以将它们用作变量名。
Of course, your font of choice must have the symbols you want, otherwise it won't work.
当然,您选择的字体必须具有您想要的符号,否则将无法使用。
回答by trashgod
I will defend using Unicode glyphs on buttons, but it's really easy to implement the Iconinterface and use paintIcon()to draw anything you want. The tutorial "Creating a Custom Icon Implementation"is a good example; this code shows a more complex, animated histogram.
我将捍卫在按钮上使用 Unicode 字形,但实现Icon界面并使用它paintIcon()来绘制任何你想要的东西真的很容易。教程 “Creating a Custom Icon Implementation”就是一个很好的例子;此代码显示了更复杂的动画直方图。

