Displaying urdu characters in JTextPane

老子叫甜甜 提交于 2019-12-10 08:51:49

问题


How can I display a single Urdu character in a JTextPane? I have translated English characters to Urdu characters. But I can't find any way to correctly display those characters into my text component.

My goal is to:

  1. Get the key pressed on the keyboard.
  2. Convert that key into the equivalent Urdu character.
  3. Display it in my text component (JTextPane).

I've completed step 1 and 2 but can't work out the last one.


回答1:


to 3- display it in my text component that is JTextPane

source Wikipedia

project Encoded in plain UTF-8

import javax.swing.*;
import java.awt.*;

public class Example {

    private JFrame frameA = new JFrame("Example");
    private JTextArea textA = new JTextArea(10, 5);

    public Example() {
        frameA.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        textA.setForeground(new Color(255, 150, 150));
        textA.setText("Peace be upon you (Hello) - السلام علیکم " + "\n");
        textA.append("Peace be upon you too (Hello) - و علیکم السلام " + "\n");
        textA.append("I am happy to meet you - آپ سے مل کر خوشی ہوئی" + "\n");
        textA.append("Do you speak English? - کیا آپ انگریزی بولتے ہیں؟" + "\n");

        frameA.add(textA);
        frameA.pack();
        frameA.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                Example exam = new Example();
            }
        });
    }
} 

EDIT:

thanks Stas

by mistake I put that to the JTextArea

added JTextPane example

import javax.swing.*;
import java.awt.*;

public class Example {

    private JFrame frameA = new JFrame("Example");
    private JTextPane textP = new JTextPane();

    public Example() {
        frameA.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        textP.setForeground(new Color(255, 150, 150));
        textP.setText("Peace be upon you (Hello) - السلام علیکم " + "\n"
        +"Peace be upon you too (Hello) - و علیکم السلام " + "\n"
        +"I am happy to meet you - آپ سے مل کر خوشی ہوئی" + "\n"
        +"Do you speak English? - کیا آپ انگریزی بولتے ہیں؟" + "\n");

        frameA.add(textP);
        frameA.pack();
        frameA.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                Example exam = new Example();
            }
        });
    }
} 


来源:https://stackoverflow.com/questions/8316923/displaying-urdu-characters-in-jtextpane

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!