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条回答

    报告相同问题?

    悬赏问题

    • ¥15 outlook无法配置成功
    • ¥15 Pwm双极模式H桥驱动控制电机
    • ¥30 这是哪个作者做的宝宝起名网站
    • ¥60 版本过低apk如何修改可以兼容新的安卓系统
    • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
    • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
    • ¥50 有数据,怎么用matlab求全要素生产率
    • ¥15 TI的insta-spin例程
    • ¥15 完成下列问题完成下列问题
    • ¥15 C#算法问题, 不知道怎么处理这个数据的转换