huajia123456789 2013-12-26 15:07 采纳率: 50%
浏览 897

大神帮帮忙《很急!程序在旧版软件下可以运行,但是怎样可以使它在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条回答

    报告相同问题?

    悬赏问题

    • ¥20 有关区间dp的问题求解
    • ¥15 多电路系统共用电源的串扰问题
    • ¥15 slam rangenet++配置
    • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
    • ¥15 对于相关问题的求解与代码
    • ¥15 ubuntu子系统密码忘记
    • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
    • ¥15 保护模式-系统加载-段寄存器
    • ¥15 电脑桌面设定一个区域禁止鼠标操作
    • ¥15 求NPF226060磁芯的详细资料