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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥100 Jenkins自动化部署—悬赏100元
    • ¥15 关于#python#的问题:求帮写python代码
    • ¥20 MATLAB画图图形出现上下震荡的线条
    • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
    • ¥15 perl MISA分析p3_in脚本出错
    • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
    • ¥15 ubuntu虚拟机打包apk错误
    • ¥199 rust编程架构设计的方案 有偿
    • ¥15 回答4f系统的像差计算
    • ¥15 java如何提取出pdf里的文字?