该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
package txt2;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.*;
public class MyPanel extends Frame implements ActionListener{
TextField tf1;
TextField tf2;
Button button1;
Button button2;
Label lb1;
Label lb2;
public MyPanel(){
GridLayout gl1=new GridLayout(3, 2);
this.setLayout(gl1);
lb1=new Label("摄氏温度(C)");
lb2=new Label("华氏温度(F)");
button1=new Button("转华式(F)");
button2=new Button("转摄氏(C)");
tf1=new TextField(10);
tf2=new TextField(10);
this.add(lb1);
this.add(tf1);
this.add(lb2);
this.add(tf2);
this.add(button1);
this.add(button2);
this.setSize(250, 150);
this.setVisible(true);
button1.addActionListener(this);
button2.addActionListener(this);
tf1.setBounds(10, 30, 100, 25);
tf2.setBounds(130,30, 100, 25);
lb1.setBounds(10, 58, 100, 25);
lb2.setBounds(130, 58, 100, 25);
button1.setBounds(10, 88, 100, 25);
button2.setBounds(130, 88, 100, 25);
}
public static void main(String[] args) {
MyPanel mp=new MyPanel();
mp.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==button2){
double sum=Double.parseDouble(tf2.getText());
double answer=(sum-32)*5/9;
tf1.setText(Double.toString(answer));
}
if(e.getSource()==button1){
double sum=Double.parseDouble(tf1.getText());
double answer=sum*9/5+32;
tf2.setText(Double.toString(answer));
}
}
}