import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class IntFieldTester extends JPanel implements ActionListener{
public static final int WIDTH=200,HEIGHT=300;
private JLabel prompt=new JLabel("Input an integer<=100:");
private IntField intField=new IntField(12,100);
private int userInt;
private String message="hello";
public IntFieldTester(){
add(prompt);
intField.addActionListener(this);
add(intField);
setSize(WIDTH,HEIGHT);
}
public void paintComponent(Graphics g){
g.setColor(getBackground());
g.fillRect(0,0,WIDTH,HEIGHT);
g.setColor(getForeground());
g.drawString(message,10,70);
}
public void actionPerformed(ActionEvent evt){
try{
  userInt=intField.getInt();
  message="you input"+userInt+"thank you";
  }catch(NumberFormatException e){
  JOptionPane.showMessageDialog(this,"the input must be an integer.please reenter.");
  }catch(IntOutOfRangeException e){
   JOptionPane.showMessageDialog(this,e.getMessage());
  }finally{
   repaint();
}
}
public static void main(String args[]){
JFrame f=new JFrame("inputfield tester");
IntFieldTester panel=new IntFieldTester();
f.getContentPane().add(panel);
f.setSize(panel.WIDTH,panel.HEIGHT);
f.setVisible(true);
f.addWindowListener(new WindowAdapter(){
   public void windowClosing(WindowEvent e){
  System.exit(0);
}
}
);
}
}
编译结果如下:
D:\Java\IOE>javac IntFieldTester.java
IntFieldTester.java:7: 找不到符号
符号: 类 IntField
位置: 类 IntFieldTester
private IntField intField=new IntField(12,100);
        ^
IntFieldTester.java:7: 找不到符号
符号: 类 IntField
位置: 类 IntFieldTester
private IntField intField=new IntField(12,100);
                              ^
IntFieldTester.java:28: 找不到符号
符号: 类 IntOutOfRangeException
位置: 类 IntFieldTester
  }catch(IntOutOfRangeException e){
         ^
3 错误