简单计算器(Java实训)

简单计算器(Java实训)

简单计算器(Java实训)_第1张图片

实训目的:

掌握 Java GUI 开发中的布局管理和事件处理机制。

实训要求:

(1)要使用 java 的 GUI 设计出计算器界面。(2)通过界面
按钮,可以实现整数或浮点数的四则运算,并能将结果显示在界面中。(3)
计算可以有小数点,和正负整数的计算。(4)要有清零功能

新生第一次上路,借鉴了他人的代码,才造出这样一台计算器。
所以程序有少量BUG未解决:
(1)精度不够,在计算1.3*6这样的数据会出现精度问题。
(2)未能控制小数点以及加减乘除不能连着输入问题。
(3)等于后算出结果,进行下一次不同数值计算时,不能将上一个结果自动清除。必须使用CE健手动清除。

本程序在计算结果,调用了JS功能,未写核心算法,主要还是学习设计GUI界面。

简单计算器(Java实训)_第2张图片

package program8;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import javax.swing.JButton;
import javax.swing.JPanel;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Font;

public class Test {
	static String input = "";
	private JFrame frame;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Test window = new Test();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public Test() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		
		frame = new JFrame();
		frame.setTitle("\u8BA1\u7B97\u5668");
		frame.setBounds(100, 100, 400, 488);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JTextField textArea = new JTextField();
		textArea.setFont(new Font("Monospaced", Font.PLAIN, 30));
		textArea.setBounds(0, 0, 286, 96);
		
		JButton btnCe = new JButton("CE");
		btnCe.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String text=btnCe.getText();
				set_text(textArea,text);
			}
		});
		btnCe.setBounds(287, 0, 95, 94);
		frame.getContentPane().setLayout(null);
		frame.getContentPane().add(textArea);
		frame.getContentPane().add(btnCe);
		
		JPanel panel = new JPanel();
		panel.setBounds(0, 96, 382, 344);
		frame.getContentPane().add(panel);
		panel.setLayout(new GridLayout(4, 4));
		
		JButton btnNewButton = new JButton("7");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String text=btnNewButton.getText();
				set_text(textArea,text);
			}
		});
		panel.add(btnNewButton);
		
		JButton btnNewButton_1 = new JButton("8");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String text=btnNewButton_1.getText();
				set_text(textArea,text);
			}
		});
		panel.add(btnNewButton_1);
		
		JButton btnNewButton_3 = new JButton("9");
		btnNewButton_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String text=btnNewButton_3.getText();
				set_text(textArea,text);
			}
		});
		panel.add(btnNewButton_3);
		
		JButton btnNewButton_4 = new JButton("/");
		btnNewButton_4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String text=btnNewButton_4.getText();
				set_text(textArea,text);
			}
		});
		panel.add(btnNewButton_4);
		
		JButton btnNewButton_2 = new JButton("4");
		btnNewButton_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String text=btnNewButton_2.getText();
				set_text(textArea,text);
			}
		});
		panel.add(btnNewButton_2);
		
		JButton btnNewButton_6 = new JButton("5");
		btnNewButton_6.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String text=btnNewButton_6.getText();
				set_text(textArea,text);
			}
		});
		panel.add(btnNewButton_6);
		
		JButton btnNewButton_5 = new JButton("6");
		btnNewButton_5.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String text=btnNewButton_5.getText();
				set_text(textArea,text);
			}
		});
		panel.add(btnNewButton_5);
		
		JButton btnNewButton_8 = new JButton("*");
		btnNewButton_8.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String text=btnNewButton_8.getText();
				set_text(textArea,text);
			}
		});
		panel.add(btnNewButton_8);
		
		JButton btnNewButton_12 = new JButton("1");
		btnNewButton_12.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String text=btnNewButton_12.getText();
				set_text(textArea,text);
			}
		});
		panel.add(btnNewButton_12);
		
		JButton btnNewButton_9 = new JButton("2");
		btnNewButton_9.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String text=btnNewButton_9.getText();
				set_text(textArea,text);
			}
		});
		panel.add(btnNewButton_9);
		
		JButton btnNewButton_7 = new JButton("3");
		btnNewButton_7.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String text=btnNewButton_7.getText();
				set_text(textArea,text);
			}
		});
		panel.add(btnNewButton_7);
		
		JButton btnNewButton_13 = new JButton("-");
		btnNewButton_13.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String text=btnNewButton_13.getText();
				set_text(textArea,text);
			}
		});
		panel.add(btnNewButton_13);
		
		JButton btnNewButton_10 = new JButton(".");
		btnNewButton_10.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String text=btnNewButton_10.getText();
				set_text(textArea,text);
			}
		});
		panel.add(btnNewButton_10);
		
		JButton btnNewButton_11 = new JButton("0");
		btnNewButton_11.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String text=btnNewButton_11.getText();
				set_text(textArea,text);
			}
		});
		panel.add(btnNewButton_11);
		
		JButton btnNewButton_14 = new JButton("=");
		btnNewButton_14.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String text=btnNewButton_14.getText();
				set_text(textArea,text);
			}
		});
		panel.add(btnNewButton_14);
		
		JButton btnNewButton_15 = new JButton("+");
		btnNewButton_15.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String text=btnNewButton_15.getText();
				set_text(textArea,text);
			}
		});
		panel.add(btnNewButton_15);
	}
	public static void set_text(JTextField tf,String s){
		if(s.equals("=")){
			//进行字符串表达式的运算
			ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
	        ScriptEngine scriptEngine = scriptEngineManager.getEngineByName("nashorn");
			try{
				input = String.valueOf(scriptEngine.eval(input));
				tf.setText(input);
			}catch(ScriptException e){
				e.printStackTrace();
			}
		}
		else if(s.equals("CE")){
			input = "";
			tf.setText(input);
		}
		else{
			input = input + s;
			tf.setText(input);
		}
	}
}

结果演示:
简单计算器(Java实训)_第3张图片
简单计算器(Java实训)_第4张图片
简单计算器(Java实训)_第5张图片

你可能感兴趣的:(Java)