JAVA实现计算器(按键事件+点击事件,仿手机计算器)

 
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class Calculator extends JFrame implements ActionListener,KeyListener       //继承监听事件接口     
{
	static final int WIDTH=300;				//Frame窗口宽300
	static final int HEIGHT=400;			//Frame窗口高400
	static ScriptEngine jse = new ScriptEngineManager().getEngineByName("JavaScript");  //JDK自带的类可以实现调用JS的功能,可以实现执行字符串中的运算公式的功能
	private JFrame frame;
	private JPanel Panel;
	private JTextField resultText;
	private String[] KEYS={"mc","m+","m-","mr","AC","+/-","/","*","7","8","9","-","4","5","6","+","1","2","3","0",".","="};
	private JButton keys[]=new JButton[KEYS.length];
	private double resultNum=0.0;
	public Calculator()
	{	
	super("计算器");
	Panel=new JPanel();
	Panel.setLayout(null);		//使用空布局
	resultText=new JTextField("0");
	resultText.setBounds(5, 8, 285, 50);
	resultText.setColumns(25);
	resultText.setHorizontalAlignment(JTextField.RIGHT);
	resultText.setEditable(false);
	resultText.setBackground(Color.white);
	for(int i=0;i


JAVA实现计算器(按键事件+点击事件,仿手机计算器)_第1张图片JAVA实现计算器(按键事件+点击事件,仿手机计算器)_第2张图片

你可能感兴趣的:(JAVA实现计算器(按键事件+点击事件,仿手机计算器))