简单计算器-功能实现_计算器



import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;

public class BaseFrame extends JFrame{

	public BaseFrame (String title)
	{
		setLayout(null);
		setTitle(title);
		setSize(800,800);
		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
			System.exit(0);
			}
		});//关闭窗口的匿名类
		setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
		
	}
	public void showMe(){
		setVisible(true);
	}
	

	

}
简单计算器-功能实现_计算器_02



import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.*;

//btn[0].setText("1");
//btn[1].setText("2");
//btn[2].setText("3");
//btn[3].setText("+");
//btn[4].setText("4");
//btn[5].setText("5");
//btn[6].setText("6");
//btn[7].setText("-");
//btn[8].setText("7");
//btn[9].setText("8");
//btn[10].setText("9");
//btn[11].setText("*");
//btn[12].setText("0");
//btn[13].setText(".");
//btn[14].setText("=");
//btn[15].setText("/");

import javax.swing.*;

public class Calc implements ActionListener {
	 JTextField t1=new JTextField();
	public Calc(){
		BaseFrame f=new BaseFrame("hello");
		JPanel j1=new JPanel();
		JPanel j2=new JPanel();
		j1.setSize(800,100);
		j2.setBounds( 0,100, 800, 700);
		j1.setLayout(new GridLayout(1,1));
		
		j1.add(t1);
		j2.setLayout(new GridLayout(4,4));
		final String[] str = {  "1", "2", "3", "+","4", "5", "6", "-", "7", "8", "9","*", "0", ".","=","/" };
		int n=16;
		JButton[] btn=new JButton[n];
		for(int i=0;i<n;i++)
		{
			btn[i]=new JButton(str[i]);
			btn[i].setPreferredSize(new Dimension(50,50));
			j2.add(btn[i]);
		}
		f.add(j1);
		f.add(j2);
		f.showMe();
		t1.addActionListener(this);
		 for (int i = 0; i < str.length; i++)
		       btn[i].addActionListener(this); 
	}   
    boolean FIRDT = true;
    double number = 0;
    String operator = "=";
	
	 public void actionPerformed(ActionEvent e) {
	       String t = e.getActionCommand();
	      if ("0123456789.".indexOf(t) != -1)
	           calcNumber(t);
	       else
	           handleOperator(t);
	    }
	
	 public void calcNumber(String key) {
	       if (FIRDT)
	           t1.setText(key);
	       else if ((key.equals(".")) && (t1.getText().indexOf(".") !=-1))
	           t1.setText(t1.getText() + ".");
	       else if (!key.equals("."))
	           t1.setText(t1.getText() + key);
	       FIRDT = false;
	    }
	
	 public void handleOperator(String key) {
	       if (operator.equals("+"))
	           number += Double.valueOf(t1.getText());
	       else if (operator.equals("-"))
	           number -= Double.valueOf(t1.getText());
	       else if (operator.equals("*"))
	           number *= Double.valueOf(t1.getText());
	       else if (operator.equals("/"))
	           number /= Double.valueOf(t1.getText());
	       else if (operator.equals("="))
	           number = Double.valueOf(t1.getText());
	       t1.setText(String.valueOf(number));
	       operator = key;
	       FIRDT = true;
	    }
	public static void main(String[] args) {
		new Calc();
		
		
	}
}