简体   繁体   中英

How to set Unicode characters are not readable?

I opened a file using JTextArea and replace text into some of the Unicode characters but those characters are readable. Problem is how to set those characters are not readable, copy and paste and when selected the text that symbols are not appeared?

   String previous=textArea.getText();
    if(button.isSelected()){
        previous=previous.replaceAll(" ","\u00b7");
        previous=previous.replaceAll("\t","\u00BB\t");
        textArea.setText(previous.replaceAll("\n", "\u00B6\n"));
    }

Use Font.canDisplayUpTo(String) to check that the font of the text area can display all the characters in the given String .

Here is an example of usage:

在此处输入图片说明

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

public class UnicodeCharacterTest {

    private JComponent ui = null;
    String s = new String(
            new String(Character.toChars(182)) + " "
            + new String(Character.toChars(183)) + " "
            + new String(Character.toChars(186)));

    UnicodeCharacterTest() {
        initUI();
    }

    public void initUI() {
        if (ui != null) {
            return;
        }

        ui = new JPanel(new GridLayout(0, 2));
        ui.setBorder(new EmptyBorder(4, 4, 4, 4));

        GraphicsEnvironment ge
                = GraphicsEnvironment.getLocalGraphicsEnvironment();
        Font[] fonts = ge.getAllFonts();
        for (Font font : fonts) {
            if (font.canDisplayUpTo(s)<0) {
                ui.add(new JLabel(font.getName()));
                JLabel output = new JLabel(s);
                output.setFont(font.deriveFont(24f));
                ui.add(output);
            }
        }
    }

    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) {
                }
                UnicodeCharacterTest o = new UnicodeCharacterTest();

                JFrame f = new JFrame("Font/Unicode test");
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                f.setLocationByPlatform(true);

                f.setContentPane(new JScrollPane(
                        o.getUI(), 
                        JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, 
                        JScrollPane.HORIZONTAL_SCROLLBAR_NEVER));
                f.pack();
                f.setSize(new Dimension(f.getSize().width,200));
                f.setMinimumSize(f.getSize());

                f.setVisible(true);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM