/*
这个例子实现基本的算术运算
*/
//----
java小程序(5)
//--------------------------------------------
1. import java.applet.*;
2. import java.awt.*;
3. import java.awt.event.*;
4.
5. public class Arithmetic extends Applet implements ActionListener
6.{
7.
Label prompt;
8.
TextField input1,input2;
9.
Button btn;
10.
int a=12,b=53;
11.
12.
public void init()
13. {
14.
prompt=new Label("Please input two integer numbers:");
15.
input1=new TextField(6);
16.
input2=new TextField(6);
17.
btn=new Button("Calculate");
18.
add(prompt);
19.
add(input1);
20.
add(input2);
21.
add(btn);
22. }
23.
public void paint(Graphics g)
24. {
25.
g.drawString(a+"+"+b+"="+(a+b),15,45);
26.
g.drawString(a+"-"+b+"="+(a-b),15,65);
27.
g.drawString(a+"*"+b+"="+(a*b),15,85);
28.
g.drawString(a+"/"+b+"="+(a/b),15,105);
29.
g.drawString(a+"%"+b+"="+(a%b),15,125);
30.
}
31. public void actionPerformed(ActionEvent e)
32. {
33. a=Integer.parseInt(input1.getText());
34. b=Integer.parseInt(input2.getText());
35. repaint();
36. }
37.}
//--------------------------------------------
//当用户单击按钮"Calculate"时,程序把输入框的字符串转化为整型数据赋给变量a,b,然后
//通过repaint()方法调用paint()方法。