Unicode 字符不兼容?
Unicode Character uncompatibility?
我在 Java 上使用 swing 进行字符编码时遇到问题。
我要写这个字:
"\u2699"
这是一个简单的 JButton 上的齿轮,但是当我开始我的程序时,我只得到一个带有方形的 JButton 而不是齿轮。这是行:
opt.setText("\u2699");
其中 opt 是按钮。
按钮结果:
我可以更改 swing 字符编码或其他吗?
谢谢
正如@Andreas 指出的那样,需要将按钮设置为使用支持此 Unicode 值的字体。为了解决诸如此类的兼容性问题,fileformat.info 是一个很好的资源。 Here 是已知支持 Gear 字符的字体列表。这些包括例如 DejaVu Sans 和 Symbola。
如 Andreas 所述,使用支持该字符的 Font
。但是除非为应用程序提供合适的字体,否则 Font
API 提供了在 运行 时发现兼容字体的方法。它提供了如下方法:
Font.canDisplay(char)
Font.canDisplay(int)
Font.canDisplayUpTo(String)
- 和其他人.. 有关所有这些的详细信息,请参阅文档。
在这个例子中,我们展示了本系统中将显示齿轮字符的十几种字体。
import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
public class GearFonts {
private JComponent ui = null;
int codepoint = 9881; // Unicode codepoint: GEAR
String gearSymbol = new String(Character.toChars(codepoint));
GearFonts() {
initUI();
}
public void initUI() {
if (ui!=null) return;
ui = new JPanel(new GridLayout(0,2,5,5));
ui.setBorder(new EmptyBorder(4,4,4,4));
Font[] fonts = GraphicsEnvironment.
getLocalGraphicsEnvironment().getAllFonts();
for (Font font : fonts) {
if (font.canDisplay(codepoint)) {
JButton button = new JButton(gearSymbol + " " + font.getName());
button.setFont(font.deriveFont(15f));
ui.add(button);
}
}
}
public JComponent getUI() {
return ui;
}
public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
GearFonts o = new GearFonts();
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);
}
}
我在 Java 上使用 swing 进行字符编码时遇到问题。 我要写这个字:
"\u2699"
这是一个简单的 JButton 上的齿轮,但是当我开始我的程序时,我只得到一个带有方形的 JButton 而不是齿轮。这是行:
opt.setText("\u2699");
其中 opt 是按钮。
按钮结果:
我可以更改 swing 字符编码或其他吗? 谢谢
正如@Andreas 指出的那样,需要将按钮设置为使用支持此 Unicode 值的字体。为了解决诸如此类的兼容性问题,fileformat.info 是一个很好的资源。 Here 是已知支持 Gear 字符的字体列表。这些包括例如 DejaVu Sans 和 Symbola。
如 Andreas 所述,使用支持该字符的 Font
。但是除非为应用程序提供合适的字体,否则 Font
API 提供了在 运行 时发现兼容字体的方法。它提供了如下方法:
Font.canDisplay(char)
Font.canDisplay(int)
Font.canDisplayUpTo(String)
- 和其他人.. 有关所有这些的详细信息,请参阅文档。
在这个例子中,我们展示了本系统中将显示齿轮字符的十几种字体。
import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
public class GearFonts {
private JComponent ui = null;
int codepoint = 9881; // Unicode codepoint: GEAR
String gearSymbol = new String(Character.toChars(codepoint));
GearFonts() {
initUI();
}
public void initUI() {
if (ui!=null) return;
ui = new JPanel(new GridLayout(0,2,5,5));
ui.setBorder(new EmptyBorder(4,4,4,4));
Font[] fonts = GraphicsEnvironment.
getLocalGraphicsEnvironment().getAllFonts();
for (Font font : fonts) {
if (font.canDisplay(codepoint)) {
JButton button = new JButton(gearSymbol + " " + font.getName());
button.setFont(font.deriveFont(15f));
ui.add(button);
}
}
}
public JComponent getUI() {
return ui;
}
public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
GearFonts o = new GearFonts();
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);
}
}