📄 weightconventer.java
字号:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class WeightConventer extends Applet implements ActionListener
{
WeightConvent weightInEarth=new WeightConvent();
Button b1,b2;
Label lab1,lab2;
TextField text1,text2;
public void init()
{
lab1=new Label("输入地球上的重量");
add(lab1);
text1=new TextField(20);
add(text1);
text1.addActionListener(this);
lab2=new Label("相应月球上的重量");
add(lab2);
text2=new TextField(20);
add(text2);
b1=new Button("确认");
add(b1);
b1.addActionListener(this);
b2=new Button("清除");
add(b2);
b2.addActionListener(this);
text2.setEditable(false);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b1||e.getSource()==text1)
{
try{
weightInEarth.setWeight(Double.valueOf(text1.getText()).doubleValue());
text2.setText(""+weightInEarth.toMoon());
}
catch(NumberFormatException event)
{
text2.setText("请输入数字字符");
}
}
else if(e.getSource()==b2)
{
text1.setText("0");
text2.setText("");
}
}
}
class WeightConvent{
private double exchangeRate=1.0/6;
private double weight;
public void setWeight(double weight){
this.weight=weight;
}
public double toMoon(){
return(weight*exchangeRate);
}
public double fromMoon(){
return(weight/exchangeRate);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -