Java 小练习之计算器

import java.awt.Button;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class jiemian extends Frame implements ActionListener{
	/**
	 * @param args
	 */
	yunsuan y;
	float aa,bb,cc;
	GridLayout grid;
	Panel p;
	Label a=new Label("被运算数");
	Label b=new Label("运算数");
	Label c=new Label("结果");
	TextField a1=new TextField(10);
	TextField b1=new TextField(10);
	TextField c1=new TextField(10);
	Button jb1=new Button("+");
	Button jb2=new Button("-");
	Button jb3=new Button("*");
	Button jb4=new Button("/");
	jiemian(){
		this.setTitle("计算器");
		grid=new GridLayout(5,2);
		p=new Panel();
		p.setLayout(grid);
		p.add(a);
		p.add(a1);
		p.add(b);
		p.add(b1);
		p.add(c);
		p.add(c1);
		p.add(jb1);
		p.add(jb2);
		p.add(jb3);
		System.out.println("safdasdf");
		p.add(jb4);
		jb1.addActionListener(this);
		jb2.addActionListener(this);
		jb3.addActionListener(this);
		jb4.addActionListener(this);
		add(p);
		this.setBounds(100,100,300,150);
		addWindowListener(new closeWin());
		this.setVisible(true);
	}
	 public static void main(String args[])
	    {
	    	jiemian j=new jiemian();
	    }
		public void actionPerformed(ActionEvent e){
			float k;
		
			if(e.getSource()==jb1){
				System.out.println("xianzai1");
				aa=Float.parseFloat(a1.getText());
				System.out.println("xianzai1");
				bb=Float.parseFloat(b1.getText());
				
				System.out.println("xianzai2");
				k=aa+bb;
				System.out.println("asd");
				c1.setText(Float.toString(k));
			}
			else if(e.getSource()==jb2){
				System.out.println("xianzai1");
			    aa=Float.parseFloat(a1.getText());
				System.out.println("xianzai1");
				bb=Float.parseFloat(b1.getText());
				System.out.println("xianzai2");
				k=aa-bb;
				System.out.println("asd");
				c1.setText(Float.toString(k));
			}
			else if(e.getSource()==jb3){
				System.out.println("xianzai1");
				aa=Float.parseFloat(a1.getText());
				System.out.println("xianzai1");
				bb=Float.parseFloat(b1.getText());
				System.out.println("xianzai2");
				k=aa*bb;
				System.out.println("asd");
				c1.setText(Float.toString(k));
			}
			else if(e.getSource()==jb4){
				System.out.println("xianzai1");
				aa=Float.parseFloat(a1.getText());
				System.out.println("xianzai1");
				bb=Float.parseFloat(b1.getText());
				System.out.println("xianzai2");
				k=aa/bb;
				System.out.println("asd");
				c1.setText(Float.toString(k));
			}
		}
}
class closeWin extends WindowAdapter    //关闭窗口类
{
	public void windowClosing(WindowEvent e)
	{
		Frame frm=(Frame)(e.getSource());
		frm.dispose();
		System.exit(0);
		
	}
}

你可能感兴趣的:(Java 小练习之计算器)