一个计算器程序 希望大虾帮小弟挑下错 编译没错 运行没异常
运行时 ArrayList里的数字都打印出来了 还有size()属性 可是就是计算不出结果来
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class TestCalc{
JFrame frame;//窗体
JPanel panel;//下面的大panel
JPanel shangmian;
JPanel xiamian;
JButton[] button;
JButton back;
JButton ce;
JTextField jtf;
boolean flag;
int num;
char oper;
public TestCalc(){
frame = new JFrame("计算器");
jtf = new JTextField();
jtf.setText(0.0+"");
panel = new JPanel();
shangmian = new JPanel();
back = new JButton("Back");
back.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
jtf.setText(0.0+"");
ce = new JButton("CE");
shangmian.add(back);
shangmian.add(ce);
xiamian = new JPanel();//FlowLayout
xiamian.setLayout(new GridLayout(4,4));
button = new JButton[16];
String str = "7,8,9,/,4,5,6,*,1,2,3,-,0,+/-,+,=";
String[] texts = str.split(",");
NumberListener nl = new NumberListener();
OperListener ol = new OperListener();
for(int i=0;ibutton.length;i++){
button[i] = new JButton(texts[i]);
xiamian.add(button[i]);
ActionListener al = ol;
if(Character.isDigit(texts[i].charAt(0)))
al = nl;
button[i].addActionListener(al);
panel.add(shangmian,"North");
panel.add(xiamian);
frame.add(panel);
frame.add(jtf,"North");
frame.setVisible(true);
frame.setSize(208,220);
frame.setDefaultCloseOperation(3);
public static void main(String[]args){
new TestCalc();
double db;
class NumberListener implements ActionListener{
public void actionPerformed(ActionEvent ae){
System.out.println("这是个数字:"+ae.getActionCommand());
String num = ae.getActionCommand();
db = Double.parseDouble(num);
jtf.setText(db+"");
class OperListener implements ActionListener{
ArrayListDouble al = new ArrayListDouble();
public void actionPerformed(ActionEvent ae){
OperListener ol2 = new OperListener();
String str = ae.getActionCommand();
Character ch = str.charAt(0);
System.out.println("这是个运算符:"+str);
//for(int i =0;i2;i++){
// String jt = jtf.getText();
al.add(db);
ol2.jisuan(ch);
System.out.println(al);
// }//for
}//actionperformed
int total;
public void jisuan(char ch){
OperListener ol3 = new OperListener();
if(al.size()1){
double dd1 = al.get(total);
System.out.println("我被执行了");
double dd2 = al.get(total+1);
switch(ch){
case ’+’:{double dd3 =dd1+dd2;ol3.jieguo(dd3,ch);}break;
case ’-’:{double dd3 = dd1-dd2;ol3.jieguo(dd3,ch);}break;
case ’*’:{double dd3 = dd1*dd2;ol3.jieguo(dd3,ch);}break;
case ’/’:{double dd3 = dd1/dd2;ol3.jieguo(dd3,ch);}break;
}}//switch
}//jisuan
public void jieguo(double dd3,Character ch){
if(ch.equals(’=’)){
jtf.setText(dd3+"");
}//if
}//jieguo
}//内部类