当前位置: 代码迷 >> Java相关 >> 一个计数器界面 无法输入数字??是什么原因?
  详细解决方案

一个计数器界面 无法输入数字??是什么原因?

热度:252   发布时间:2008-06-05 16:34:59.0
一个计数器界面 无法输入数字??是什么原因?
程序代码:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.*;
public class Calculator extends MouseAdapter{
    JFrame frame;int i=0;int t=0;int t1=0;
    JButton b1,b2,b3,b4,b5,b6,b7,b8,b9,b0;
    JButton j1,j2,j3,j4,j5,j6; //j1为。j2为=,+  -* /
    JTextField jt;
    JPanel jp1,jp2;
    public static void main(String args[])
    {
        Calculator ca=new Calculator();
        ca.go();        
    }
    public void go(){
        frame=new JFrame("Calculator");
        Container co=frame.getContentPane();
        co.setLayout(new BorderLayout());
        jp1=new JPanel();
        jp1.setLayout(new GridLayout(4,4));
        b1=new JButton("1");
        b2=new JButton("2");
        b3=new JButton("3");
        b4=new JButton("4");
        b5=new JButton("5");
        b6=new JButton("6");
        b7=new JButton("7");
        b8=new JButton("8");
        b9=new JButton("9");
        b0=new JButton("0");
        j1=new JButton(".");
        j2=new JButton("=");
        j3=new JButton("+");
        j4=new JButton("-");
        j5=new JButton("*");
        j6=new JButton("/");
        jp1.add(b7);
        b7.addMouseListener( this);
        jp1.add(b8);
        b8.addMouseListener( this);
        jp1.add(b9);
        b9.addMouseListener( this);
        jp1.add(j3);
        j3.addMouseListener( this);
        jp1.add(b4);
        b4.addMouseListener( this);
        jp1.add(b5);
        b5.addMouseListener( this);
        jp1.add(b6);
        b6.addMouseListener( this);
        jp1.add(j4);
        j4.addMouseListener( this);
        jp1.add(b1);
        b1.addMouseListener( this);
        jp1.add(b2);
        b2.addMouseListener( this);
        jp1.add(b3);
        b3.addMouseListener( this);
        jp1.add(j5);
        j5.addMouseListener( this);
        jp1.add(b0);
        b0.addMouseListener( this);
        jp1.add(j1);
        j1.addMouseListener( this);
        jp1.add(j2);
        j2.addMouseListener( this);
        jp1.add(j6);
        j6.addMouseListener( this);
        
        jt=new JTextField("200 ",20);
        jp2=new JPanel();
        jp2.add(jt);
        co.add(jp2,BorderLayout.NORTH);
        co.add(jp1,BorderLayout.CENTER);
        frame.setSize(350,200);
        frame.setVisible(true);        
    }

    /*for (int i = 0; i <= 9; i++)
        if (temp == button[i] && clickable == true)
          textAnswer.setText(textAnswer.getText() + Integer.toString(i));*/

    public void mouseClicked(MouseEvent e){
        if (e.getSource() == b1) {             // 监听程序监听到的资料为b1时进行相应的处理
            if(i!=0){t=t*10+1;}
            else{t=1;}
            i=i+1;String s=String.valueOf(t);jt.setText(s);
        }
        if (e.getSource() == b2) {             // 监听程序监听到的资料为b1时进行相应的处理
            if(i!=0){t=t*10+2;}
            else{t=2;}
            i=i+1;String s=String.valueOf(t);jt.setText(s);
        }
        if (e.getSource() == b3) {             // 监听程序监听到的资料为b1时进行相应的处理
            if(i!=0){t=t*10+3;}
            else{t=3;}
            i=i+1;String s=String.valueOf(t);jt.setText(s);
        }
        if (e.getSource() == b4) {             // 监听程序监听到的资料为b1时进行相应的处理
            if(i!=0){t=t*10+4;}
            else{t=4;}String s=String.valueOf(t);jt.setText(s);
        }
        if (e.getSource() == b5) {             // 监听程序监听到的资料为b1时进行相应的处理
            if(i!=0){t=t*10+5;}
            else{t=5;}i=i+1;String s=String.valueOf(t);jt.setText(s);
        }
        if (e.getSource() == b6) {             // 监听程序监听到的资料为b1时进行相应的处理
            if(i!=0){t=t*10+6;}
            else{t=6;}i=i+1;String s=String.valueOf(t);jt.setText(s);
        }
        if (e.getSource() == b7) {             // 监听程序监听到的资料为b1时进行相应的处理
            if(i!=0){t=t*10+7;}
            else{t=7;}i=i+1;String s=String.valueOf(t);jt.setText(s);
        }
        if (e.getSource() == b8) {             // 监听程序监听到的资料为b1时进行相应的处理
            if(i!=0){t=t*10+8;}
            else{t=8;}i=i+1;String s=String.valueOf(t);jt.setText(s);
        }
        if (e.getSource() == b9) {             // 监听程序监听到的资料为b1时进行相应的处理
            if(i!=0){t=t*10+9;}
            else{t=9;}i=i+1;String s=String.valueOf(t);jt.setText(s);jt.setText(""+t);
        }
        if (e.getSource() == b0) {             // 监听程序监听到的资料为b1时进行相应的处理
            t=t;i=i+1;String s=String.valueOf(t);jt.setText(s);
        }   System .out.print(t  ); System.out.println(i);
        String s=String.valueOf(t);jt.setText(s);
    
            jt.setText("");        
            if(e.getSource()==j6){if(t1==0)t1=t;else{jt.setText("");t1=t1/t;}jt.setText("/");i=0;t=0;}
            if(e.getSource()==j5){if(t1==0)t1=t;else{jt.setText("");t1=t1*t;}jt.setText("*");i=0;t=0;}
            if(e.getSource()==j4){if(t1==0)t1=t;else{jt.setText("");t1=t1-t;}jt.setText("-");i=0;t=0;}
            if(e.getSource()==j3){if(t1==0)t1=t;else{jt.setText("");t1=t1+t;}jt.setText("+");i=0;t=0;}
            
            if(e.getSource()==j2){String s1=String.valueOf(t1);jt.setText(s1);System.out.println(t1);t1=0;}            
    }
        
    
}



程序可以通过编译,但是运行后,无法输入数字?    那里有点问题?  大家帮我看下,谢谢了!
搜索更多相关的解决方案: 计数器  数字  界面  输入  

----------------解决方案--------------------------------------------------------
JButton应该用ActionListener事件监听器
你的JTextField没有事件处理,怎么会有显示???
----------------解决方案--------------------------------------------------------
  相关解决方案