|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?注册
x
发信人: zshjc (与狼共舞), 信区: Java
标 题: 关于superMMX中文显示说明的一点体会
发信站: BBS 水木清华站 (Sat Aug 4 13:59:30 2001)
要想显示中文字体,有时并非系统中有这种字体,同时java也支持这种字体就可以显示
.
需要显式地调用GraphicsEnvironment类的getAvailableFontFamilyName方法。
如:String[] fontNames = GraphicsEnvironment.getLocalGraphicsEnvironment().ge
tAvaila
lbeFontFamilyNames();
下面是我编写的一个例子,希望对初学者有点帮助
- ----------------------------
- __________________________________________
- /**
- * ChineseTest.java
- * July 25, 2001
- *
- * @author zshjc
- */
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- public class ChineseTest extends JFrame
- {
- private JPanel up;
- private JLabel label;
- private JButton btn;
- private Font f1,f2;
- public static void main(String[] args)
- {
- ChineseTest frame = new ChineseTest();
- frame.addWindowListener
- (
- new WindowAdapter()
- {
- public void windowClosing(WindowEvent e)
- {
- System.exit(0);
- }
- }
- );
- frame.show();
- }
- public ChineseTest()
- {
- setTitle("Chinese Test");
- setSize(400, 300);
- Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
- setLocation(screenSize.width/2 - 200,screenSize.height/2 - 150);
- GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnviro
- nment();
- ge.getAvailableFontFamilyNames();
- getContentPane().setLayout(new BorderLayout());
- up = new JPanel();
- f1 = new Font("STXingkai",Font.PLAIN,20);
- f2 = new Font("FangSong_GB2312",Font.PLAIN,20);
- label = new JLabel("中文测试");
- label.setFont(f1);
- label.setForeground(Color.red);
- up.add(label);
- btn = new JButton("按钮");
- btn.setFont(f2);
- btn.setForeground(Color.blue);
- up.add(btn);
- getContentPane().add(up, BorderLayout.NORTH);
- getContentPane().add(new JPanel()
- {
- public void paintComponent(Graphics g)
- {
- super.paintComponent(g);
- String s = "现在大家满足了吧!!!";
- Font f=new Font("STXinwei",Font.BOLD,25);
- FontMetrics fm = g.getFontMetrics(f);
- Dimension d = getSize();
- int cx = (d.width-fm.stringWidth(s)) / 2;
- int cy = (d.height-fm.getHeight()) / 2;
- g.setFont(f);
- g.drawString(s, cx, cy);
- }
- }, BorderLayout.CENTER);
- }
- }?
复制代码
--
※ 来源:·BBS 水木清华站 smth.org·[FROM: 166.111.47.29] |
|