回复:(Sieben7)\'class\' or \'interface\' expected这...
其实这道问题是这样的:
编写java独立应用程序,接收人在地球上体重,输出他在月球上的体重。
(1) 月球上人的体重是在地球上体重的1/6。
(2) 定义可实例化类 ,以实现人在地球上的体重与在月球上体重的转换。
(3) 使用图形用户界面组件标签、文本框、按钮。
(4) 处理输入错误的情况。
我的代码如下:
import java.awt.event.*;
import java.awt.*;
public class WeightConApplication extends Frame implements ActionListener,WindowListener{
public Frame mainFrame;
public Button ConButton;
public TextField inputField;
public TextField messageField;
public Label inputLabel;
WeightConApplication()
{
setLayout(new FlowLayout());
inputField=new TextField(20);
inputLabel=new Label("Input weight");
ConButton=new Button("Convert");
add(ConButton);
add(inputField);
add(messageField);
add(inputLabel);
ConButton.addActionListener(this);
setBounds(100,100,150,150);
setVisible(true);
//mainFrame.setSize(250,125);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}
});
validate();
}
public static void main(String args[])
{
System.out.println("Starting WeightConApplication...");
WeightConApplication mainFrame = new WeightConApplication();
mainFrame.setSize(400, 400);
mainFrame.setTitle("WeightConApplication");
mainFrame.setVisible(true);
}
public void actionPerformed(ActionEvent exception)
{
try{
if(exception.getSource()==ConButton){
messageField.setText(Double.parseDouble(inputField.getText())/6);
}
}
catch(NumberFormatException e)
{
messageField.setText("非法字符");}
}
}
编译后有两个错误:
Z:\WeightConApplication.java:4: WeightConApplication is not abstract and does not override abstract method windowDeactivated(java.awt.event.WindowEvent) in java.awt.event.WindowListener
public class WeightConApplication extends Frame implements ActionListener,WindowListener{
^
Z:\WeightConApplication.java:55: cannot find symbol
symbol : method setText(double)
location: class java.awt.TextField
messageField.setText(Double.parseDouble(inputField.getText())/6);
^
2 errors
热心的斑斑或者路过的大牛们能够指点指点,我实在是没办法。java书上的例子很容易看懂,一到自己动手了就开始不行了。