20100915_Dawning_Calculator


package com.calculator.model; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import java.awt.Dimension; import java.awt.EventQueue; import java.awt.GridLayout; import java.awt.Insets; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import java.util.Vector; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextField; import com.calculator.biz.OperateAction; import com.calculator.entity.Operate; /*** * 计算器 * * @author Dawning * * @author 2010-09-15 */ public class DawningCalculator implements ActionListener { public static void main(String[] args) { new DawningCalculator(); } //Java计算器 构造器 public DawningCalculator() { addComponent(); } JFrame f; JMenu mEdit,mView,mHelp; JMenuItem mCopy,mPaste,mExit,mShowView; JTextField tResult; Container contentPane; JButton bNumber,bOperator,bOther,bM; int i = 0 ; boolean isDouble = false;//是否为实数 int preOp, currentOp = 0;//标准位,保存当前的操作符号 double op1 = 0, op2 = 0; //操作数 StringBuffer copyBoard = new StringBuffer(20); //剪贴板 StringBuffer memory = new StringBuffer(20); //M系列 StringBuffer str = new StringBuffer(); //记录BackSpace Operate operate = null; List flag = null; List flag2 = null; /*** * 生成组建 */ private void addComponent() { flag = new ArrayList(); flag2 = new ArrayList(); f = new JFrame("Dawning计算器"); f.setLocation(300,200); contentPane = f.getContentPane(); /**************************Java计算器 菜单的创建*****************************/ JMenuBar mBar = new JMenuBar(); mBar.setOpaque(true); /** * JMenu 实例 */ mEdit = new JMenu("编辑(E)"); mEdit.setMnemonic(KeyEvent.VK_E); //鼠标事件KeyEvent.VK_E mCopy = new JMenuItem("复制(C)"); mCopy.setMnemonic(KeyEvent.VK_C); mCopy.addActionListener(this); mEdit.add(mCopy); mPaste = new JMenuItem("粘贴(P)"); mPaste.setMnemonic(KeyEvent.VK_P); mPaste.addActionListener(this); mEdit.add(mPaste); mPaste = new JMenuItem("保存(S)"); mPaste.setMnemonic(KeyEvent.VK_S); mPaste.addActionListener(this); mEdit.add(mPaste); mEdit.addSeparator(); mExit = new JMenuItem("Exit"); mExit.setMnemonic(KeyEvent.VK_ESCAPE); mExit.addActionListener(this); mEdit.add(mExit); mView = new JMenu("查看(V)"); mView.setMnemonic(KeyEvent.VK_V); mShowView = new JMenuItem("结果排序"); mShowView.addActionListener(this); mView.add(mShowView); mShowView = new JMenuItem("时间排序"); mShowView.addActionListener(this); mView.add(mShowView); mView.addSeparator(); mShowView = new JMenuItem("标准显示"); mShowView.addActionListener(this); mView.add(mShowView); mHelp = new JMenu("帮助(H)"); mHelp.setMnemonic(KeyEvent.VK_H); mHelp.add(new JMenuItem("帮助主题")); mHelp.addSeparator(); mHelp.add(new JMenuItem("关于计算器")); /** * 添加菜单到菜单栏中 */ mBar.add(mEdit); mBar.add(mView); mBar.add(mHelp); f.setJMenuBar(mBar); contentPane.setLayout(new BorderLayout()); JPanel pTop = new JPanel(); tResult = new JTextField("0.", 26); tResult.setHorizontalAlignment(JTextField.RIGHT); tResult.setEditable(false); pTop.add(tResult); contentPane.add(pTop, BorderLayout.NORTH); JPanel pBottom = new JPanel(); pBottom.setLayout(new BorderLayout()); JPanel pLeft = new JPanel(); pLeft.setLayout(new GridLayout(5, 1, 3, 3)); bM = new JButton(" "); bM.setEnabled(false); pLeft.add(bM); /*************************Java计算器 功能键定义***************************/ bOther = new JButton("MC"); bOther.addActionListener(this); bOther.setForeground(Color.red); bOther.setMargin(new Insets(3, 2, 3, 2)); pLeft.add(bOther); bOther = new JButton("MR"); bOther.addActionListener(this); bOther.setForeground(Color.red); bOther.setMargin(new Insets(3, 2, 3, 2)); pLeft.add(bOther); bOther = new JButton("MS"); bOther.addActionListener(this); bOther.setForeground(Color.red); bOther.setMargin(new Insets(3, 2, 3, 2)); pLeft.add(bOther); bOther = new JButton("M+"); bOther.addActionListener(this); bOther.setForeground(Color.red); bOther.setMargin(new Insets(3, 2, 3, 2)); pLeft.add(bOther); pBottom.add(pLeft, BorderLayout.WEST); JPanel pRight = new JPanel(); pRight.setLayout(new BorderLayout()); JPanel pUp = new JPanel(); pUp.setLayout(new GridLayout(1, 3, 3, 0)); bOther = new JButton("BackSpace"); bOther.addActionListener(this); bOther.setForeground(Color.red); bOther.setMargin(new Insets(3, 0, 3, 5)); pUp.add(bOther); bOther = new JButton("CE"); bOther.addActionListener(this); bOther.setForeground(Color.red); pUp.add(bOther); bOther = new JButton("C"); bOther.addActionListener(this); bOther.setForeground(Color.red); pUp.add(bOther); /***************************Java计算器 数字键盘区定义**************************/ JPanel pDown = new JPanel(); pDown.setLayout(new GridLayout(4, 5, 3, 2)); bNumber = new JButton("7"); bNumber.setForeground(Color.blue); bNumber.addActionListener(this); bNumber.setMargin(new Insets(3, 3, 3, 3)); pDown.add(bNumber); bNumber = new JButton("8"); bNumber.setForeground(Color.blue); bNumber.addActionListener(this); bNumber.setMargin(new Insets(3, 3, 3, 3)); pDown.add(bNumber); bNumber = new JButton("9"); bNumber.setForeground(Color.blue); bNumber.addActionListener(this); bNumber.setMargin(new Insets(3, 3, 3, 3)); pDown.add(bNumber); bOperator = new JButton("/"); bOperator.setForeground(Color.red); bOperator.addActionListener(this); bOperator.setMargin(new Insets(3, 0, 3, 0)); pDown.add(bOperator); bOperator = new JButton("sqrt"); bOperator.addActionListener(this); bOperator.setForeground(Color.red); bOperator.setMargin(new Insets(3, 0, 3, 0)); pDown.add(bOperator); bNumber = new JButton("4"); bNumber.setForeground(Color.blue); bNumber.addActionListener(this); bNumber.setMargin(new Insets(3, 3, 3, 3)); bNumber.setHorizontalTextPosition(JButton.LEFT); pDown.add(bNumber); bNumber = new JButton("5"); bNumber.setForeground(Color.blue); bNumber.addActionListener(this); bNumber.setMargin(new Insets(3, 3, 3, 3)); pDown.add(bNumber); bNumber = new JButton("6"); bNumber.setForeground(Color.blue); bNumber.addActionListener(this); bNumber.setMargin(new Insets(3, 3, 3, 3)); pDown.add(bNumber); bOperator = new JButton("*"); bOperator.setForeground(Color.red); bOperator.addActionListener(this); bOperator.setMargin(new Insets(3, 3, 3, 3)); pDown.add(bOperator); bOperator = new JButton("%"); bOperator.setForeground(Color.blue); bOperator.addActionListener(this); bOperator.setMargin(new Insets(3, 3, 3, 3)); pDown.add(bOperator); bNumber = new JButton("1"); bNumber.setForeground(Color.blue); bNumber.addActionListener(this); bNumber.setMargin(new Insets(3, 3, 3, 3)); pDown.add(bNumber); bNumber = new JButton("2"); bNumber.setForeground(Color.blue); bNumber.addActionListener(this); bNumber.setMargin(new Insets(3, 3, 3, 3)); pDown.add(bNumber); bNumber = new JButton("3"); bNumber.setForeground(Color.blue); bNumber.addActionListener(this); bNumber.setMargin(new Insets(3, 3, 3, 3)); pDown.add(bNumber); bOperator = new JButton("-"); bOperator.setForeground(Color.red); bOperator.addActionListener(this); bOperator.setMargin(new Insets(3, 3, 3, 3)); pDown.add(bOperator); bOperator = new JButton("1/x"); bOperator.setForeground(Color.blue); bOperator.addActionListener(this); pDown.add(bOperator); bNumber = new JButton("0"); bNumber.setForeground(Color.blue); bNumber.addActionListener(this); bNumber.setMargin(new Insets(3, 3, 3, 3)); pDown.add(bNumber); bOperator = new JButton("+/-"); bOperator.setForeground(Color.blue); bOperator.addActionListener(this); bOperator.setMargin(new Insets(3, 3, 3, 3)); pDown.add(bOperator); bOperator = new JButton("."); bOperator.setForeground(Color.blue); bOperator.addActionListener(this); bOperator.setMargin(new Insets(3, 3, 3, 3)); pDown.add(bOperator); bOperator = new JButton("+"); bOperator.setForeground(Color.blue); bOperator.addActionListener(this); bOperator.setMargin(new Insets(3, 3, 3, 3)); pDown.add(bOperator); bOperator = new JButton("="); bOperator.setForeground(Color.blue); bOperator.addActionListener(this); bOperator.setMargin(new Insets(3, 3, 3, 3)); pDown.add(bOperator); pRight.add(pUp, BorderLayout.NORTH); pRight.add(pDown, BorderLayout.SOUTH); pBottom.add(pRight, BorderLayout.EAST); contentPane.add(pBottom, BorderLayout.SOUTH); f.setSize(new Dimension(320, 256)); f.setResizable(false); f.setVisible(true); f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); } /************************Java计算器 计算方法区***************************/ public void actionPerformed(ActionEvent e) { String s = e.getActionCommand(); String temps =null; if (s.equals("复制(C)")) { temps = tResult.getText().trim(); copyBoard.replace(0, copyBoard.length(), temps); mPaste.setEnabled(true); } else if (s.equals("粘贴(P)")) { tResult.setText(copyBoard.toString()); } else if(s.equals("保存(S)")){ if(operate!=null){ String lien = ""; OperateAction op = new OperateAction(); for(int i = 0 ; i < flag.size(); i++){ lien += flag.get(i).toString(); for(int j = 0; j < flag2.size() ; j++){ lien += flag2.get(j); flag2.remove(flag2.get(j)); break; } } flag = new ArrayList(); flag2 = new ArrayList(); i=0; System.out.println("=================/n"+lien +" = " + op1); //op.save(operate); } }else if(s.endsWith("Exit")){ System.exit(0); }else if(s.equals("标准显示")){ runView(0); }else if(s.equals("时间排序")){ runView(1); }else if(s.equals("结果排序")){ runView(2); }else if (s.equals("CE")) { //如果是CE则清除文本框 tResult.setText("0."); } else if (s.equals("BackSpace")) { if (!tResult.getText().trim().equals("0.")) { //如果文本框中有内容 if (str.length() != 1 && str.length() != 0) { tResult.setText(str.delete(str.length() - 1, str.length()) .toString()); } else { tResult.setText("0."); str.setLength(0); } } op2 = Double.parseDouble(tResult.getText().trim()); } else if (s.equals("C")) { //如果是C删除当前计算 tResult.setText("0."); op1 = op2 = 0; str.replace(0, str.length(), " "); preOp = currentOp = 0; } else if (s.equals("MC")) { //如果是MC则清除缓冲区 String temp = tResult.getText(); memory.replace(0, memory.length(), temp); bM.setText(" "); } else if (s.equals("MR")) { //如果按键为MR则恢复缓冲区的数到文本框 tResult.setText(memory.toString()); } else if (s.equals("MS")) { //如果按键为MS则将文本框的数存入缓冲区 String s1 = tResult.getText().trim(); memory.replace(0, memory.length(), s1); bM.setText(memory.toString()); } else if (s.equals("M+")) { //如果按键为M+则将文本框值与缓冲区的数相加但不显示结果 String temp1 = tResult.getText().trim(); double dtemp = Double.parseDouble(temp1); String temp2 = memory.toString(); dtemp += Double.parseDouble(temp2); temp1 = String.valueOf(dtemp); memory.replace(0, memory.length(), temp1); } else if (s.equals("1/x")) { //如果按键为1/x则将文本框中的数据为它的倒数 double dtemp = Double.parseDouble(tResult.getText().trim()); tResult.setText("" + 1 / dtemp); }else if (s.equals("+/-")){ String flag = tResult.getText(); if(flag.substring(0, 1).equals("-")) tResult.setText(flag.substring(1)); else tResult.setText("-"+tResult.getText().trim()); op1 = Double.parseDouble(tResult.getText().trim()); } else if (s.equals("sqrt")) { //如果按键为sqrt则将文本框中的内容求平方根 double dtemp = Double.parseDouble(tResult.getText().trim()); tResult.setText("" + Math.sqrt(dtemp)); } else if (s.equals("+")) { str.setLength(0); if (currentOp == 0) { preOp = currentOp = 1;//op1=op2;op2=0; tResult.setText("" + op1); } else { currentOp = preOp; preOp = 1; Op(); } } else if (s.equals("-")) { str.setLength(0); if (currentOp == 0) { preOp = currentOp = 2;//op1=op2;op2=0; tResult.setText("" + op1); } else { currentOp = preOp; preOp = 2; Op(); } } else if (s.equals("*"))//* { str.setLength(0); if (currentOp == 0) { preOp = currentOp = 3;//op1=op2;op2=1; tResult.setText("" + op1);//op1=op2; } else { currentOp = preOp; preOp = 3; Op(); } } else if (s.equals("/"))// / { str.setLength(0); if (currentOp == 0) { preOp = currentOp = 4;//op2=1; tResult.setText("" + op1);//op1=op2; } else { currentOp = preOp; preOp = 4; Op(); } }else if(s.equals("%")){ if(currentOp == 0 ){ str.setLength(0); if(currentOp == 0){ preOp = currentOp = 5; }else{ currentOp = preOp; preOp = 5; Op(); } } } else if (s.equals("="))// = { if (currentOp == 0) { str.setLength(0); tResult.setText("" + op2); } else { str.setLength(0); currentOp = preOp; Op(); currentOp = 0; op2 = 0; } } else if (s.equals(".")) { isDouble = true; if (tResult.getText().trim().equals("0.")) { str.setLength(0); tResult.setText((str.append("0" + s)).toString()); } else { tResult.setText((str.append(s)).toString()); } } else if (s.equals("0"))//如果选择的是"0"这个数字键 { if (tResult.getText().trim().equals("0.")) { } else { tResult.setText(str.append(s).toString()); op2 = Double.parseDouble(tResult.getText().trim()); } } else { tResult.setText(str.append(s).toString()); op2 = Double.parseDouble(tResult.getText().trim()); if (currentOp == 0) op1 = op2; } }//end actionPerformed private void runView(final int flag) { EventQueue.invokeLater(new Runnable(){ public void run() { try { ResultView view = new ResultView(flag); view.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); view.setVisible(true); new WindowAdapter() { public void windowClosing(WindowEvent e) { //正常退出Java虚拟机 System.exit(0); } }; } catch (SQLException e) { e.printStackTrace(); } } }); } /** * 判断操作 [currentOp 保存当前操作符号] */ private void Op() { //值代替 double op11 = op1; operate = new Operate(); switch (currentOp) { case 1: op1 = op1 + op2; flag2.add("+"); break; case 2: op1 = op1 - op2; flag2.add("-"); break; case 3: op1 = op1 * op2; flag2.add("*"); break; case 4: if(op2 == 0.0) { JOptionPane paen = new JOptionPane(); paen.showMessageDialog(null, "除数不能为0!,press enew input !!!","操作提示!", 3); operate = null; return; } op1 = op1 / op2; flag2.add("/"); break; case 5: op1 = op1 % op2; flag2.add("%"); break; } tResult.setText("" + op1); double op111 = op1-op11; if(i==0) { flag.add(op11); i=1; } flag.add(op111); } }

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


你可能感兴趣的:(Java图像界面案例)