源码:
import java.awt.event.*;
class Calculate extends Frame implements ActionListener {
TextField t1 = new TextField(5); //第一个操作数文本框
TextField t2 = new TextField(5); //运算符文本框
TextField t3 = new TextField(5); //第二个操作数文本框
TextField t4 = new TextField(5); //结果文本框
Lable L1 = new Lable("=");
Button btn = new Button("计算"); public Calculate() {
setLayout(new FlowLayout());
add(t1);add(t2);add(t3);
add(L1);add(t4);add(btn);

btn.addActionListenter(this); //注册动作事件监听者为当前对象
addWindowListener(new WindowAdapter() { //关闭窗口事件
public void windowClosing(WindowEvent e) {
dispose();//释放窗口
System.exit(0);//退出程序
}
});
}

public void actionPerformed(ActionEvent e) {
float x,y; //操作数变量
double result = 0; //结果变量
String op;
try { //异常捕获机制
x = Float.parseFloat(t1.getText()); //将字符串数据转换成浮点数据
y = Float.parseFloat(t3.getText());
op = t2.getText();
if(op.equals("+")) //运算符为"+"
result = x+y;
if(op.equals("-")) //运算符为"-"
result = x-y;
else if(op.equals("*")) //运算符为"*"
result = x*y;
else if(op.equals("/")) //运算符为"/"
result = x/y;
t4.setText(Double.toString(result));
}catch(Exception ee)
{t4.setText("数据错误"); } //捕获异常,数据错误时,显示信息
}
}
public static void main(String [] args) {
Calculate mainFrame = new Calculate();
mainFrame.setSize(400,400);
mainFrame.setTitle("两个数的计算程序");
mainFrame.setVisible(true);
}
编译时:F:\JavaExercise>javac Calculate.java
Calculate.java:49: 进行语法解析时已到达文件结尾
                        }
我知道是差一个},但是加在那儿呢?

解决方案 »

  1.   

    package com.xuz.csdn.june16;import java.awt.Button;
    import java.awt.FlowLayout;
    import java.awt.Frame;
    import java.awt.TextField;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;class Calculate extends Frame implements ActionListener {
    TextField t1 = new TextField(5); // 第一个操作数文本框
    TextField t2 = new TextField(5); // 运算符文本框
    TextField t3 = new TextField(5); // 第二个操作数文本框
    TextField t4 = new TextField(5); // 结果文本框
    Lable L1 = new Lable("=");
    Button btn = new Button("计算"); public Calculate() {
    setLayout(new FlowLayout());
    add(t1);
    add(t2);
    add(t3);
    add(L1);
    add(t4);
    add(btn); btn.addActionListenter(this); // 注册动作事件监听者为当前对象
    addWindowListener(new WindowAdapter() { // 关闭窗口事件
    public void windowClosing(WindowEvent e) {
    dispose();// 释放窗口
    System.exit(0);// 退出程序
    }
    });
    } public void actionPerformed(ActionEvent e) {
    float x, y; // 操作数变量
    double result = 0; // 结果变量
    String op;
    try { // 异常捕获机制
    x = Float.parseFloat(t1.getText()); // 将字符串数据转换成浮点数据
    y = Float.parseFloat(t3.getText());
    op = t2.getText();
    if (op.equals("+")) // 运算符为"+"
    result = x + y;
    if (op.equals("-")) // 运算符为"-"
    result = x - y;
    else if (op.equals("*")) // 运算符为"*"
    result = x * y;
    else if (op.equals("/")) // 运算符为"/"
    result = x / y;
    t4.setText(Double.toString(result));
    } catch (Exception ee) {
    t4.setText("数据错误");
    } // 捕获异常,数据错误时,显示信息
    } public static void main(String[] args) {
    Calculate mainFrame = new Calculate();
    mainFrame.setSize(400, 400);
    mainFrame.setTitle("两个数的计算程序");
    mainFrame.setVisible(true);
    }
    }
      

  2.   

    大哥,您有IDE么? 用记事本编程哈?
      

  3.   

    Label都写错了。。你这个程序有很多问题
    你看到没main方法没有包括在类里面。。在最后面加个"}",,把main方法上面那个"}"删除了
      

  4.   

    就算用文本编辑器,也要找个支持代码高亮、括号配对等功能的、比如自由开源的SciTE、收费的EditPlus等之类的。
      

  5.   

    一大推错误- -!import java.awt.Button;
    import java.awt.FlowLayout;
    import java.awt.Frame;
    import java.awt.TextField;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import javax.swing.JLabel;
    class Calculate extends Frame implements ActionListener {
    TextField t1 = new TextField(5); // 第一个操作数文本框
    TextField t2 = new TextField(5); // 运算符文本框
    TextField t3 = new TextField(5); // 第二个操作数文本框
    TextField t4 = new TextField(5); // 结果文本框
    JLabel L1 = new JLabel("=");
    Button btn = new Button("计算"); public Calculate() {
    setLayout(new FlowLayout());
    add(t1);
    add(t2);
    add(t3);
    add(L1);
    add(t4);
    add(btn); btn.addActionListener(this); // 注册动作事件监听者为当前对象
    addWindowListener(new WindowAdapter() { // 关闭窗口事件
    public void windowClosing(WindowEvent e) {
    dispose();// 释放窗口
    System.exit(0);// 退出程序
    }
    });
    } public void actionPerformed(ActionEvent e) {
    float x, y; // 操作数变量
    double result = 0; // 结果变量
    String op;
    try { // 异常捕获机制
    x = Float.parseFloat(t1.getText()); // 将字符串数据转换成浮点数据
    y = Float.parseFloat(t3.getText());
    op = t2.getText();
    if (op.equals("+")) // 运算符为"+"
    result = x + y;
    if (op.equals("-")) // 运算符为"-"
    result = x - y;
    else if (op.equals("*")) // 运算符为"*"
    result = x * y;
    else if (op.equals("/")) // 运算符为"/"
    result = x / y;
    t4.setText(Double.toString(result));
    } catch (Exception ee) {
    t4.setText("数据错误");
    } // 捕获异常,数据错误时,显示信息
    } public static void main(String[] args) {
    Calculate mainFrame = new Calculate();
    mainFrame.setSize(400, 400);
    mainFrame.setTitle("两个数的计算程序");
    mainFrame.setVisible(true);
    }
    }