java课程设计(简易计算器)源代码 JAVA 源代码有解析 免费分享

标题:java课程设计(简易计算器)源代码 JAVA 源代码有解析 免费分享

先言

这是我挺久以前课程设计java写的计算器,只能实现基本的功能,我现在分享出来希望能帮助到别人。写得其实挺一般的,见谅见谅。有课程设计任务的学弟学妹也是写计算器的可以看看参考参考。源代码里我写有挺多解析的,能帮助理解。

功能描述:

参考 Windows操作系统提供的计算器设计一个实用的计算器,要求除了具有普通的计算功能外,还具有保存计算过程的功能。
①单击计算器上的数字按钮(0、1、2、3、4、5、6、7、8、9)可以设置参与计算的运算数。
②单击计算器上的运算符按钮(+、—、*、/)可以选择运算符号。
③单击计算器上的函数按钮可以计算出相应的函数值。
④单击计算器上的等号(=)按钮显示计算结果。
⑤在一个文本框中显示当前的计算过程,在一个文本区中显示以往的计算过程。
⑥单击“保存”按钮可以将文本区中显示的全部计算过程保存到文件:单击“复制”按钮可以将文本区中选中的文本复制到剪贴板单击“清除”技钮可以清除文本区中的全部内容。

注意事项:

一.
总体包含11个Java源文件。Work.java ; ShuziButton.java ; SymbolButton.java ;
OperateNumber.java ; OperateSymbol.java ; OperateBack.java ; OperateClear.java ;OperateEquality.java ;OperateDot.java ;
OperateZhengFu.java ; OperateSin.java
二.各个源文件实现功能

  1. Work.java负责计算器主窗口的创建。含有main方法。从该类开始执行。同样也负责(退格,=,归零,. ,+/-,sin)按钮创建。
  2. SymbolButton.java 负责(+,-,*,/)四个运算符号按钮的创建。
  3. ShuziButton.java 负责0-9数字按钮的创建。
  4. OperateNumber.java 负责用户点击数字(0-9)按钮时的事件处理。
  5. OperateSymbol.java 负责用户点击(+,-,*,/)按钮时的事件处理。
  6. OperateBack.java 负责用户点击(退格)按钮时的事件处理。
  7. OperatClear.java 负责用户点击(归零)按钮时的事件处理。
  8. OperateEquality 负责用户点击(=)按钮十五的事件处理。
  9. OperateDot.java 负责用户点击( . )按钮时的事件处理。
  10. OperateZhengFu.java 负责用户点击(+/-)按钮时的事件处理。
  11. OperateSin.java.java 负责用户点击(Sin)按钮时的事件处理。

三:涉及的知识点
12. 基本数据类型与数组。
13. 运算符,表达式和语句。
14. 类与对象。
15. 子类与继承。
16. 接口与实现。
17. 内部类与异常类。
18. 常用String类。
19. 组件及事件处理。
20. 输入与输出流。
21. Linked List类的基本用法。

直接先看效果:

效果

全部源文件都是放在 keshe这个包里面,直接用的话建这个包再复制源文件代码进去就行或者自己重新写 package代码用自己建的包名。

源码:

1.work.java

 package keshe;

import java.util.LinkedList;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

public class Work extends JFrame implements ActionListener {
   JButton sin,Backspace,Equals,Zero,Flex,zhengfu;
   JButton saveButton,clearButton,copyButton;
   JTextField resultDisplay;  //当前输入与运算结果显示。
   JTextField processShow;   //  当前计算过程显示。
   JTextArea  stepShow;     //计算步骤显示。
   LinkedList<String>list;  //存放第一个数,操作符号与第二个数。
   ShuziButton shuziButton[];  
   SymbolButton symbolButton[];
   OperateNumber operateNumber;    //处理ActionEvent事件。
   OperateSymbol operateSymbol;    
   OperateBack   operateBack;       
   OperateClear  operateClear;     
   OperateEquality operateEquality;  
   OperateDot   operateDot;         
   OperateZhengFu operateZhengFu;
   OperateSin    operateSin;     
   public Work() {    //构造方法
	   setTitle("五彩缤纷的计算——XXX");
	   setVisible(true);    //窗口可见。
	   setBounds(120,60,600,300);     //窗口的初始位置与长与宽;
	   setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//点击关闭后结束窗口所在的应用程序。
	   JPanel panelZuo,panelYou;  //面板
	   list=new LinkedList<String>();
	   resultDisplay=new JTextField(10);
	   resultDisplay.setHorizontalAlignment(JTextField.RIGHT);  //对齐方式
	   resultDisplay.setFont(new Font("TimesRoman",Font.BOLD,16));//字体样式,字体格式 ,字体大小
	   resultDisplay.setForeground(Color.green);    //字体颜色
	   resultDisplay.setBackground(Color.white); //背景色
	   resultDisplay.setEditable(false); //选项不可用
	   resultDisplay.setBorder(new SoftBevelBorder(BevelBorder.LOWERED));//显示半个边框
	   processShow=new JTextField();
	   processShow.setHorizontalAlignment(JTextField.CENTER);  //对齐方式
	   processShow.setFont(new Font("Arial",Font.BOLD,16));//字体样式,字体格式 ,字体大小
	   processShow.setBackground(Color.green);
	   processShow.setEditable(false); //选项不可用
	   stepShow=new JTextArea();
	   stepShow.setEditable(false); //选项不可用
	   stepShow.setFont(new Font("宋体",Font.PLAIN,16));
	   stepShow.setForeground(Color.blue);   //字体颜色
	   shuziButton=new ShuziButton[10];
	   operateNumber = new OperateNumber(list,resultDisplay,processShow);
	   for(int i=0;i<=9;i++) {
		   shuziButton[i]=new ShuziButton(i); 
		   shuziButton[i].setFont(new Font("Arial",Font.BOLD,20)); //数字样式,格式,大小
		   shuziButton[i].addActionListener(operateNumber);  //shuziButton将operateNumber注册为自己的监视器		   
	   }
	   symbolButton=new SymbolButton[4];
	   operateSymbol=new OperateSymbol(list,resultDisplay,processShow,stepShow);
	   String fuhao1[]= {"+","-","*","/"};
	   for(int i=0;i<4;i++) {
		   symbolButton[i]=new SymbolButton(fuhao1[i]);
		   symbolButton[i].setFont(new Font("Arial",Font.BOLD,20));
		   symbolButton[i].addActionListener(operateSymbol);  //symbolButton将operateSymbol注册为自己的监视器	
	   }
	   Flex=new JButton(".");
	   operateDot=new OperateDot(list,resultDisplay,processShow);
	   Flex.addActionListener(operateDot);
	   zhengfu=new JButton("+/-");
	   operateZhengFu=new OperateZhengFu(list,resultDisplay,processShow);
	   zhengfu.addActionListener(operateZhengFu);
	   Zero=new JButton("归零");
	   operateClear=new OperateClear(list,resultDisplay,processShow);
	   Zero.addActionListener(operateClear);
	   Backspace=new JButton("退格");
	   operateBack=new OperateBack(list,resultDisplay,processShow);
	   Backspace.addActionListener(operateBack);
	   Equals=new JButton("=");
	   operateEquality=new OperateEquality(list,resultDisplay,processShow,stepShow); 
	   Equals.addActionListener(operateEquality);
	   sin=new JButton("sin");
	   operateSin=new OperateSin(list,resultDisplay,processShow,stepShow);
	   sin.addActionListener(operateSin);
	   Zero.setForeground(Color.green);          //颜色
	   Backspace.setForeground(Color.cyan);
	   Equals.setForeground(Color.magenta);
	   Flex.setForeground(Color.gray);
	   zhengfu.setForeground(Color.red);
	   sin.setForeground(Color.pink);
	   panelZuo=new JPanel();
	   panelYou=new JPanel();   
	   panelZuo.setLayout(new BorderLayout());        //panelZuo使用BorderLayout布局。
	   JPanel centerZuo=new JPanel();         //建一个放在panelZuo中间部分的centerZuo容器放数字与符号。
	   panelZuo.add(resultDisplay,BorderLayout.NORTH);     //运算结果放在panelZuo容器的北边。   
	   panelZuo.add(centerZuo,BorderLayout.CENTER);
	   centerZuo.setLayout(new GridLayout(5,4));            //centerZuo使用GridLayout布局;
	   centerZuo.add(symbolButton[0]);
	   centerZuo.add(symbolButton[1]);
	   centerZuo.add(symbolButton[2]);
	   centerZuo.add(symbolButton[3]);
	   centerZuo.add(shuziButton[1]);
	   centerZuo.add(shuziButton[2]);
	   centerZuo.add(shuziButton[3]);
	   centerZuo.add(zhengfu);
	   centerZuo.add(shuziButton[4]);
	   centerZuo.add(shuziButton[5]);
	   centerZuo.add(shuziButton[6]);
	   centerZuo.add(sin);
	   centerZuo.add(shuziButton[7]);
	   centerZuo.add(shuziButton[8]);
	   centerZuo.add(shuziButton[9]);
	   centerZuo.add(Backspace);
	   centerZuo.add(shuziButton[0]);
	   centerZuo.add(Flex);
	   centerZuo.add(Equals);
	   centerZuo.add(Zero);
	   panelYou.setLayout(new BorderLayout());      //panelYou使用BorderLayout布局。
	   panelYou.add(processShow,BorderLayout.NORTH);          //  运算过程放在panelYou的北边.
	   saveButton=new JButton("保存");
	   copyButton=new JButton("复制");
	   clearButton=new JButton("清除");
	   saveButton.setForeground(Color.blue);    //颜色
	   copyButton.setForeground(Color.red);
	   clearButton.setForeground(Color.gray);
	   saveButton.setToolTipText("保存计算过程到文件里");          //设置鼠标停留在saveButton上时显示的信息;
	   copyButton.setToolTipText("复制选中计算过程");       //设置鼠标停留在copyButton上时显示的信息;
	   clearButton.setToolTipText("清除计算过程");         //设置鼠标停留在clearButton上时显示的信息;
	   saveButton.addActionListener(this);
	   clearButton.addActionListener(this);
	   copyButton.addActionListener(this);
	   panelYou.add(new JScrollPane(stepShow),BorderLayout.CENTER);//滚动窗格
	   JPanel  southYou=new JPanel();                 //建一个放在panelYou南边的southYou容器存放3个文字按钮。
	   southYou.add(saveButton);   //放入按钮
	   southYou.add(copyButton);
	   southYou.add(clearButton);
	   panelYou.add(southYou,BorderLayout.SOUTH);//将southYou放在panelYou南边。
	   JSplitPane split=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,panelZuo,panelYou);//界面一分为二;
	   add(split,BorderLayout.CENTER);
	   validate();
      }
	  public void actionPerformed(ActionEvent e) {
		  if(e.getSource()==copyButton) {     //如果按下复制按钮
			  stepShow.copy();        //实现复制
		  }
		  if(e.getSource()==clearButton) {  //如果按下清除按钮
			  stepShow.setText(null);   //实现清除
		  }
		  if(e.getSource()==saveButton){   //如果按下保存按钮
			  JFileChooser chooser=new JFileChooser();  //文件选择器
			  int state = chooser.showSaveDialog(null);  //显示文件保存对话框
			  File file = chooser.getSelectedFile();  //输入要保存的文件名;
			  if(file!=null&&state==JFileChooser.APPROVE_OPTION) {   //若选好了文件.
				  try {
					  String baoCun=stepShow.getText(); //将运算步骤文本赋值给baoCun;
					  StringReader read = new StringReader(baoCun); //生成字符流。
					  BufferedReader in=new BufferedReader(read);  //生成缓冲输入流。
					  FileWriter out=new FileWriter(file); //根据选好的文件,生成FileWriter对象;
					  BufferedWriter out1=new BufferedWriter(out);  //生成缓冲输出流;
					  String str=null;
					  while((str=in.readLine())!=null) {  //判断是否有字符
						  out1.write(str);  //把内容写到文件中
						  out1.newLine();    //新的一行					  
					  }
					  in.close();  //关闭文件;
					  out1.close();	  
				  }catch(IOException el){
					  
				  }
				  
			  }		  			  
		  }
		  
	  } 
	public static void main(String args[]) {
		 new Work();
	 }
   }
   

2.OperateBack.java

package keshe;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;
import javax.swing.JTextField;

public class OperateBack implements ActionListener{
	   LinkedList<String>  list;
	   JTextField resultDisplay;   //当前输入与运算结果显示
	   JTextField processShow;    //显示当前计算过程
	   OperateBack(LinkedList<String> list1,JTextField one,JTextField two) {      //构造方法
		   list=list1;
		   resultDisplay=one;
		   processShow=two;   
	   }
	   public void actionPerformed(ActionEvent e) {
		   if(list.size()==1) {
			   String shu =(String) list.getFirst();  //获取第一个元素
			   if(shu.length()>=1) {   //判断第一个运算数有几位数
				   shu=shu.substring(0,shu.length()-1);  //提取字符串(不要最后一位数)
				   list.set(0, shu);     //替换				   
				   resultDisplay.setText(""+shu); 
				   processShow.setText(""+shu);				   
			   }else {       //只有一位数
				   list.removeLast();      //移除第一位数
				   resultDisplay.setText("0"); 
				   processShow.setText("0");				   
			   }
		   }else if(list.size()==3) {
			   String shu2 =(String) list.getLast();  //获取第二个运算数字
			   if(shu2.length()>=1) {   //判断第一个运算数有几位数
				   shu2=shu2.substring(0,shu2.length()-1);  //提取字符串(不要最后一位数)
				   list.set(2, shu2);     //替换				   
				   resultDisplay.setText(""+shu2); 
				   processShow.setText(""+shu2);				   
			   }else {       //只有一位数
				   list.removeLast();      //移除第二运算位数
				   resultDisplay.setText("0"); 
				   processShow.setText("0");				   
			   }
		   }
	   }

}

3.OperateClear.java

package keshe;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;
import javax.swing.JTextField;

public class OperateClear implements ActionListener {
	LinkedList<String>  list;
	   JTextField resultDisplay;    //当前输入与运算结果显示
	   JTextField processShow;        //显示当前计算过程
	   OperateClear(LinkedList<String> list1,JTextField one,JTextField two) {      //构造方法
		   list=list1;
		   resultDisplay=one;
		   processShow=two;   
	   }
	   public void actionPerformed(ActionEvent e) {
		   resultDisplay.setText("0");
		   list.clear();               //全部清除
		   processShow.setText(null);
		   
		   
	   }
}

4.OperateDot.java

package keshe;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;
import javax.swing.JTextField;
public class OperateDot implements ActionListener{
	   LinkedList<String>  list;
	   JTextField resultDisplay;      //当前输入与运算结果显示
	   JTextField processShow;              //显示当前计算过程
	   OperateDot(LinkedList<String> list1,JTextField one,JTextField two) {      //构造方法
		   list=list1;
		   resultDisplay=one;
		   processShow=two; 
	   }
	   public void actionPerformed(ActionEvent e) {
		   String dot = e.getActionCommand();  //获取小数点
		   if(list.size()==1)  {
			   String a = list.getFirst();    //获取第一个元素
			   String b = null ;
			   if(a.indexOf(dot)==-1) {     //判断是否出现dot
				   b=a.concat(dot);    //把小数点拼接在数字后面
				   list.set(0, b);         //替换
			   }else {
				   b=a;
			   }
			   resultDisplay.setText(b);
			   processShow.setText(""+list.get(0));
		    }else if (list.size()==3 ) {
		       String a = list.getLast();    //获取第3个元素
			   String b = null ;
			   if(a.indexOf(dot)==-1) {     //判断是否出现dot
				   b=a.concat(dot);    //把小数点拼接在数字后面
				   list.set(2, b);         //替换
			   }else {
				    b=a;
				   }
			   resultDisplay.setText(b);
			   processShow.setText(""+list.get(0)+""+list.get(1)+""+list.get(2));	
		    }
	   }

}

5.OperateEquality.java

package keshe;
import  java.awt.event.ActionEvent;
import  java.awt.event.ActionListener;
import java.util.LinkedList;
import javax.swing.JTextField;
import javax.swing.JTextArea;
public class OperateEquality implements ActionListener{
	   LinkedList<String>  list;
	   JTextField resultDisplay;   //当前输入与运算结果显示
	   JTextField processShow;     //显示当前计算过程
	   JTextArea stepShow;       // 显示计算步骤整体过程
	   OperateEquality(LinkedList<String> list1,JTextField one,JTextField two,JTextArea three) {      //构造方法
		   list=list1;
		   resultDisplay=one;
		   processShow=two; 
		   stepShow=three;
	   }
	   public void actionPerformed(ActionEvent e) {
		   if(list.size()==1)  {
			   String a = list.getFirst();    //获取第一个元素
			   resultDisplay.setText(a);
			   processShow.setText(list.get(0));
		    }
		   if(list.size()==2)  {
			   String a = list.getFirst();    //获取第一个元素
			   
			   resultDisplay.setText(a);
			   processShow.setText(list.get(0));
		    }
		    else if(list.size()==3) { 
		    	String a = list.getFirst();  //获取第一个元素
		    	String b = list.get(1);      //获取第二个元素
		    	String c = list.getLast();   //获取最后一个元素
		    	try {
		    		double d1 = Double.parseDouble(a);
		    		double d2 = Double.parseDouble(c);   //数据类型转换
		    		double result = 0;
		    		if(b.equals("+")) {
		    			result = d1 + d2 ;
		    		}else if(b.equals("-")) {
		    			result = d1 - d2;
		    		}else if(b.equals("*")) {
		    			result = d1*d2;
		    		}else if(b.equals("/")) {
		    			result = d1/d2;
		    		}
		    		String step = ""+a+""+b+""+c+"="+result;
		    		resultDisplay.setText(""+result);
		    		processShow.setText(step);
		    		stepShow.append(""+step+"\n");
		    		list.set(0, ""+result);   //将第一个元素为结果
		    		list.removeLast();   //清除第二个运算数
		    		list.removeLast();      //清除符号
		    	}catch (Exception ee) {
		    		
		      }			   
		    }
	   }
}

6.OperateNumber.java

package keshe;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;
import javax.swing.JTextField;
public class OperateNumber implements ActionListener {
   LinkedList<String>  list;
   JTextField resultDisplay;        //当前输入与运算结果显示
   JTextField processShow;         //显示当前计算过程
   OperateNumber(LinkedList<String> list1,JTextField one,JTextField two) {      //构造方法
	   list=list1;
	   resultDisplay=one;
	   processShow=two;   
   }
	public void actionPerformed(ActionEvent e) {
		ShuziButton s=(ShuziButton)e.getSource();  //获得点击的ShuziButton
		if(list.size()==0) {    //如果链表个数为零
			int number=s.getNumber();    
			list.add(""+number);  //向list添加元素
			resultDisplay.setText(""+number);  //将resultDisplay对象上的文字设置成number
		}else if(list.size()==1) {
			int number=s.getNumber();
			String a = list.getFirst();	//取得链表第一个元素
			String b = a.concat(""+number);  //字符串拼接。
			list.set(0, b);     //替换指定下标元素;
			resultDisplay.setText(b);
			processShow.setText(""+list.get(0));        //以上为储存输入的第一个运算数。
		}else if(list.size()==2) {
			int number=s.getNumber();
			list.add(""+number);    //向列表添加新元素;
			resultDisplay.setText(""+number);
			processShow.setText(""+list.get(0)+""+list.get(1)+""+list.get(2));
		}else if(list.size()==3) {
			int number=s.getNumber();
			String a = list.getFirst();	//取得链表第三个元素
			String b = a.concat(""+number);  //字符串拼接。
			list.set(2, b);     //替换指定下标元素;
			resultDisplay.setText(b);
			processShow.setText(""+list.get(0)+""+list.get(1)+""+list.get(2));        //以上为储存输入的第二个运算数
		}		
	}
	
}

7.OperateSin.java

package keshe;
import  java.awt.event.ActionEvent;
import  java.awt.event.ActionListener;
import java.util.LinkedList;
import javax.swing.JTextField;
import javax.swing.JTextArea;
public class OperateSin implements ActionListener{
	   LinkedList<String>  list;
	   JTextField resultDisplay;      //当前输入与运算结果显示
	   JTextField processShow;    //显示当前计算过程
	   JTextArea stepShow;        // 显示计算步骤整体过程
	   OperateSin(LinkedList<String> list1,JTextField one,JTextField two,JTextArea three) {      //构造方法
		   list=list1;
		   resultDisplay=one;
		   processShow=two; 
		   stepShow=three;
	   }
	   public void actionPerformed(ActionEvent e) {
		   if(list.size()==1||list.size()==2) {
			   String a = list.getFirst();     //获取第一个元素
			   try {
				   double b = Double.parseDouble(a);  //数据类型转换
				   double c = Math.sin(b);
				   String d = String.valueOf(c) ;  //数据类型转换
				   list.set(0, d);   //替换第一个元素
				   String step = "sin("+a+")="+c;   //整个过程
				   resultDisplay.setText(d);
				   processShow.setText(step);
				   stepShow.append(""+step+"\n");
			     }catch(Exception ee) {
		      }	 
			 }else if(list.size()==3) {
			    String a = list.getLast();     //获取最后一个元素
			    try {
				   double b = Double.parseDouble(a);  //数据类型转换
				   double c = Math.sin(b);
				   String d = String.valueOf(c) ;  //数据类型转换
				   list.set(0, d);   //替换第一个元素
				   String step = "sin("+a+")="+c;   //整个过程
				   resultDisplay.setText(d);
				   processShow.setText(step);
				   stepShow.append(""+step+"\n");
				   list.removeLast();   //移掉第2个运算数
				   list.removeLast();	  // 移掉运算符号  
			     }catch(Exception ee) {		
			    	 
			      }
			    }
			 }
		   }
	   

8.OperateSymbol.java

package keshe;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class OperateSymbol implements ActionListener{
	   LinkedList<String>  list;
	   JTextField resultDisplay;        //当前输入与运算结果显示
	   JTextField processShow;        //显示当前计算过程
	   JTextArea   stepShow;             // 显示计算步骤整体过程
	   OperateSymbol(LinkedList<String> list1,JTextField one,JTextField two,JTextArea three) {      //构造方法
		   list=list1;
		   resultDisplay=one;
		   processShow=two; 
		   stepShow=three;
	   }
	   public void actionPerformed(ActionEvent e) {
		   SymbolButton a=(SymbolButton)e.getSource();
		   if(list.size()==1) {
			   String fuhao = a.getFuHao1();
			   list.add(fuhao);           //添加元素
			   processShow.setText(""+list.get(0)+""+list.get(1));	
		   }else if(list.size()==2) {
			   String fuhao = a.getFuHao1();
			   list.set(1, fuhao);        //替换指定下标元素;
			   processShow.setText(""+list.get(0)+""+list.get(1));  //以上为储存第一个运算符号
		   }else if(list.size()==3) {
			String num1 = list.getFirst();   //获取第一个数字
			String num2 = list.getLast();    //获取第二个数字
			String fuHao = list.get(1);     // 获取运算符号
			String processStep = num1+""+fuHao+""+num2;
			try {
				double n1 = Double.parseDouble(num1);   //字符串转换数据 
				double n2 = Double.parseDouble(num2);  
				double result = 0 ;
		        if(fuHao.equals("+")) {   
		        	result = n1+n2;
		        }else if(fuHao.equals("-")) {
		        	result = n1-n2;
		        }else if(fuHao.equals("*")) {
		        	result = n1*n2;
		        }else if(fuHao.equals("/")) {
		        	result = n1/n2;
		        }
		        String fuhao = a.getFuHao1();
		        list.clear();  //清除
		        list.add(""+result);  //把计算结果换成第一个运算数
		        list.add(fuhao);
		        String step = processStep+"="+""+list.get(1);
		        resultDisplay.setText(""+result);      
		 	    processShow.setText(step);
		 	    stepShow.append(""+processStep+"="+result+"\n");
		        
		      }catch(Exception ee) {				
			}			
		 }
	}
}

9.OperateZhengFu.java

package keshe;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;
import javax.swing.JTextField;
public class OperateZhengFu implements ActionListener{
	   LinkedList<String>  list;
	   JTextField resultDisplay;   //当前输入与运算结果显示
	   JTextField processShow;      //显示当前计算过程
	   OperateZhengFu(LinkedList<String> list1,JTextField one,JTextField two) {      //构造方法
		   list=list1;
		   resultDisplay=one;
		   processShow=two; 
	   }
	   public void actionPerformed(ActionEvent e) {
		   if(list.size()==1) {
			   String a = list.getFirst();  //  获取第一个元素;
			   try {
				   double b = Double.parseDouble(a);   //数据类型转换;
				   b=-1*b;   //换成异号
				   String c = String.valueOf(b);   //数据类型又换成Sring型
				   list.set(0, c);
				   resultDisplay.setText(c);
				   processShow.setText(""+list.get(0));
			   }catch(Exception  ee) {
		    }
		   }else if(list.size()==3) {
			   String a = list.getFirst();  //  获取第一个元素;
			   try {
				   double b = Double.parseDouble(a);   //数据类型转换;
				   b=-1*b;   //换成异号
				   String c = String.valueOf(b);   //数据类型又换成Sring型
				   list.set(2, c);
				   resultDisplay.setText(c);
				   processShow.setText(""+list.get(0)+""+list.get(1)+""+list.get(2));
			   }catch(Exception  ee) {
			   }
		   }
	   }
}

10.ShuziButton.java

package keshe;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ShuziButton extends JButton{
	int number;
	public ShuziButton(int number) {
		super(""+number);
		this.number=number;
		setForeground(Color.orange);
	}
    public int getNumber() {
    	return number;       //返回数字。
    }
}

11.SymbolButton.java

package keshe;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SymbolButton extends JButton {
	String fuHao1;
	public SymbolButton(String s) {
		super(s);
		fuHao1=s;
		setForeground(Color.blue);
	}
    public String getFuHao1() {
    	return fuHao1;  // 返回符号
    }
}

总结

全部源码就上面全部了,写完的话高分可能拿不到,但是过是没问题的。有要UML图的可以留言~~

要UML图的可以私信我,发邮箱。急的也可以去这下载:https://download.csdn.net/download/luo1831251387/15770017

  • 57
    点赞
  • 241
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 78
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 78
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

北极光之夜。

谢谢~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值