Java swing开发计算器

程序运行效果
在这里插入图片描述
直接上代码

主类:

/*
 * 程序主类
 * 制作时间:2019/6/21
 */


package 技能竞赛4;
import java.awt.ComponentOrientation;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.*;

public class calcuator extends JFrame{
	//判断输入次数
	public static int k=0;
	
	//程序主函数
	public static void main(String[] args){
		new  calcuator();
	}
	
	//程序内容函数
	public calcuator(){
		//窗口容器
		JPanel b=new JPanel();
		b.setLayout(null);
		add(b);
		
		//输入文本域
		JTextArea km=new  JTextArea();
		km.setBounds(5, 5, 680, 50);
		km.setFont(new Font("微软雅黑",Font.BOLD,30));
		b.add(km);
		
		/*
		 * 下面均属按钮
		 */
		
		JButton button1=new JButton("1");
		button1.setFont(new Font("微软雅黑",Font.BOLD,15));
		button1.setBounds(5, 70, 140, 40);
		b.add(button1);
		button1.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
			km.append("1");
			}
			
		});
		
		JButton button2=new JButton("2");
		button2.setFont(new Font("微软雅黑",Font.BOLD,15));
		button2.setBounds(185, 70, 140, 40);
		b.add(button2);
		button2.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
			km.append("2");
			}
			
		});
		
		
		JButton button3=new JButton("3");
		button3.setFont(new Font("微软雅黑",Font.BOLD,15));
		button3.setBounds(365, 70, 140, 40);
		b.add(button3);
		button3.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
			km.append("3");
			}
			
		});
		
		JButton buttona=new JButton("+");
		buttona.setFont(new Font("微软雅黑",Font.BOLD,15));
		buttona.setBounds(545, 70, 140, 40);
		b.add(buttona);
		buttona.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
			km.append("+");
			}
			
		});
		
		JButton button4=new JButton("4");
		button4.setFont(new Font("微软雅黑",Font.BOLD,15));
		button4.setBounds(5, 130, 140, 40);
		b.add(button4);
		button4.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
			km.append("4");
			}
			
		});
		
		JButton button5=new JButton("5");
		button5.setFont(new Font("微软雅黑",Font.BOLD,15));
		button5.setBounds(185, 130, 140, 40);
		b.add(button5);
		button5.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
			km.append("5");
			}
			
		});
		
		JButton button6=new JButton("6");
		button6.setFont(new Font("微软雅黑",Font.BOLD,15));
		button6.setBounds(365, 130, 140, 40);
		b.add(button6);
		button6.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
			km.append("6");
			}
			
		});
		
		JButton buttonb=new JButton("-");
		buttonb.setFont(new Font("微软雅黑",Font.BOLD,15));
		buttonb.setBounds(545, 130, 140, 40);
		b.add(buttonb);
		buttonb.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
			km.append("-");
			}
			
		});

		JButton button7=new JButton("7");
		button7.setFont(new Font("微软雅黑",Font.BOLD,15));
		button7.setBounds(5, 190, 140, 40);
		b.add(button7);
		button7.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
			km.append("7");
			}
			
		});
		
		JButton button8=new JButton("8");
		button8.setFont(new Font("微软雅黑",Font.BOLD,15));
		button8.setBounds(185, 190, 140, 40);
		b.add(button8);
		button8.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
			km.append("8");
			}
			
		});
		
		JButton button9=new JButton("9");
		button9.setFont(new Font("微软雅黑",Font.BOLD,15));
		button9.setBounds(365, 190, 140, 40);
		b.add(button9);
		button9.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
			km.append("9");
			}
			
		});
		
		JButton buttonc=new JButton("*");
		buttonc.setFont(new Font("微软雅黑",Font.BOLD,15));
		buttonc.setBounds(545, 190, 140, 40);
		b.add(buttonc);
		buttonc.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
			km.append("*");
			}
			
		});
		

		JButton button0=new JButton("0");
		button0.setFont(new Font("微软雅黑",Font.BOLD,15));
		button0.setBounds(5, 250, 140, 40);
		b.add(button0);
		button0.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
			km.append("0");
			}
			
		});
		
		JButton buttond=new JButton("(");
		buttond.setFont(new Font("微软雅黑",Font.BOLD,15));
		buttond.setBounds(185, 250, 140, 40);
		b.add(buttond);
		buttond.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
			km.append("(");
			}
			
		});
		
		JButton buttone=new JButton(")");
		buttone.setFont(new Font("微软雅黑",Font.BOLD,15));
		buttone.setBounds(365, 250, 140, 40);
		b.add(buttone);
		buttone.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
			km.append(")");
			}
			
		});
		
		JButton buttonf=new JButton("/");
		buttonf.setFont(new Font("微软雅黑",Font.BOLD,15));
		buttonf.setBounds(545, 250, 140, 40);
		b.add(buttonf);
		buttonf.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
			km.append("/");
			}
			
		});

		JButton buttong=new JButton("计算结果");
		buttong.setFont(new Font("微软雅黑",Font.BOLD,15));
		buttong.setBounds(480, 310, 200, 40);
		b.add(buttong);
		buttong.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
			CalerReturn.content=km.getText();
			CalerReturn a=new CalerReturn();
			Object u=a.CalerReturns();
			km.setText(String.valueOf(u));		
			}
			
		});
		

		
		//窗口属性
		new JFrame();
		this.setLocationRelativeTo(null);
		this.setTitle("程序设计比赛");
		this.setResizable(false);
		this.setSize(700, 400);
		this.setVisible(true);
	}
	
		
		
	

}

计算类:

/*
 * 计算结果
 */

package 技能竞赛4;
import java.util.*;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import javax.swing.JOptionPane;

public class CalerReturn {
	
	//获取计算内容
	public static String content;
	
	//计算结果
	public static Object result;
	

	public static Object CalerReturns(){
	
		ScriptEngineManager manager = new ScriptEngineManager();
		ScriptEngine engine = manager.getEngineByName("js");
		try {
			Object results = engine.eval(content);
			result=results;
		} catch (ScriptException e) {
			// TODO Auto-generated catch block
			JOptionPane.showMessageDialog(null,"你输入的字符可能不是运算符或中文!");
		}
		
		
		return result;
		
	}
}

发布了15 篇原创文章 · 获赞 6 · 访问量 2802

猜你喜欢

转载自blog.csdn.net/qq_41743240/article/details/105609604