萍聚社区-德国热线-德国实用信息网

 找回密码
 注册

微信登录

微信扫一扫,快速登录

萍聚头条

查看: 1027|回复: 0

1-1-16-4-4 关于superMMX中文显示说明的一点体会

[复制链接]
发表于 2003-5-11 23:04 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?注册 微信登录

x
发信人: zshjc (与狼共舞), 信区: Java
标  题: 关于superMMX中文显示说明的一点体会
发信站: BBS 水木清华站 (Sat Aug  4 13:59:30 2001)

要想显示中文字体,有时并非系统中有这种字体,同时java也支持这种字体就可以显示
.
需要显式地调用GraphicsEnvironment类的getAvailableFontFamilyName方法。
如:String[] fontNames = GraphicsEnvironment.getLocalGraphicsEnvironment().ge
tAvaila
lbeFontFamilyNames();
下面是我编写的一个例子,希望对初学者有点帮助
  1. ----------------------------
  2. __________________________________________
  3. /**
  4.   *  ChineseTest.java
  5.   *  July 25, 2001
  6.   *
  7.   *  @author  zshjc
  8.   */
  9. import java.awt.*;
  10. import java.awt.event.*;
  11. import javax.swing.*;
  12. public class ChineseTest extends JFrame
  13. {
  14.     private JPanel up;
  15.     private JLabel label;
  16.     private JButton btn;
  17.     private Font f1,f2;
  18.     public static void main(String[] args)
  19.     {
  20.        ChineseTest frame = new ChineseTest();
  21.        frame.addWindowListener
  22.        (
  23.            new WindowAdapter()
  24.            {
  25.               public void windowClosing(WindowEvent e)
  26.                {
  27.                  System.exit(0);
  28.               }
  29.            }
  30.        );
  31.          frame.show();
  32.     }
  33.     public ChineseTest()
  34.     {
  35.        setTitle("Chinese Test");
  36.          setSize(400, 300);
  37.          Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

  38.          setLocation(screenSize.width/2 - 200,screenSize.height/2 - 150);
  39.          GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnviro
  40. nment();
  41.          ge.getAvailableFontFamilyNames();
  42.          getContentPane().setLayout(new BorderLayout());
  43.        up = new JPanel();
  44.          f1 = new Font("STXingkai",Font.PLAIN,20);
  45.          f2 = new Font("FangSong_GB2312",Font.PLAIN,20);
  46.          label = new JLabel("中文测试");
  47.          label.setFont(f1);
  48.          label.setForeground(Color.red);
  49.          up.add(label);
  50.          btn = new JButton("按钮");
  51.          btn.setFont(f2);
  52.          btn.setForeground(Color.blue);
  53.          up.add(btn);
  54.          getContentPane().add(up, BorderLayout.NORTH);
  55.          getContentPane().add(new JPanel()
  56.          {
  57.                public void paintComponent(Graphics g)
  58.            {
  59.              super.paintComponent(g);
  60.           String s = "现在大家满足了吧!!!";
  61.                      Font f=new Font("STXinwei",Font.BOLD,25);
  62.                      FontMetrics fm = g.getFontMetrics(f);
  63.              Dimension d = getSize();
  64.            int cx = (d.width-fm.stringWidth(s)) / 2;
  65.            int cy = (d.height-fm.getHeight()) / 2;
  66.               g.setFont(f);
  67.               g.drawString(s, cx, cy);
  68.          }
  69.          }, BorderLayout.CENTER);
  70.     }
  71. }?
复制代码

--

※ 来源:·BBS 水木清华站 smth.org·[FROM: 166.111.47.29]
Die von den Nutzern eingestellten Information und Meinungen sind nicht eigene Informationen und Meinungen der DOLC GmbH.
您需要登录后才可以回帖 登录 | 注册 微信登录

本版积分规则

手机版|Archiver|AGB|Impressum|Datenschutzerklärung|萍聚社区-德国热线-德国实用信息网

GMT+1, 2025-3-12 22:44 , Processed in 0.055439 second(s), 16 queries , MemCached On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表