package calculator;
import java.awt.*;
import java.awt.event.*;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.HeadlessException;
import java.awt.Image;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JTextField;
public class Calculator extends JFrame {
// 声明组件
private JMenuBar bar; //声明标题栏
private JPanel panel; //声明
private JTextField text; //声明结果显示栏
private JPanel panelBtn; //声明键盘栏
private JLabel btnSin, btnCos, btnTan, btnLog, btnExp, btnBack, btnCE,
btnC, btnAR, btnSqrt, btn0, btnPoint, btn1, btn2, btn3, btn4, btn5,
btn6, btn7, btn8, btn9, btnAdd, btnReduce, btnMultiply, btnDivide,
btnMod, btnBottom, btnEqual, am, bm, cm;
/**************变量定义*****************************************************/
private double value = 0; //保存预执行操作的数
private int operation = 0; //运算操作:0:无操作。1:加。2:减。3:乘。4:除
private StringBuffer tmp = new StringBuffer(); //保存临时数据
private int allowLength = 32; //文字框允许输入的数字长度
private boolean hasPressed = false;
/***********main方法入口***************************************************/
public static void main(String[] args) {
new Calculator().init();
}
/***********GUI初始化*****************************************************/
public void init() {
bar = new JMenuBar(); //定义标题栏
bar.setBackground(new Color(217, 223, 240)); //标题栏颜色配置
panel = new JPanel(new BorderLayout(10, 3)); //BorderLayout布局
text = new JTextField("0"); //默认结果显示0
text.setEditable(false); //
text.setBackground(Color.WHITE); //文本框颜色配置
//text.setMargin(new Insets(0, 6, 0, 8));
text.setHorizontalAlignment(JTextField.RIGHT); //右对齐
panelBtn = new JPanel(); //定义键盘栏
panelBtn.setBackground(new Color(217, 223, 240)); //键盘栏颜色配置
panelBtn.setLayout(null);
/****************按键配置*************************************************/
btnSin = new JLabel(new ImageIcon("image/sin1.png"));
btnSin.setBounds(10, 5, 34, 27);
btnCos = new JLabel(new ImageIcon("image/cos1.png"));
btnCos.setBounds(50, 5, 34, 27);
btnTan = new JLabel(new ImageIcon("image/tan1.png"));
btnTan.setBounds(90, 5, 34, 27);
btnLog = new JLabel(new ImageIcon("image/log1.png"));
btnLog.setBounds(130, 5, 34, 27);
btnExp = new JLabel(new ImageIcon("image/exp1.png"));
btnExp.setBounds(170, 5, 34, 27);
btnBack = new JLabel(new ImageIcon("image/b1.png"));
btnBack.setBounds(10, 40, 34, 27);
btnCE = new JLabel(new ImageIcon("image/ce1.png"));
btnCE.setBounds(50, 40, 34, 27);
btnC = new JLabel(new ImageIcon("image/c1.png"));
btnC.setBounds(90, 40, 34, 27);
btnAR = new JLabel(new ImageIcon("image/ar1.png"));
btnAR.setBounds(130, 40, 34, 27);
btnSqrt = new JLabel(new ImageIcon("image/s1.png"));
btnSqrt.setBounds(170, 40, 34, 27);
btn7 = new JLabel(new ImageIcon("image/71.png"));
btn7.setBounds(10, 75, 34, 27);
btn8 = new JLabel(new ImageIcon("image/81.png"));
btn8.setBounds(50, 75, 34, 27);
btn9 = new JLabel(new ImageIcon("image/91.png"));
btn9.setBounds(90, 75, 34, 27);
btnDivide = new JLabel(new ImageIcon("image/d1.png"));
btnDivide.setBounds(130, 75, 34, 27);
btnMod = new JLabel(new ImageIcon("image/mod1.png"));
btnMod.setBounds(170, 75, 34, 27);
btn4 = new JLabel(new ImageIcon("image/41.png"));
btn4.setBounds(10, 110, 34, 27);
btn5 = new JLabel(new ImageIcon("image/51.png"));
btn5.setBounds(50, 110, 34, 27);
btn6 = new JLabel(new ImageIcon("image/61.png"));
btn6.setBounds(90, 110, 34, 27);
btnMultiply = new JLabel(new ImageIcon("image/m1.png"));
btnMultiply.setBounds(130, 110, 34, 27);
btnBottom = new JLabel(new ImageIcon("image/bt1.png"));
btnBottom.setBounds(170, 110, 34, 27);
btn1 = new JLabel(new ImageIcon("image/11.png"));
btn1.setBounds(10, 145, 34, 27);
btn2 = new JLabel(new ImageIcon("image/21.png"));
btn2.setBounds(50, 145, 34, 27);
btn3 = new JLabel(new ImageIcon("image/31.png"));
btn3.setBounds(90, 145, 34, 27);
btnReduce = new JLabel(new ImageIcon("image/r1.png"));
btnReduce.setBounds(130, 145, 34, 27);
btnEqual = new JLabel(new ImageIcon("image/e1.png"));
btnEqual.setBounds(170, 145, 34, 62);
btn0 = new JLabel(new ImageIcon("image/01.png"));
btn0.setBounds(10, 180, 73, 27);
btnPoint = new JLabel(new ImageIcon("image/p1.png"));
btnPoint.setBounds(90, 180, 34, 27);
btnAdd = new JLabel(new ImageIcon("image/a1.png"));
btnAdd.setBounds(130, 180, 34, 27);
am = new JLabel("基于Java_JDK的计算器");
bm = new JLabel("仅用于学习和交流");
bm.setBounds(50, 215, 200, 27);
cm = new JLabel("2015.7.5");
cm.setBounds(80, 235, 205, 27);
/************按键及文字布局***********************************************/
setBounds(10, 5, 34, 27);
bar.add(am);
bar.setLayout(new FlowLayout(FlowLayout.CENTER,3,0));
panelBtn.add(btnSin);
panelBtn.add(btnCos);
panelBtn.add(btnTan);
panelBtn.add(btnLog);
panelBtn.add(btnExp);
panelBtn.add(btnBack);
panelBtn.add(btnCE);
panelBtn.add(btnC);
panelBtn.add(btnAR);
panelBtn.add(btnSqrt);
panelBtn.add(btn7);
panelBtn.add(btn8);
panelBtn.add(btn9);
panelBtn.add(btnDivide);
panelBtn.add(btnMod);
panelBtn.add(btn4);
panelBtn.add(btn5);
panelBtn.add(btn6);
panelBtn.add(btnMultiply);
panelBtn.add(btnBottom);
panelBtn.add(btn1);
panelBtn.add(btn2);
panelBtn.add(btn3);
panelBtn.add(btnReduce);
panelBtn.add(btnEqual);
panelBtn.add(btn0);
panelBtn.add(btnPoint);
panelBtn.add(btnAdd);
panelBtn.add(bm);
panelBtn.add(cm);
panel.add(text, BorderLayout.NORTH);
panel.add(panelBtn);
/********整体配置**********************************/
this.setJMenuBar(bar); //显示标题栏
this.add(panel); //显示键盘栏及文本框
this.setTitle("计算器"); //窗口标题设置
this.setBounds(555, 185, 220, 335); //设置窗口位置及大小
this.setVisible(true);
this.setResizable(false); //窗口固定大小不可拖动
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
/*******图标配置**********************************************************/
Image icon = Toolkit.getDefaultToolkit().getImage("image/ico.png");
this.setIconImage(icon);
/*******配置按键监听 *****************************************************/
MyListener myLis = new MyListener();
btnSin.addMouseListener(myLis);
btnCos.addMouseListener(myLis);
btnTan.addMouseListener(myLis);
btnLog.addMouseListener(myLis);
btnExp.addMouseListener(myLis);
btnBack.addMouseListener(myLis);
btnCE.addMouseListener(myLis);
btnC.addMouseListener(myLis);
btnAR.addMouseListener(myLis);
btnSqrt.addMouseListener(myLis);
btn0.addMouseListener(myLis);
btnPoint.addMouseListener(myLis);
btn1.addMouseListener(myLis);
btn2.addMouseListener(myLis);
btn3.addMouseListener(myLis);
btn4.addMouseListener(myLis);
btn5.addMouseListener(myLis);
btn6.addMouseListener(myLis);
btn7.addMouseListener(myLis);
btn8.addMouseListener(myLis);
btn9.addMouseListener(myLis);
btnAdd.addMouseListener(myLis);
btnReduce.addMouseListener(myLis);
btnMultiply.addMouseListener(myLis);
btnDivide.addMouseListener(myLis);
btnMod.addMouseListener(myLis);
btnBottom.addMouseListener(myLis);
btnEqual.addMous
评论0