📄 convertawt2.java
字号:
import java.awt.*;
import java.awt.event.*;
public class ConvertAWT2 extends Frame {
private Label fahrenheitLabel;
private TextField fahrenheitField;
private Label centigradeLabel;
private TextField centigradeField;
private Button fahrenheitButton;
private Button centigradeButton;
private Panel p1,p2;
// The instance of the following member class will be used as a listener
// for WindowEvent and ActionEvent
class GenericListener extends WindowAdapter implements ActionListener {
public void windowClosing (WindowEvent e) { System.exit(0); }
public void actionPerformed(ActionEvent e){
int fahrenheit, centigrade;
if (e.getSource()==fahrenheitButton) {
fahrenheit = getFahrenheit();
centigrade=(int) ( (fahrenheit - 32.0) * 5.0 / 9.0);
centigradeField.setText("" + centigrade);
} else {
centigrade=getCentigrade();
fahrenheit =(int) ( centigrade * 9.0 / 5.0 + 32.0);
fahrenheitField.setText("" + fahrenheit);
}
}
}
public ConvertAWT2() { // constructor
fahrenheitLabel = new Label("Degress Fahrenheit");
fahrenheitField = new TextField("32",6);
centigradeLabel = new Label("Degress Centigrade");
centigradeField = new TextField("0",6);
fahrenheitButton = new Button(">>>>>>");
centigradeButton = new Button("<<<<<<");
p1=new Panel(); p2=new Panel();
p1.setLayout(new GridLayout(2,2));
p2.setLayout(new GridLayout(1,2));
p1.add(fahrenheitLabel);
p1.add(centigradeLabel);
p1.add(fahrenheitField);
p1.add(centigradeField);
p2.add(fahrenheitButton);
p2.add(centigradeButton);
add("North", p1);
add("South",p2);
// register event listeners
GenericListener gl = new GenericListener();
fahrenheitButton.addActionListener(gl);
centigradeButton.addActionListener(gl);
addWindowListener(gl);
} // end constructor
public int getFahrenheit() {
String str = fahrenheitField.getText().trim();
int fahrenheit = (new Integer(str)).intValue();
return fahrenheit;
}
public int getCentigrade() {
String str = centigradeField.getText().trim();
int centigrade = (new Integer(str)).intValue();
return centigrade;
}
public static void main(String[] args) {
Frame frm = new ConvertAWT2();
frm.setSize(250,150);
frm.setVisible(true);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -