huajia123456789 2013-12-26 05:06 采纳率: 50%
浏览 850

程序是对的,但是在学生用的版本jdk运行不出,怎样能运行出结果呢?

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

@SuppressWarnings("serial")
public class CalculatorTest extends JFrame implements ActionListener {
double N = 0;
double n = 0;
double x = 0;
double y = 0;
String s = "";
char ch = 0;
TextField txt = new TextField(50);
JButton btn[] = new JButton[10];
JButton btnSign = new JButton("+/-");
JButton btnDot = new JButton(".");
JButton btnAdd = new JButton("+");
JButton btnSub = new JButton("-");
JButton btnEnd = new JButton("=");
JButton btnMultiply = new JButton("×");
JButton btnDivide = new JButton("÷");
JButton btnClear = new JButton("C");
JButton btnBackSpace = new JButton("BackSpace");
JButton btnCE = new JButton("CE");

Panel panel1;
Panel panel2;
Panel panel3;

public CalculatorTest() {
super("计算器");
panel1 = new Panel();
panel2 = new Panel();
panel3 = new Panel();

setLayout(new BorderLayout());
add("North", panel1.add(txt));
txt.setText("0");

add("Center", panel2);
panel2.setLayout(new GridLayout(4, 3));
for (int i = 9; i >= 0; i--) {
    btn[i] = new JButton(String.valueOf(i));
    panel2.add(btn[i]);
    btn[i].addActionListener(this);
}
panel2.add(btnSign);
btnSign.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        String s;
        s = txt.getText();
        if (Double.parseDouble(txt.getText()) > 0) {
            s = "-" + s;
            txt.setText(s);
        } else if (s.equals("0")) {
            txt.setText("0");
        } else {
            N = Double.parseDouble(txt.getText());
            txt.setText(String.valueOf(N));
        }
    }
});
panel2.add(btnDot);
btnDot.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        s = "";
        s = txt.getText();
        s = s + ".";
        txt.setText(s);
    }
});

add("East", panel3);
panel3.setLayout(new GridLayout(4, 2));
panel3.add(btnAdd);
panel3.add(btnClear);
panel3.add(btnSub);
panel3.add(btnCE);
panel3.add(btnMultiply);
panel3.add(btnBackSpace);
panel3.add(btnDivide);
panel3.add(btnEnd);
btnClear.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        y = x = n = N = 0;
        txt.setText("0");
    }
});
btnCE.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        txt.setText("0");
    }
});
btnBackSpace.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        s = txt.getText();
        if (s.length() != 0) {
            s = s.substring(0, (s.length() - 1));
            txt.setText(s);
            if (s.length() <= 0)
                txt.setText("0");
        }
    }
});

btnAdd.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        y = Double.parseDouble(txt.getText());
        N = 0;
        n = 0;
        txt.setText("+");
        ch = '+';
    }
});
btnSub.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        y = Double.parseDouble(txt.getText());
        N = 0;
        n = 0;
        txt.setText("-");
        ch = '-';
    }
});
btnMultiply.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        y = Double.parseDouble(txt.getText());
        N = 0;
        n = 0;
        txt.setText("×");
        ch = '*';
    }
});
btnDivide.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        y = Double.parseDouble(txt.getText());
        N = 0;
        n = 0;
        txt.setText("÷");
        ch = '/';
    }
});
btnEnd.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        switch (ch) {
        case '+':
            x = Double.parseDouble(txt.getText());
            y = y + x;
            break;
        case '-':
            x = Double.parseDouble(txt.getText());
            y = y - x;
            break;
        case '*':
            x = Double.parseDouble(txt.getText());
            y = y * x;
            break;
        case '/':
            x = Double.parseDouble(txt.getText());
            y = y / x;
            break;
        }
        if (ch != 0)
            txt.setText(String.valueOf(y));
        ch = 0;
    }
});

setSize(400, 200);
setVisible(true);

}

public void actionPerformed(ActionEvent e) {
s = "";
s = txt.getText();
if (s.equals("0") || s.equals("+") || s.equals("-") || s.equals("×")
|| s.equals("÷"))
s = "";
s = s + ((JButton) e.getSource()).getLabel();
txt.setText(s);
}

public static void main(String args[]) throws Exception {
CalculatorTest cal = new CalculatorTest();
cal.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}

  • 写回答

0条回答

    报告相同问题?

    悬赏问题

    • ¥15 oracle集群安装出bug
    • ¥15 关于#python#的问题:自动化测试
    • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
    • ¥15 教务系统账号被盗号如何追溯设备
    • ¥20 delta降尺度方法,未来数据怎么降尺度
    • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
    • ¥15 再不同版本的系统上,TCP传输速度不一致
    • ¥15 高德地图点聚合中Marker的位置无法实时更新
    • ¥15 DIFY API Endpoint 问题。
    • ¥20 sub地址DHCP问题