日期:2014-05-20  浏览次数:20634 次

我想编一个计算器,现在只是考虑加法,为什么点击“=”时,文本框不会出现结果?
import java.awt.*;
import javax.swing.*;
import javax.swing.border.LineBorder;
import javax.swing.text.BadLocationException;

import java.awt.event.*;

public class JSQ extends JFrame implements ActionListener{
JButton[] an={null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null};
JTextField wbk1,wbk2;
String firstnum,secomenum,firstfh,secomefh;
double s1,s2;
boolean sign,sign2;
public static void main(String[] args){
JSQ a1=new JSQ();
}
public JSQ(){

an[0]=new JButton("0");     an[1]=new JButton("1");
an[2]=new JButton("2");     an[3]=new JButton("3");
an[4]=new JButton("4");     an[5]=new JButton("5");
an[6]=new JButton("6");     an[7]=new JButton("7");
an[8]=new JButton("8");     an[9]=new JButton("9");
an[10]=new JButton("+");    an[11]=new JButton("-");
an[12]=new JButton("*");    an[13]=new JButton("÷");
an[14]=new JButton("xn");   an[15]=new JButton("n√");
an[16]=new JButton("(");    an[17]=new JButton(")");
an[18]=new JButton("DEL");  an[19]=new JButton("C");
an[20]=new JButton(".");    an[21]=new JButton("=");
wbk1=new JTextField(20);    wbk2=new JTextField(20);
firstnum=new String("");    secomenum=new String("");
firstfh=new String("");     secomefh=new String("");
sign=false;                 sign2=false;

for(int i=0;i<=21;i++){
an[i].setFont(new Font("宋体",Font.PLAIN,20));
an[i].setBorder(new LineBorder(Color.black));
}
this.setLayout(null);

wbk1.setSize(290,35);
wbk1.setLocation(20,30);
//wbq.setBorder(new LineBorder(Color.black));
wbk2.setSize(290,35);
wbk2.setLocation(20,64);
//wbk.setBorder(new LineBorder(Color.black));

for(int i=1;i<=20;i++){
an[i].setSize(65,45);
}
an[0].setSize(140,45);
an[21].setSize(65,100);

for(int i=16,n=20;i<=19;i++){
an[i].setLocation(n,130);
n+=75;
}
for(int i=12,n=20;i<=15;i++){
an[i].setLocation(n,185);
n+=75;
}
for(int i=7,n=20;i<=9;i++){
an[i].setLocation(n,240);
n+=75;
}
for(int i=4,n=20;i<=6;i++){
an[i].setLocation(n,295);
n+=75;
}
for(int i=1,n=20;i<=3;i++){
an[i].setLocation(n,350);
n+=75;
}
an[11].setLocation(245,240);
an[10].setLocation(245,295);
an[0].setLocation(20,405);
an[20].setLocation(170,405);
an[21].setLocation(245,350);

for(int i=0;i<=18;i++){
an[i].setForeground(Color.white);
an[i].setBackground(Color.gray);
}
an[19].setForeground(Color.red);
an[20].setForeground(Color.white);
an[21].setForeground(Color.white);
an[19].setBackground(Color.gray);
an[20].setBackground(Color.gray);
an[21].setBackground(Color.orange);

for(int i=0;i<=21;i++){
this.add(an[i]);
an[i].addActionListener(this);

}
an[0].setActionCommand("0");      an[1].setActionCommand("1");
an[2].setActionCommand("2");      an[3].setActionCommand("3");
an[4].setActionCommand("4");      an[5].setActionCommand("5");
an[6].setActionCommand("6");      an[7].setActionCommand("7");
an[8].setActionCommand("8");      an[9].setActionCommand("9");
an[10].setActionCommand("+");     an[11].setActionCommand("-");
an[12].setActionCommand("*");     an[13].setActionCommand("/");
an[14].setActionCommand("xn");    an[15].setActionCommand("n√");
an[16].setActionCommand("(");     an[17].setActionCommand(")");
an[18].setActionCommand("DEL");   an[19].setActionCommand("C");
an[20].setActionCommand(".");     an[21].setActionCommand("=");
this.add(wbk1);
this.add(wbk2);
this.setSize(335,500);
this.setLocation(500,200);
this.setTitle("计算器");
this.setIconImage((new ImageIcon("image/jsq.jpg")).getImage());
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}

public void actionPerformed(ActionEvent e){
for(int i=0;i<=9;i++){
if(e.getSource()==an[i]){
wbk1.setText(wbk1.getText()+Integer.toString(i));
String s=e.getActionCommand();
shuju(s);
}
}
if(e.getSource()==an[21]){
String s=e.getActionCommand();
fuhao(s);
Operator();
}
if(e.getSource()==an[10]){
sign=true;
wbk1.setText(wbk1.getText()+String.valueOf("+"));
// String s=e.getActionCommand();
// fuhao(s);
}//else if(e.getSource()==an[11]){