一个小应用程序,界面如下图所示,其功能为实现摄氏温度和华氏温度的相互转换。以下是程序的大部分,请编写其中的监视器方法。
注:华氏温度(F)=摄氏温度(C)×9/5+32
import java.applet.Applet;
import java.awt.Event;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JTextField;
public class Temperature extends Applet implements ActionListener
{
JTextField text1,text2;
JButton c2f,f2c;
public void init()
{
text1=new JTextField(5);
text2=new JTextField(5);
c2f=new JButton("摄氏转换为华氏");
f2c=new JButton("华氏转换为摄氏");
add(text1);
add(text2);
add(f2c);
add(c2f);
text1.requestFocus();
c2f.addActionListener(this);
f2c.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
double result;
if(e.getSource()==c2f)
{
result=Double.parseDouble(text1.getText())*9/5+32;
text2.setText(String.valueOf(result));
}
else if(e.getSource()==f2c)
{
result=(Double.parseDouble(text1.getText())-32)*5/9;
text2.setText(String.valueOf(result));
}
}
}