//使用CustomButton 类的Averageing Applet
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
//<applet code=AverageApplet.class width=400 height=300></applet>
public class AverageApplet extends Applet implements ActionListener
{
public CustomAverage average;
public CustomButton b1;
public CustomTextField input1,input2,display;
public CustomLabel labelTitle,label1input,label2input,label3display;
public Color newColor;
public Font newFont;
public int num1,num2;
public void init()
{
Panel p1;
newColor=new Color(125,0,255);
newFont=new Font("Helvetica",Font.BOLD+Font.ITALIC,24);
average=new CustomAverage();
b1=new CustomButton("Push for Average",newColor,Color.yellow,newFont);
input1=new CustomTextField(12,"0",newColor,Color.yellow,newFont,ture);
input2=new CustomTextField(12,"0",newColor,Color.lightGray,newFont,ture);
display=new CustomTextField(12,"",newColor,Color.red,newFont,false);
LabelTitle=new CustomLabel("Average Calculator",newColor,Color.pink,newFont);
Label1input=new CustomLabel("First Number",newColor,Color.white,newFont);
Label2input=new CustomLabel("Second Number",newColor,Color.orange,newFont);
Label3display=new CustomLabel("Computed Average",newColor,Color.gray,newFont);
p1=new Panel(new GridLayout(8,1,10,10));
b1.addActionListener(this);
p1.add(labelTitle);p1.add(label1input);p1.add(input1);
p1.add(label2input);p1.add(input2);
p1.add(b1);p1.add(label3display);p1.add(display);add(p1);
}
public void actionPerformed(ActionEvent e)
{
display.setText(""+average.getAverage(Integer.parseInt(input1.getText()),
Integer.parseInt(input2.getText())));
}
/*
public void actionPerformed(ActionEvent e)
{
int num1,num2;
double avg;
num1=Integer.parseInt(input1.getText());
num2=Integer.parseInt(input2.getText());
avg=average.getAverage(num1,num2);
display.setText(""+avg);
}
*/
}
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
//<applet code=CustomLabel.class width=400 height=300></applet>
public class CustomLabel extends Label
{
private Color myBackgroundColor,myForegroundColor;
private String myLabel;
private Font myFont;
public CustomLabel(String str,Color fgColor,Color bgColor,Font ft)
{
super(str);
myLabel=str;
myBackgroundColor=bgColor;
myForegroundColor=fgColor;
myFont=ft;
setBackground(myBackgroundColor);
setForeground(myForegroundColor);
setFont(ft);
}
}
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
//<applet code=CustomTextField.class width=400 height=300></applet>
public class CustomTextField extends TextField
{
private Color myBackgroundColor,myForegroundColor;
private String myLabel;
private Font myFont;
private int mySize;
private boolean myCanEdit;
public CustomTextField(int size,String str,Color fgColor,Color bgColor,Font ft,boolean canEdit)
{
super(size);
myLabel=str;
myBackgroundColor=bgColor;
myForegroundColor=fgColor;
myFont=ft;
mySize=size;
myCanEdit=canEdit;
setBackground(myBackgroundColor);
setForeground(myForegroundColor);
setFont(ft);
setText(str);
setEditable(myCanEdit);
}
}