- 论坛徽章:
- 0
|
import java.awt.event.*;
import java.awt.*;
public class FontPick extends Frame{
public FontPick(){
this.setTitle("font metrics test");
// this.setSize(WIDTH, HEIGHT);
this.add(listPane, "North");
// metricsPanel.setSize(400, 200);
this.add(metricsPanel, "Center");
this.pack();
this.addWindowListener(
new WindowAdapter(){
public void windowClosing(WindowEvent e){
FontPick.this.setVisible(false);
System.exit(0);
}
}
);
}
public static void main(String[] args){
FontPick a = new FontPick();
a.setVisible(true);
}
public static int WIDTH = 400;
public static int HEIGHT = 400;
private FontListPanel listPane = new FontListPanel();
private MetricsPanel metricsPanel = new MetricsPanel();
}
class FontListPanel extends Panel{
FontListPanel(){
String[] fonts = this.getToolkit().getFontList();
for(String i : fonts){
font.add(i);
}
String[] styles = new String[]{"Plain", "Italic", "Bold", "BoldItlic"};
for(String i : styles){
style.add(i);
}
String[] sizes = {"12", "16", "24", "30", "48"};
for(String i : sizes){
size.add(i);
}
this.add(font);
this.add(style);
this.add(size);
font.addItemListener(listener);
size.addItemListener(listener); //应在此处添加 [color="#ff0000"]size.select(int);
style.addItemListener(listener);
}
class Listener implements ItemListener{
public void itemStateChanged(ItemEvent e){
Font fontValue = new Font(font.getSelectedItem(), style.getSelectedIndex(),Integer.parseInt(size.getSelectedItem()));
// metricspane.updatePanel(fontValue);
if (fontValue != null)
System.out.println(fontValue);
// metricspane.repaint();
}
}
private List font = new List(6);
private List style = new List(6);
private List size = new List(6);
private MetricsPanel metricspane = new MetricsPanel();;
private Listener listener = new Listener();
}
class MetricsPanel extends Panel{
public void paint(Graphics g){
// Dimension size = this.getSize();
// metrics = this.getFontMetrics(font);
// Point p = new Point();
g.drawString(text,100, 100);
}
// public void updatePanel(Font font){
// this.font = font;
// this.repaint();
//
// }
// public void setText(Font f){
// StringBuffer buffer ;
// f.getFamily();
// int i = f.getStyle();
// switch(i){
// case Font.PLAIN: i = " Plain "; break;
// case Font.BOLD: i = " Bold "; break;
// case Font.ITALIC: i = " Italic "; break;
// case Font.BOLD + Font.ITALIC:
// i = " Bold Italic ";
// break;
//
// }
// }
Font font;
FontMetrics metrics;
String text = " i hite you";
}
------------------------------------
问题出现在添加事件中List.add(Listener)上面,运行时选择List选项时会报“AWT-EventQueue-0”java.lang.NumberFormatException:null的错误。暂不知如何解决。问题是在了Font(String,int,int)类的转化上面,其中最后一个参数是因为有Integer.parseInt(或Integer.valueOf())从而引发了NumberFormatExcepiton异常。现在不知如何解决这个展常。
解决办法: 原因是因为在List size初始化时默认选定值,从而引起第Integer.parseInt(或Integer.valueOf())时引用了null值,所以抛出异常。解决时应在 List size赋完值后,调用List.select(int)方法来确定新值。运行即可正常。
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/4709/showart_45273.html |
|