3

テキスト ファイルを読み取り、Unicode 文字の概要を表示するプログラムを作成するように依頼されました。これを行っているときに、コンソールに疑問符として出力されているように見えるいくつかの Unicode 文字で問題が発生しました。ただし、Swing を使用して同じ Unicode テキストを出力すると、疑問符ではなくなります。

    System.out.println("\u0126"); // appears to be ? in my console.

    JOptionPane.showMessageDialog(null,"\u0126"); // seems to display the character successfuly

GUIを使うので放置でいいのですが、私のような初心者にもわかるような説明が欲しいです。

一部の Unicode 文字がコンソールでは疑問符のように表示されるのに、Swing では正しく表示されないのはなぜですか? (Eclipse、NetBeans、JCreator、JGrasp は同じことを行います。IDE の問題だと思いました)。

エンコーディングまたはフォントの問題ですか?また、今後疑問符の問題が発生することなく、コンソールに Unicode テキストを正常に表示するにはどうすればよいですか?

4

3 に答える 3

1

Javaに絵文字とファンシーなUnicode文字を挿入するには?

自分で解決策を見つけるのにしばらく時間がかかりました。それが役立つことを願っています:)

  // To run emojis in Java make sure you have installed Windows Terminal
  // and in order to insert them, remember Java char only support 16-bit 
  // characters meanwhile a great part of Unicode is 32-bit sooooo
  // every single Unicode character must be break into two 16-bit 
  // character, these are called surrogates.


  // How to create surrogates and use emojis in Java! :D
  //1. Copy Uni code you want to use (Ommit U+, that's only a label)
  //   https://unicode-table.com/en/1F680/
  //2. Paste in here and copy both surrogates
  //   http://www.russellcottrell.com/greek/utilities/SurrogatePairCalculator.htm
  //3. Code it as Strings and separate it by \u symbol in Java.
  //4. Compile it and run it in Windows Terminal.
  //   #HappyCoding!!


  // One small thing: For some reason you need to put two spaces after 
  // every two surrogates, otherwise it'll print two question marks next
  // to it.

  // Other small thing: Try to use emojis at the end of a line because for 
  // some reason if you insert String after the Unicode it'll do strange 
  // things.


  String rocket = "Hello Entrepeneurs \uD83D\uDE80  ";
  System.out.print(rocket);
于 2020-01-21T16:47:02.467 に答える