简易计算器---JAVA

实验题目:基于Java Swing的应用程序实验

实验目的:学习使用Eclipse编写运行Java程序,综合应用Java的SWING、输入输出流和数据库的相关操作

实验内容: 设计并实现一个GUI简易计算器

要求:

1)至少实现“+”“-”“*”“/”“%”运算

2)调用Math类的相关方法,至少实现对数运算、幂运算;

3利用菜单条、菜单项、菜单至少实现数值复制粘贴剪切退出功能,并添加相应的快捷键分别为“Ctrl+c”“Ctrl+v”“Ctrl+x”“Ctrl+e”

4检查数据合法性,若输入数据不合法,则调用文本框类的相关方法,弹出窗口提示用户进行正确输入;

5体现自己的工作量界面至少显示作者的班级、姓学号


具体代码:

Computer.java

public class computer {
    public static void main(String args[]){
   	 WindowOperation win=new WindowOperation();
   	 win.setBounds(700,300,500,400);
   	 win.setTitle("简易计算器");
    }
}

ComputerListener.java

import java.awt.event.*;
import  javax.swing.*;
import java.awt.*;
	
public  class ComputerListener implements ActionListener {
    JTextField  inputNumberOne,inputNumberTwo;
    JTextArea textShow;
    String fuhao=" ";

    public void setJTextFieldOne(JTextField t){
 	   inputNumberOne =t;
    }
    public void setJTextFieldTwo(JTextField t){
 	   inputNumberTwo =t;
    }
    public void setJTextArea(JTextArea area){
 	   textShow =area;
    }
    public void setFuhao(String s){
 	   fuhao=s;
    }
    static public double log(double value, double base) {
 	   double a=0;
 	   a=Math.log(value)/Math.log(base);  	   
 	   return a;
 	   }
    public void actionPerformed(ActionEvent e){
     try{
 	       double number1;
 	       double number2;
 		   double result=0;
 		   number1=Double.parseDouble(inputNumberOne.getText());
 		   number2=Double.parseDouble(inputNumberTwo.getText());
 		   if(fuhao.equals("+")){
 			   result=number1+number2;
 		   }
 		   else if(fuhao.equals("-")){
 			   result=number1-number2;  
 			   }
 		   else if(fuhao.equals("*")){
 			   result=number1*number2;  
 			   }
 		   else if(fuhao.equals("/")){
 			   result=number1/number2;  
 			   }
 		   else if(fuhao.equals("%")){
 			   result=number1%number2;  
 			   }
 		   else if(fuhao.equals("幂运算")){
 			   result=Math.pow(number1, number2);
 		   }
 		   else if(fuhao.equals("对数运算")){
 			   result=log(number1,number2);
 		   }
            
             if(fuhao.equals(" ")){
         	   JOptionPane.showMessageDialog(null,"请输入运算符号","警告对话框",JOptionPane.WARNING_MESSAGE);
            }
            else{
 		   textShow.append(number1+" "+fuhao+" "+number2+"="+result+"\n");
            }
     }
     catch(Exception exp){
     	JOptionPane.showMessageDialog(null,"请输入数字","警告对话框",JOptionPane.WARNING_MESSAGE);
     }
    }
}

ConcreteMediator.java

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


public class ConcreteMediator {
     JMenu menu;
     JMenuItem copyItem,cutItem,pasteItem;
     JTextArea text;
     public void openMenu(){
    	 Clipboard clipboard =text.getToolkit().getSystemClipboard();
    	 String str =text.getSelectedText();
    	 if(str==null){
    		 copyItem.setEnabled(false);
    		 cutItem.setEnabled(false);
    	 }
    	 else{
    		 copyItem.setEnabled(true);
    		 cutItem.setEnabled(true);
    	 }
    	 boolean boo =clipboard.isDataFlavorAvailable(DataFlavor.stringFlavor);
    	 if(boo){
    		 pasteItem.setEnabled(true);
    	 }
     }
     public void paste(){
    	 text.paste();
     }
     public void copy(){
    	 text.copy();
     }
     public void cut(){
    	 text.cut();
     }
     public void registerMenu(JMenu menu){
    	 this.menu=menu;
     }
     public void registerPasteItem(JMenuItem item){
    	 pasteItem=item;
     }
     public void registerCopyItem(JMenuItem item){
    	 copyItem=item;
    	 copyItem.setEnabled(false);
     }
     public void registerCutItem(JMenuItem item){
    	 cutItem =item;
    	 cutItem.setEnabled(false);
     }
     public void registerText(JTextArea text){
    	 this.text=text;
     }
}

WindowOperation.java

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.border.EmptyBorder;

import java.awt.FlowLayout;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
public class WindowOperation extends JFrame {
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	JTextField inputNumberOne,inputNumberTwo;
    JComboBoxchoiceFuhao;
    JTextArea textShow;
    JButton button;
    JMenuBar menubar;
    JMenu menu;
    JMenuItem copyItem,cutItem,pasteItem,quitItem;
    OperatorListener operator;
    ComputerListener computer;
    private JTextField textField;
    public WindowOperation(){
    	init();
    	setVisible(true);
    	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    
    void init(){
    	getContentPane().setLayout(new FlowLayout());
    	inputNumberOne =new JTextField(5);
    	inputNumberTwo =new JTextField(5);
    	choiceFuhao =new JComboBox();
    	button = new JButton("计算");
    	choiceFuhao.addItem("选择运算符号");
    	String []a={"+","-","*","/","%","幂运算","对数运算"};
    	for(int i=0; ichoice;
      ComputerListener workTogether;
      public void setJComboBox(JComboBox box){
    	  choice=box;
      }
      public void setWorkTogether(ComputerListener computer){
    	  workTogether =computer;
    	  
      }
      public void itemStateChanged(ItemEvent e){
    	  String fuhao =choice.getSelectedItem().toString();
    	  workTogether.setFuhao(fuhao);
      }
}

实现效果:

简易计算器---JAVA_第1张图片

简易计算器---JAVA_第2张图片

简易计算器---JAVA_第3张图片


你可能感兴趣的:(JAVA)