是制作一个简易计算器的代码。。我是这方面的新手。哪位高手帮我看下,谢谢了import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;public class Cal extends WindowAdapter implements ActionListener{
String s=""; //控制文本框
float k=0;
float p=0;
int c=0;
public void windowClosing(WindowEvent e){System.exit(0);} Frame f = new Frame();
Panel a=new Panel();//按键区域
Panel b=new Panel();//输出区域 TextField tf=new TextField("",22);//制作按钮 Button buttonAC=new Button("AC");
Button buttonsq=new Button("sqrt");
Button buttonC1=new Button("/");
Button buttonZF=new Button("+/-"); Button button7=new Button("7");
Button button8=new Button("8");
Button button9=new Button("9");
Button buttonC2=new Button("*"); Button button4=new Button("4");
Button button5=new Button("5");
Button button6=new Button("6");
Button buttonJ1=new Button("-"); Button button1=new Button("1");
Button button2=new Button("2");
Button button3=new Button("3");
Button buttonJ2=new Button("+"); Button button0=new Button("0");
Button buttonP=new Button(".");
Button buttonF=new Button("1/x");
Button buttonD=new Button("="); public void mouseEntered(MouseEvent e){}
  public void mouseExited(MouseEvent e){}
  public void mousePressed(MouseEvent e){}
  public void mouseReleased(MouseEvent e){}
public Cal(){
f.add(b,"North");
f.add(a,"Center"); f.setTitle("计算器");
f.setSize(200,200);
f.setBackground(Color.yellow);
f.addWindowListener(new Cal());
a.setSize(200,150);
b.setSize(100,200); a.setLayout(new GridLayout(5,4,3,3));
a.setVisible(true);
b.setVisible(true);//添加按钮

a.add(buttonAC);
a.add(buttonsq);
a.add(buttonC1);
a.add(buttonZF); a.add(button7);
a.add(button8);
a.add(button9);
a.add(buttonC2); a.add(button4);
a.add(button5);
a.add(button6);
a.add(buttonJ1); a.add(button1);
a.add(button2);
a.add(button3);
a.add(buttonJ2);
a.add(button0);
a.add(buttonP);
a.add(buttonF);
a.add(buttonD); b.add(tf);
//添加事件
   buttonAC.addActionListener(this);
   buttonsq.addActionListener(this);
  buttonC1.addActionListener(this);
   buttonZF.addActionListener(this); button7.addActionListener(this);
   button8.addActionListener(this);
   button9.addActionListener(this);
   buttonC2.addActionListener(this);   button4.addActionListener(this);
   button5.addActionListener(this);
   button6.addActionListener(this);
   buttonJ2.addActionListener(this);   button0.addActionListener(this);
   buttonP.addActionListener(this);
   buttonF.addActionListener(this);
   buttonD.addActionListener(this);
}//main函数
public static void main(String[] args){
new Cal();
}//输出
public void actionPerformed(ActionEvent e){
if(e.getActionCommand()=="0"){
if(c!=5){
s=s+"0";
tf.setText(s);
}
}
if(e.getActionCommand()=="1"){
if(c!=5){
s=s+"1";
tf.setText(s);
}
}
if(e.getActionCommand()=="2"){
if(c!=5){
s=s+"2";
tf.setText(s);
}
}
if(e.getActionCommand()=="3"){
if(c!=5){
s=s+"3";
tf.setText(s);
}
}
if(e.getActionCommand()=="4"){
if(c!=5){
s=s+"4";
tf.setText(s);
}
}
if(e.getActionCommand()=="5"){
if(c!=5){
s=s+"5";
tf.setText(s);
}
}
if(e.getActionCommand()=="6"){
if(c!=5){
s=s+"6";
tf.setText(s);
}
}
if(e.getActionCommand()=="7"){
if(c!=5){
s=s+"7";
tf.setText(s);
}
}
if(e.getActionCommand()=="8"){
if(c!=5){
s=s+"8";
tf.setText(s);
}
}
if(e.getActionCommand()=="9"){
if(c!=5){
s=s+"9";
tf.setText(s);
}
}
if(e.getActionCommand()=="."){
if(c!=5){
s=s+".";
tf.setText(s);
}
}
if(e.getActionCommand()=="AC"){
s="";
tf.setText(s);
k=0;
p=0;
c=0;
}
if(e.getActionCommand()=="+"){
if(s!=""){
if(c!=0&&c!=5) s=this.equal(s,p,k,c);
k=Float.parseFloat(s);
tf.setText(s);
s="";
}
c=1;
}
if(e.getActionCommand()=="-"){
if(s!=""){
if(c!=0&&c!=5) s=this.equal(s,p,k,c);
k=Float.parseFloat(s);
tf.setText(s);
s="";
}
c=2;
}
if(e.getActionCommand()=="*"){
if(s!=""){
if(c!=0&&c!=5) s=this.equal(s,p,k,c);
k=Float.parseFloat(s);
tf.setText(s);
s="";
}
c=3;
}
if(e.getActionCommand()=="/"){
if(s!=""){
if(c!=0&&c!=5) s=this.equal(s,p,k,c);
k=Float.parseFloat(s);
tf.setText(s);
s="";
}
c=4;
}
if(e.getActionCommand()=="="){
s=this.equal(s,p,k,c);
tf.setText(s);
c=5;    //运算结束
}
}
public String equal(String s,float p,float k,int c){
if(s!=""){
p=Float.parseFloat(s);
if(c==1)
s=""+(p+k);
if(c==2)
s=""+(k-p);
if(c==3)
s=""+(p*k);
if(c==4)
if(p==0) s="除数不能为零";
else s=""+(k/p);
} return s;
} public void keyPressed(KeyEvent e){}
public void keyReleased(KeyEvent arg0){}
public void keyTyped(KeyEvent arg0){}

}

解决方案 »

  1.   

    1.构造函数递归,你把整个类即做适配器又当监听器,什么活都一个人全干了,结果在创建自身的过程中又有调用自身构造方法再次创建自身。何苦呢。
    2.你的主窗口没有设置可见,在构造的最后应该加一句f.setVisible(true);3.你有几个按钮没有添加事件,如1,2,3最后送你几句话
    1.虽然你的功能实现了,但是你的设计却是十分的糟糕
    飞机不是按顺序造出来的,请先分层次,然后每个层次内再分模块,最后再组合
    你这样一口气全部搞定,看起来都类2.一个类的功能应该是明确的,需求的边界是清晰的,而你的类写得什么功能都有,简直一个上帝类,糟糕头顶。尝试着把时间处理写成内部类吧
      

  2.   

    “public class Cal extends WindowAdapter implements ActionListener”呵呵!楼主,你的主类是WindowadApter的子类啊!那么,你的窗口是谁呢?没有窗口,程序如何运行?!它应当是
    public class Cal extends JFrame implements ActionListener