[问题]再提java中文问题!!

软件和网站开发以及相关技术探讨
头像
bird
帖子: 765
注册时间: 2006-09-04 10:45
联系:

[问题]再提java中文问题!!

#1

帖子 bird » 2007-08-13 16:13

我的JDK不是用APT安装的,用的是SUN官方网站上的bin包来安装的;

JDK是1.5.0_12版本;

安装到了/opt下面,

出现SWING不支持中文的问题,按照wiki(http://wiki.ubuntu.org.cn/index.php?tit ... iant=zh-cn)上的提示:

其它方法

在java安装目录,如/opt/jdk1.5.0_11/jre/lib/fonts下,建立文件夹fallback,并拷贝一个中文字体进去,java就支持中文了,如在安装netbeans的时候,就不会出现乱码,而是显示中文了。
拷贝了windows下的黑体:simhei.ttf 文件到fallback目录下;

使用中发现,有些地方还是不能显示中文,比如:

在jPanel上用Graphics对象的drawString()方法时,还是不能正常显示中文,显示为方块!

ex:
graphics.drawString("娃哈哈",100,100);

这个代码运行出来的结果就是无法把中文draw出来!

但是,其他swing的一些方法,比如:

jframe.setTitle("刘德华");

这个运行出来就可以正确显示中文!

不知道是怎么回事,在windows下面一切正常,哈哈!

望各位大虾指点迷津 ^_^

-----------------------------------------------------

最后,附上我的一个小的测试程序,大家可以跑一跑:

代码: 全选

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

public class NotHelloWorld
{  
   public static void main(String[] args)
   {  
      NotHelloWorldFrame frame = new NotHelloWorldFrame();
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setVisible(true);
   }
}

class NotHelloWorldFrame extends JFrame
{
   public NotHelloWorldFrame()
   {
      setTitle("刘德华");
      setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

      NotHelloWorldPanel panel = new NotHelloWorldPanel();
      add(panel);
   }

   public static final int DEFAULT_WIDTH = 300;
   public static final int DEFAULT_HEIGHT = 200;  
}

class NotHelloWorldPanel extends JPanel
{  
   public void paintComponent(Graphics g)
   {  
      super.paintComponent(g);

      g.drawString("孙燕姿", MESSAGE_X, MESSAGE_Y);
   }

   public static final int MESSAGE_X = 75;
   public static final int MESSAGE_Y = 100;
}
头像
iridiumcao
帖子: 245
注册时间: 2007-01-18 18:05
来自: Shanghai
联系:

#2

帖子 iridiumcao » 2007-08-17 23:01

帮你测试了下,刘德华可以,孙燕姿不行。
我也是自己安装的jdk,暂时没有中文需要,这个问题也没有急着去解决了。。。 8)
Dell Inspiron 1501
Xubuntu&Lubuntu 11.10

Ade, Gnome3, Ade, Unity!

随心所欲不逾矩
头像
bird
帖子: 765
注册时间: 2006-09-04 10:45
联系:

#3

帖子 bird » 2007-08-20 9:19

有用ubuntu自带jdk的朋友吗?帮忙试验一下,看看是不是还是乱码。
头像
aitilang
帖子: 1026
注册时间: 2007-04-28 21:38

#4

帖子 aitilang » 2007-08-20 9:59

源里的jdk,都已显示。
thinkpad x61 2G DDR no cdrom
--------------------------------------------
ABS学习中
sed学习中
awk学习中
perl学习中
新手描述不清,老手猜测不到,胡乱指挥一通,后果难以预料
头像
toawinner47
帖子: 200
注册时间: 2006-11-08 20:46
联系:

#5

帖子 toawinner47 » 2007-08-20 20:18

乍错这么多呀:

代码: 全选

javac NotHelloWorld.java
----------
1. ERROR in NotHelloWorld.java (at line 1)
        mport javax.swing.*;
        ^^^^^
Syntax error on token "mport", import expected
----------
2. ERROR in NotHelloWorld.java (at line 9)
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                                       ^^^^^^
JFrame cannot be resolved
----------
3. ERROR in NotHelloWorld.java (at line 10)
        frame.setVisible(true);
              ^^^^^^^^^^
The method setVisible(boolean) is undefined for the type NotHelloWorldFrame
----------
4. ERROR in NotHelloWorld.java (at line 14)
        class NotHelloWorldFrame extends JFrame
                                         ^^^^^^
JFrame cannot be resolved to a type
----------
5. ERROR in NotHelloWorld.java (at line 18)
        setTitle("刘德华");
        ^^^^^^^^
The method setTitle(String) is undefined for the type NotHelloWorldFrame
----------
6. ERROR in NotHelloWorld.java (at line 19)
        setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
        ^^^^^^^
The method setSize(int, int) is undefined for the type NotHelloWorldFrame
----------
7. ERROR in NotHelloWorld.java (at line 22)
        add(panel);
        ^^^
The method add(NotHelloWorldPanel) is undefined for the type NotHelloWorldFrame
----------
8. ERROR in NotHelloWorld.java (at line 29)
        class NotHelloWorldPanel extends JPanel
                                         ^^^^^^
JPanel cannot be resolved to a type
----------
9. ERROR in NotHelloWorld.java (at line 33)
        super.paintComponent(g);
              ^^^^^^^^^^^^^^
The method paintComponent(Graphics) is undefined for the type Object
----------
9 problems (9 errors)
szwgh
帖子: 6
注册时间: 2006-12-15 16:41

#6

帖子 szwgh » 2007-08-21 23:10

我试着跑了一下你的程序,一切正常。
我设置的默认JDK是1.6的。
附件
Screenshot.png
Screenshot.png (3.74 KiB) 查看 3512 次
头像
bird
帖子: 765
注册时间: 2006-09-04 10:45
联系:

#7

帖子 bird » 2007-08-24 14:55

6楼请注意我的JDK版本:1.5.0_12 !!

因为我需要JDK 1.5 的环境,所以……
上次由 bird 在 2007-08-24 14:59,总共编辑 1 次。
头像
bird
帖子: 765
注册时间: 2006-09-04 10:45
联系:

#8

帖子 bird » 2007-08-24 14:58

5楼你第1行的import都没有复制完整,自己看错误提示!
1. ERROR in NotHelloWorld.java (at line 1)
mport javax.swing.*;
^^^^^
Syntax error on token "mport", import expected
----------
头像
magisu
帖子: 202
注册时间: 2005-12-31 21:23

#9

帖子 magisu » 2007-08-28 13:35

我用的是JDK1.6版本,一切正常。
老子很生气,后果不严重。
头像
liuhello
帖子: 216
注册时间: 2007-04-24 13:44

#10

帖子 liuhello » 2007-08-28 19:24

在java安装目录,如/opt/jdk1.5.0_11/jre/lib/fonts下,建立文件夹fallback,并拷贝一个中文字体进去,java就支持中文了,如在安装netbeans的时候,就不会出现乱码,而是显示中文了。
我是这样装的可以阿
附件
Screenshot-刘德华.png
Screenshot-刘德华.png (8.29 KiB) 查看 3370 次
头像
bird
帖子: 765
注册时间: 2006-09-04 10:45
联系:

#11

帖子 bird » 2007-09-04 17:13

10楼的,无语了,方便透露下你的jdk是多少版本呢? 是sun的jdk吗?
大槛哥
帖子: 54
注册时间: 2006-04-07 13:00

#12

帖子 大槛哥 » 2007-09-17 11:11

源里的jdk是可以的。。。。
我下载的是java_ee_sdk-5_02-linux,也有中文问题,但cp了字体后就好了。。。
头像
leemingjun
帖子: 109
注册时间: 2007-05-17 17:26

Re: [问题]再提java中文问题!!

#13

帖子 leemingjun » 2007-10-20 19:57

头像
AutumnHealth
帖子: 776
注册时间: 2006-12-20 18:41

#14

帖子 AutumnHealth » 2007-10-20 22:15

ls这个版就有http://forum.ubuntu.org.cn/viewtopic.php?t=76414
不过blog上还有一些补充 :D
尽情享受生活~

Welcome to AutumnHealth's blog:
http://forum.ubuntu.org.cn/weblog.php?w=1421
http://blog.csdn.net/autumnhealth
头像
mmxcq
帖子: 214
注册时间: 2007-07-28 14:04
联系:

#15

帖子 mmxcq » 2007-10-28 10:26

我也是一样的阿[/img]
附件
Screenshot-1.png
回复