问题描述
- 程序是对的,但是在学生用的版本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);
}
});
}
}