Unicode characters in app doesn't show correctly
790 просмотра
3 ответа
How to convert string like that for example "Bohinjska Češnjica"
. The string that I get from the website and it is not encoded in unicode
. It works just fine in program that uses swing(Netbeans
), but when I past the link to jar to windows console(to run jframe
application) it doesn't show correct characters in the string "Bohinjska Češnjica"
for characters 'Č'
and 'š'
.
String example="Bohinjska Češnjica";
I get that string from website.
How do I encode or show as it is ("Bohinjska Češnjica")
in a Swing application, so when i will run a jframe
application it will show me this characters(and others unicode characters ofcourse ('Ž','ž','č' and 'Š'))?
Link 1 :jar file of my program runned from console Link 2:when i run program from netbeans
READING CONTENT FROM WEBSITE :
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...
}
Автор: slodeveloper Источник Размещён: 12.11.2019 09:53Ответы (3)
5 плюса
At run-time, we can use Font.canDisplayUpTo(String)
to determine which of the installed fonts can display a given text. Logical fonts such as Font.SANS_SERIF
and Font.SERIF
typically are made of of other fonts and can cover vast ranges of different scripts.
Here is an example using the given text, with the results seen on this machine.
BTW - Google translate is telling me that is Slovenian rather than Croation, but fortunately, the exact same technique will work for any script.
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);
}
}
Автор: Andrew Thompson
Размещён: 13.03.2015 09:40
0 плюса
Try using fonts which support unicode characters like Arial Unicode MS.
Автор: Mandeep Rajpal Размещён: 13.03.2015 09:030 плюса
BufferedReader br = new BufferedReader( new InputStreamReader(conn.getInputStream(), "UTF-8"));
Автор: slodeveloper Размещён: 14.03.2015 10:29Вопросы из категории :
- java В чем разница между int и Integer в Java и C #?
- java Как я могу определить IP моего маршрутизатора / шлюза в Java?
- java Каков наилучший способ проверки XML-файла по сравнению с XSD-файлом?
- java Как округлить результат целочисленного деления?
- swing Java Swing: отображение изображений внутри Jar
- swing Как реализовать перетаскиваемые вкладки с помощью Java Swing?
- swing Можно ли отобразить компоненты Swing в JSP?
- swing Как добавить ActionListener в JButton в Java
- netbeans FTP в NetBeans 6.1
- netbeans Как улучшить производительность Netbeans?
- netbeans Может ли менеджер по верстке создать несколько JPanels?
- netbeans Netbeans имеет «Печать в HTML» - есть ли способ добавить эту функцию в Eclipse?
- unicode Regex и unicode
- unicode Как определить, имеет ли конкретная строка символы Юникода (особенно двойные байтовые символы)
- unicode Java, API веб-службы Alfresco и Unicode NamedValues
- unicode How to use Special Chars in Java/Eclipse
- fonts Как определить, какой из указанных шрифтов был использован на веб-странице?
- fonts Проверьте, установлен ли шрифт
- fonts Применение общей схемы шрифта к нескольким объектам в wxPython
- fonts Веб-страницы и шрифты штрих-кода