slo*_*per -2 java unicode fonts swing netbeans
例如,如何转换字符串"Bohinjska ?ešnjica".我从网站获得的字符串,它没有编码unicode.它在使用swing(Netbeans)的程序中工作正常,但是当我通过jar到Windows控制台的链接(运行jframe应用程序)时,它不会在字符串中显示正确"Bohinjska ?ešnjica"的字符'?'和字符'š'.
String example="Bohinjska ?ešnjica";
Run Code Online (Sandbox Code Playgroud)
我从网站上获取该字符串.如何编码或显示,因为它是("Bohinjska ?ešnjica")在一个Swing应用程序,所以当我将运行一个jframe应用程序,它会显示我这个角色(以及其他Unicode字符ofcourse("Z","Z","C"和"S") )?
链接1:我的程序的jar文件从控制台 链接2 运行:当我从netbeans运行程序时
阅读网站上的内容:
URL nov = new URL("http://www.arso.gov.si/vreme/napovedi%20in%20podatki/vreme_avt.html");
URLConnection conn = nov.openConnection(); //connect to a website
BufferedReader br = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
String inputLine;
StringBuilder niz = new StringBuilder();
while ((inputLine = br.readLine()) != null) {
String vrstica = inputLine.trim(); //reading html...
Run Code Online (Sandbox Code Playgroud)
}
在运行时,我们可以Font.canDisplayUpTo(String)用来确定哪些已安装的字体可以显示给定的文本.逻辑字体,例如Font.SANS_SERIF并且Font.SERIF通常由其他字体组成,并且可以覆盖大量不同的脚本.
以下是使用给定文本的示例,其结果显示在此计算机上.

顺便说一句 - 谷歌翻译告诉我这是斯洛文尼亚语而不是克罗地亚语,但幸运的是,完全相同的技术适用于任何脚本.
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.EmptyBorder;
import java.util.Vector;
public class CroationTextInGUI {
private JComponent ui = null;
private String text = "Bohinjska ?ešnjica";
CroationTextInGUI() {
initUI();
}
public void initUI() {
if (ui!=null) return;
ui = new JPanel(new BorderLayout(4,4));
ui.setBorder(new EmptyBorder(4,4,4,4));
String[] fontFamilies = GraphicsEnvironment.
getLocalGraphicsEnvironment().
getAvailableFontFamilyNames();
Vector<String> croatFreindlyFonts = new Vector<String>();
for (String name : fontFamilies) {
Font font = new Font(name, Font.PLAIN, 20);
if (font.canDisplayUpTo(text)<0) {
croatFreindlyFonts.add(name);
}
}
final JList list = new JList(croatFreindlyFonts);
list.setVisibleRowCount(20);
list.getSelectionModel().setSelectionMode(
ListSelectionModel.SINGLE_SELECTION);
ui.add(new JScrollPane(list), BorderLayout.LINE_START);
final JTextArea output = new JTextArea(text, 2, 12);
output.setLineWrap(true);
output.setWrapStyleWord(true);
ui.add(new JScrollPane(output));
ListSelectionListener showFontListener = new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
Font f = new Font(
list.getSelectedValue().toString(), Font.PLAIN, 50);
output.setFont(f);
}
};
list.addListSelectionListener(showFontListener);
list.setSelectedIndex(0);
}
public JComponent getUI() {
return ui;
}
public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception useDefault) {
}
CroationTextInGUI o = new CroationTextInGUI();
JFrame f = new JFrame("Croation Text in GUI");
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);
}
}
Run Code Online (Sandbox Code Playgroud)