import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class jisuanqi extends JFrame implements ActionListener{
        private float Num1,Num2,Num; //分别用来存储运算数和结果
        private char sign;           //存储输入的运算符
        
        //定义并初始化控件
        private JPanel p1=new JPanel();
        private JPanel p2=new JPanel();
        private JPanel p3=new JPanel();
        private JPanel p4=new JPanel();
        private JPanel p5=new JPanel();
        private JPanel p6=new JPanel();
        private JPanel p7=new JPanel();
        private JPanel p8=new JPanel();
        private JPanel p9=new JPanel();
        
        private JTextField text=new JTextField("0",20);//定义文本域初始值为0
        
        private JButton clear=new JButton("清除");
        
        private JButton b0=new JButton("0");
        private JButton b1=new JButton("1");
        private JButton b2=new JButton("2");
        private JButton b3=new JButton("3");
        private JButton b4=new JButton("4");
        private JButton b5=new JButton("5");
        private JButton b6=new JButton("6");
        private JButton b7=new JButton("7");
        private JButton b8=new JButton("8");
        private JButton b9=new JButton("9");
        private JButton Ab=new JButton("A");
        private JButton B=new JButton("B");
        private JButton C=new JButton("C");
        private JButton D=new JButton("D");
        private JButton E=new JButton("E");
        private JButton F=new JButton("F");
        
        private JButton cheng=new JButton("*");
        private JButton chu=new JButton("/");
        private JButton jia=new JButton("+");
        private JButton jian=new JButton("-");
        private JButton deng=new JButton("=");
        private JButton dian=new JButton(".");
        private JButton sqrt=new JButton("sqrt");
        private JButton mee=new JButton("^");
       
         private JButton ox=new JButton("二进制");
         private JButton dx=new JButton("十进制");
         private JButton hex=new JButton("十六进制");
        
        
        
        public jisuanqi(){  
                super("计算器");
                setResizable(false);//屏蔽最大化 
                this.getContentPane().setLayout(new GridLayout(9,1));
                this.getContentPane().add(p1); 
                this.getContentPane().add(p6); 
                this.getContentPane().add(p2); 
                this.getContentPane().add(p3); 
                this.getContentPane().add(p4); 
                this.getContentPane().add(p5);
                this.getContentPane().add(p7); 
                this.getContentPane().add(p8); 
                this.getContentPane().add(p9);   
                
                //按钮
                p1.add(text);                p6.setLayout(new BorderLayout());
                p6.add("Center",clear);
                
                p2.setLayout(new GridLayout(1,4));
                p2.add(b7); p2.add(b8); p2.add(b9); p2.add(jia);
                
                p3.setLayout(new GridLayout(1,4));
                p3.add(b4); p3.add(b5); p3.add(b6); p3.add(jian);
                
                p4.setLayout(new GridLayout(1,4));
                p4.add(b1); p4.add(b2); p4.add(b3); p4.add(cheng);
                
                p5.setLayout(new GridLayout(1,4));
                p5.add(dian); p5.add(b0); p5.add(Ab); p5.add(chu);
                
                p7.setLayout(new GridLayout(1,4));
                p7.add(B); p7.add(C); p7.add(D); p7.add(E);
                
                 p8.setLayout(new GridLayout(1,4));
                p8.add(F); p8.add(ox); p8.add(dx); p8.add(hex);
                
                p9.setLayout(new GridLayout(1,3));
                p9.add(sqrt); p9.add(mee); p9.add(deng);
                
                b0.addActionListener(this);
                b1.addActionListener(this);
                b2.addActionListener(this);
                b3.addActionListener(this);
                b4.addActionListener(this);
                b5.addActionListener(this);
                b6.addActionListener(this);
                b7.addActionListener(this);
                b8.addActionListener(this);
                b9.addActionListener(this);
                /*Ab.addActionlistener(this);
                B.addActionlistener(this);
                C.addActionlistener(this);
                D.addActionlistener(this);
                E.addActionlistener(this);
                F.addActionlistener(this);*/
                
                                b0.addKeyListener(new KeyBoard());
                                b1.addKeyListener(new KeyBoard());
                                b2.addKeyListener(new KeyBoard());
                                b3.addKeyListener(new KeyBoard());
                                b4.addKeyListener(new KeyBoard());
                                b5.addKeyListener(new KeyBoard());
                                b6.addKeyListener(new KeyBoard());
                                b7.addKeyListener(new KeyBoard());
                                b8.addKeyListener(new KeyBoard());
                                b9.addKeyListener(new KeyBoard());
                                /*A.addKeyListener(new KeyBoard());
                                B.addKeyListener(new KeyBoard());
                                C.addKeyListener(new KeyBoard());
                                D.addKeyListener(new KeyBoard());
                                E.addKeyListener(new KeyBoard());
                                F.addKeyListener(new KeyBoard());*/
                                
                                
                clear.addActionListener(this);
                jia.addActionListener(this);
                jian.addActionListener(this);
                cheng.addActionListener(this);
                chu.addActionListener(this);
                deng.addActionListener(this);
                dian.addActionListener(this);
                sqrt.addActionlistener(this);
                mee.addActionlistener(this);
                                               clear.addKeyListener(new KeyBoard());
                                jia.addKeyListener(new KeyBoard());
                                jian.addKeyListener(new KeyBoard());
                                cheng.addKeyListener(new KeyBoard());
                                chu.addKeyListener(new KeyBoard());
                                deng.addKeyListener(new KeyBoard());
                                dian.addKeyListener(new KeyBoard());
                                sqrt.addKeyListener(new KeyBoard());
                                mee.addKeyListener(new KeyBoard());
                               
                                
                                  text.addKeyListener(new KeyBoard());
                
                pack();show();
                
                //窗口关闭
                addWindowListener(new WindowAdapter(){
                        public void windowClosing(WindowEvent we){
                                System.exit(0);
                                }
                        });
                        text.setEditable(false);//去掉文本的可编辑性
                        text.setHorizontalAlignment(text.RIGHT);//文本右对齐
                }
        
        

解决方案 »

  1.   

    上面只是一半,下面还有。。
    麻烦大家了
    //按钮事件
            public void actionPerformed(ActionEvent e){
                    String input=e.getActionCommand();//定义input存储按钮点击产生的值
                    String str="0123456789ABCDEF";
                    String ysf="+-*/^";
                    
                    //数字输入
                    if(str.indexOf(input)>=0){
                            if(text.getText().equals("0"))
                              text.setText(input);//若TextField中初始值为零则TextField中显示输入的数字
                            else
                              text.setText(text.getText()+input);//如果TextField中的值不是零则叠加
                            }
                            
                    //小数点
                    else if(input.equals("."))
                          text.setText(text.getText()+input);
                     
                    //运算符
                    else if(ysf.indexOf(input)>=0){
                            Num1=Float.parseFloat(text.getText());//输入运算符时Num1存储TextField中的值
                            text.setText("");
                            sign=ysf.charAt(ysf.indexOf(input));//sign读取输入的运算符
                            }
                    
                    //等号        
                    else if(input.equals("=")){
                            Num2=Float.parseFloat(text.getText());//Num2存储点击运算符后的数值
                            
                           
    switch(sign){
                                    //加法
                                    case '+':{
                                            Num=Num1+Num2;
                                            text.setText(""+Num);
                                            break;
                                            }
                                            
                                    //减法
                                    case '-':{
                                            Num=Num1-Num2;
                                            text.setText(""+Num);
                                            break;
                                            }
                                    
                                    //乘法
                                    case '*':{
                                            Num=Num1*Num2;
                                            text.setText(""+Num);
                                            break;
                                            }
                                            
                                    //除法
                                    case '/':{
                                            if(Num2==0)
                                              text.setText("除数不能为零");//除数为零,则提示除数不能为零
                                            else{
                                                    Num=Num1/Num2;
                                                    text.setText(""+Num);
                                                    break;
                                                    }
                                    
                                            }
                                    //幂
                                    case '^':{
                                     int i=0,m=Mum1;
                                            double s=1;
                                              for (i=1;i<=Mum2;i++)
                                                  s=s*m;
                                                   Mum=s;
                                                   text.setText(""+Num);
                                                    break;
                                   
                                                 }
                                    }
                            }
                            
                            //清除
                            else if(input.equals("清除")){
                                    Num=' ';
                                    Num1=' ';
                                    Num2=' ';
                                    sign=' ';
                                    text.setText("0");
                                    }
                    }                
            //键盘事件
            class KeyBoard implements KeyListener{
                    char ysf2=' ';//存储运算符
                    
                    //按下事件
                    public void keyPressed(KeyEvent ke){
                            if(ke.getKeyCode()==27){//清除(Esc)
                                    Num1=' ';
                                    Num2=' ';
                                    Num=' ';
                                    text.setText("0");
                                    }
                                    
                            if((ke.getKeyCode()==10)||(ke.getKeyChar()=='=')){//回车或等号
                                    Num2=Float.parseFloat(text.getText());//Num2存储点击运算符后的数值
                                    switch(ysf2){
                                            case '+':{
                                                    Num=Num1+Num2;
                                                    text.setText(""+Num);
                                                    break;
                                                    }
                                            case '-':{
                                                    Num=Num1-Num2;
                                                    text.setText(""+Num);
                                                    break;
                                                    }
                                            case '*':{
                                                    Num=Num1*Num2;
                                                    text.setText(""+Num);
                                                    break;
                                                    }
                                            case '/':{
                                                    if(Num2==0){
                                                      text.setText("除数不能为零");
                                                            }
                                                    else{
                                                            Num=Num1/Num2;
                                                            text.setText(""+Num);
                                                            }
                                                    break;
                                                    }
                                            case '^':{
                                                int i=0,m=Mum1;
                                                        double s=1;
                                                        for (i=1;i<=Mum2;i++)
                                                          s=s*m;
                                                         Mum=s;
                                                    text.setText(""+Num);
                                                    break;
                                   
                                                 }
                                            }
                                    }
                            
                        }
     还有。待续
      

  2.   

     //弹起事件
                    public void keyReleased(KeyEvent ke){
                            //从键盘读入数字
                            if(ke.getKeyChar()=='0'){
                                    if(text.getText().equals("0"))
                                            text.setText("0");
                                    else text.setText(text.getText()+"0");
                                    }
                            if(ke.getKeyChar()=='1'){
                                    if(text.getText().equals("0"))
                                            text.setText("1");
                                    else text.setText(text.getText()+"1");
                                    }
                            if(ke.getKeyChar()=='2'){
                                    if(text.getText().equals("0"))
                                            text.setText("2");
                                    else text.setText(text.getText()+"2");
                                    }
                            if(ke.getKeyChar()=='3'){
                                    if(text.getText().equals("0"))
                                            text.setText("3");
                                    else text.setText(text.getText()+"3");
                                    }
                            if(ke.getKeyChar()=='4'){
                                    if(text.getText().equals("0"))
                                            text.setText("4");
                                    else text.setText(text.getText()+"4");
                                    }
                            if(ke.getKeyChar()=='5'){
                                    if(text.getText().equals("0"))
                                            text.setText("5");
                                    else text.setText(text.getText()+"5");
                                    }
                            if(ke.getKeyChar()=='6'){
                                    if(text.getText().equals("0"))
                                            text.setText("6");
                                    else text.setText(text.getText()+"6");
                                    }
                            if(ke.getKeyChar()=='7'){
                                    if(text.getText().equals("0"))
                                            text.setText("7");
                                    else text.setText(text.getText()+"7");
                                    }
                            if(ke.getKeyChar()=='8'){
                                    if(text.getText().equals("0"))
                                            text.setText("8");
                                    else text.setText(text.getText()+"8");
                                    }
                            if(ke.getKeyChar()=='9'){
                                    if(text.getText().equals("0"))
                                            text.setText("9");
                                    else text.setText(text.getText()+"9");
                                    }
                                    
                            //从键盘输入小数点        
                            if(ke.getKeyChar()=='.')
                                    text.setText(text.getText()+".");
                                    
                            //加法
                            if(ke.getKeyChar()=='+'){
                                    Num1=Float.parseFloat(text.getText());
                                    ysf2='+';
                                    text.setText("");
                                    }
                                    
                            //减法
                            if(ke.getKeyChar()=='-'){
                                    Num1=Float.parseFloat(text.getText());
                                    ysf2='-';
                                    text.setText("");
                                    }
                                    
                            //乘法
                            if(ke.getKeyChar()=='*'){
                                    Num1=Float.parseFloat(text.getText());
                                    ysf2='*';
                                    text.setText("");
                                    }
                                    
                            //除法
                            if(ke.getKeyChar()=='/'){
                                    Num1=Float.parseFloat(text.getText());
                                    ysf2='/';
                                    text.setText("");
                                    }
                            }
                    public void keyTyped(KeyEvent ke){
                            
                            }
                    }
            public static void main(String args[]){
                    jisuanqi jsq=new jisuanqi();
                    }
            }
    以上的题做了一半,做不下去了,思想是要使键盘和鼠标都实现,而且键盘的“sqrt”是用zlt+s实现的
      

  3.   

    “以上的题做了一半,做不下去了,”“做不下去”的障碍在哪里?
    这个程序的思路无非是鼠标事件和键盘事件的处理,你要同时实现两个,请记住这两个要分开,不要混淆到一起,经常有人犯这种错误。
    鼠标事件源是每一个按钮,当你在该按钮上点击,即触发事件,在文本框里显示该按钮的数字;
    键盘事件源是程序主窗口本身,只要焦点在主窗口上,则按某个键,即触发键盘事件,在文本框里显示该键盘的数字;至于Alt+s,那是组合键的定义而已,教材上都有清晰的介绍的。