事件监听 计算器

Posted 4910yxq

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了事件监听 计算器相关的知识,希望对你有一定的参考价值。

package 事件监听;
import java.awt.*;
import java.awt.event.*;
public class Event extends Frame implements ActionListener {
 private Button button1;

 public Event() //导入AWT包
  {
   super("个人信息");           //窗口标题
      this.setSize(280,300);    //组件尺寸
      this.setLocation(300, 240);  //组件的显示位置
      this.setBackground(Color.blue);  //组件的背景颜色
      this.setLayout(new FlowLayout());   //容器为流布局,居中
      this.add(new Label("姓名"));          //标签,添加到框架上
      this.add(new TextField("小小青",30));   //文本行,30列
      this.add(new Label("性别"));
      this.add(new TextField("女",30));
      this.add(new Label("爱好"));
      this.add(new TextField("打篮球",30));
      button1=new Button("OK");
       this.add(button1);
       button1.addActionListener(this);
       this.addWindowListener(new WinClose());
       this.setVisible(true);
  }
  public void actionPerformed(ActionEvent ev)
  {
   if(ev.getSource()==button1)
   {
    System.out.print("welcome ");
   
   }
  }
  public static void main(String arg[])
  {
   new Event();
  }
      class WinClose implements WindowListener
  {
   public void windowClosing(WindowEvent ev)
   {
    System.exit(0);
   }
   public void windowOpened(WindowEvent ev) {}
   public void windowActivated(WindowEvent ev) {}
   public void windowDeactivated(WindowEvent ev) {}
   public void windowClose(WindowEvent ev) {}
   public void windowIconified(WindowEvent ev) {}
   public void windowDeiconified(WindowEvent ev) {}
   @Override
   public void windowClosed(WindowEvent arg0) {}
  }
}

技术分享图片

 

 

 

package 计算器;
import java.awt.*; 
import java.awt.event.*;

public  class Calculator extends Frame implements ActionListener {
 private double a=0, b=0, sum=0; 
    int flag=0, count=0,p=1; 
    char mode=‘?‘;  //本次运算的符号 
    private TextField display = new TextField(); 
    private Button one = new Button("1"); 
    private Button two = new Button("2"); 
    private Button three = new Button("3"); 
    private Button four = new Button("4"); 
    private Button five = new Button("5"); 
    private Button six = new Button("6"); 
    private Button seven = new Button("7"); 
    private Button eight = new Button("8"); 
    private Button nine = new Button("9"); 
    private Button zero = new Button("0"); 
    private Button point = new Button("."); 
    private Button add = new Button("+"); 
    private Button sub = new Button("-"); 
    private Button mul = new Button("×"); 
    private Button div = new Button("÷"); 
    private Button result = new Button("="); 
    private Button clr = new Button("CLR"); 
    private Button quit = new Button("quit"); 
    private Label label = new Label("calculator"); 
     
    private class WindowCloser extends WindowAdapter{ 
        public void windowClosing(WindowEvent we) 
        {   System.exit(0); } 
    } 
    //布局 
    private void setup() { 
        Panel top = new Panel(); 
        top.setLayout(new GridLayout(2,1)); 
        top.add(label); 
        top.add(display); 
        Panel number = new Panel(); 
        number.setLayout(new GridLayout(6,3)); 
        number.add(one); number.add(four); number.add(seven); 
        number.add(two); number.add(five); number.add(eight); 
        number.add(three); number.add(six); number.add(nine); 
        number.add(zero); number.add(add); number.add(sub); 
        number.add(point); number.add(mul); number.add(div); 
        number.add(result); number.add(clr); number.add(quit); 
        setLayout(new BorderLayout()); add("North",top); 
        add("South",number); 
    } 
     
    //构造方法 
    public Calculator() { 
        super("Calculator"); 
        setup(); 
        one.addActionListener(this); 
        two.addActionListener(this); 
        three.addActionListener(this); 
        four.addActionListener(this); 
        five.addActionListener(this); 
        six.addActionListener(this); 
        seven.addActionListener(this); 
        eight.addActionListener(this); 
        nine.addActionListener(this); 
        zero.addActionListener(this); 
        point.addActionListener(this); 
        add.addActionListener(this); 
        sub.addActionListener(this); 
        mul.addActionListener(this); 
        div.addActionListener(this); 
        clr.addActionListener(this); 
        quit.addActionListener(this); 
        result.addActionListener(this); 
        addWindowListener(new WindowCloser()); 
        pack(); setSize(200,222); 
        setVisible(true); 
    } 
    void reset0() { 
        a=0; b=0; count=0; sum=0; p=1; mode=‘?‘; 
    } 
    void setAndDis(char c) { 
        count=0; flag=0; 
        a=Double.parseDouble(display.getText()); 
        mode=c; 
    } 
    //说明单击按钮以后要采取的行动 
    public void actionPerformed(ActionEvent ae) { 
        if(ae.getSource() == quit) 
            System.exit(0); 
        if(ae.getSource() == clr) { 
            reset0(); 
            flag=0; 
            display.setText("0"); 
        }else if(ae.getSource() == zero) { 
            if(count != 0) { 
                reset0(); 
            } 
            if(flag == 0) { 
                display.setText("0"); 
                flag=1; 
            }else display.setText(display.getText()+"0"); 
        } 
        //输入0~9 
        if(ae.getSource() == one) { 
            if(count != 0) { 
                reset0(); 
            }if(flag == 0||display.getText().equals("0")) { 
                display.setText("1"); 
                flag=1; 
            }else display.setText(display.getText()+"1"); 
        } 
         
        if(ae.getSource() == two) 
        { 
            if(count != 0) { 
                reset0(); 
            } 
            if(flag == 0||display.getText().equals("0")) { 
                display.setText("2"); 
                flag=1; 
            }else display.setText(display.getText()+"2"); 
        } 
         
        if(ae.getSource() == three) 
        { 
            if(count != 0) { 
                reset0(); 
            } 
            if(flag == 0||display.getText().equals("0")) { 
                display.setText("3"); 
                flag=1; 
            }else display.setText(display.getText()+"3"); 
        } 
         
        if(ae.getSource() == four) 
        { 
            if(count != 0) { 
                reset0(); 
            } 
            if(flag == 0||display.getText().equals("0")) { 
                display.setText("4"); 
                flag=1; 
            }else display.setText(display.getText()+"4"); 
        } 
         
        if(ae.getSource() == five) 
        { 
            if(count != 0) { 
                reset0(); 
            } 
            if(flag == 0||display.getText().equals("0")) { 
                display.setText("5"); 
                flag=1; 
            }else display.setText(display.getText()+"5"); 
        } 
         
        if(ae.getSource() == six) 
        { 
            if(count != 0) { 
                reset0(); 
            } 
            if(flag == 0||display.getText().equals("0")) { 
                display.setText("6"); 
                flag=1; 
            }else display.setText(display.getText()+"6"); 
        } 
         
        if(ae.getSource() == seven) 
        { 
            if(count != 0) { 
                reset0(); 
            } 
            if(flag == 0||display.getText().equals("0")) { 
                display.setText("7"); 
                flag=1; 
            }else display.setText(display.getText()+"7"); 
        } 
         
        if(ae.getSource() == eight) 
        { 
            if(count != 0) { 
                reset0(); 
            } 
            if(flag == 0||display.getText().equals("0")) { 
                display.setText("8"); 
                flag=1; 
            }else display.setText(display.getText()+"8"); 
        } 
         
        if(ae.getSource() == nine) 
        { 
            if(count != 0) { 
                reset0(); 
            } 
            if(flag == 0||display.getText().equals("0")) { 
                display.setText("9"); 
                flag=1; 
            }else display.setText(display.getText()+"9"); 
        } 
         
        if(ae.getSource() == point) { 
            if(flag == 1 && p != 0) { 
                display.setText(display.getText()+"."); 
                p=0; 
            } 
        } 
        //输入+,-,×,÷ 
        if(ae.getSource() == add) { 
            setAndDis(‘+‘); 
        } 
        if(ae.getSource() == sub) { 
            setAndDis(‘-‘); 
        } 
        if(ae.getSource() == mul) { 
            setAndDis(‘ב); 
        } 
        if(ae.getSource() == div) { 
            setAndDis(‘÷‘); 
        } 
         
        if((ae.getSource() == result)&&mode != ‘?‘) { 
            flag=0; 
            if(count == 0) { 
                b=Double.parseDouble(display.getText()); 
                count=1; 
            } 
            sum=run(a,b,mode); 
            a=sum; 
            display.setText(String.valueOf(sum)); 
        }        
    } 
    public double run(double a, double b, char mode) { 
        double sum=0; 
        if(mode == ‘+‘) sum=a+b; 
        if(mode == ‘-‘) sum=a-b; 
        if(mode == ‘ב) sum=a*b; 
        if(mode == ‘÷‘) sum=a/b; 
        return(sum); 
    } 
    public static void main(String[] args) { 
        Calculator cal = new Calculator(); 
    } 

}

 

0*4=0.0

技术分享图片

 

0/6=0.0

技术分享图片

 

3/0=Infinity

技术分享图片

 

1+4=5.0

技术分享图片

 


































































































































































































































































































以上是关于事件监听 计算器的主要内容,如果未能解决你的问题,请参考以下文章

GUI编程事件监听以及简易加法计算器的实现

事件监听 计算器

事件监听 计算机界面

事件监听与计算机界面

计算器及事件监听

计算器界面与监听事件