JAVA课程设计 计算器模拟程序 王华俊(201521123015)

1.团队课程设计博客链接

http://www.cnblogs.com/yuanj/p/7072137.html

2.个人负责模块或任务说明

GUI界面设计

各类之间拼接

3.自己的代码提交记录截图

4.自己负责模块或任务详细说明

编写计算器界面

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.text.DecimalFormat;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField; 	

public class Calculator {
    //计算器面板
private JFrame f = new JFrame("Calculator");
//输入面扳
private JPanel inputPanel = new JPanel();
//加减乘除面板
private JPanel operationPanel = new JPanel();
//数字面板
private JPanel buttonsPanel = new JPanel();
//输入数据文本框
private JTextField input = new JTextField(20);

//退格键
private JButton backspace = new JButton("←");
//清空
private JButton CE = new JButton("CE");
//删除
private JButton C = new JButton("C");
//取正负号
private JButton neg = new JButton("±");

//四则运算符号键
private JButton add = new JButton("+");
private JButton sub = new JButton("-");
private JButton mul = new JButton("*");
private JButton div = new JButton("/");

//小数点
private JButton decimal = new JButton(".");
//等号
private JButton equal = new JButton("=");

//数字键
private JButton zero = new JButton("0");
private JButton one = new JButton("1");
private JButton two = new JButton("2");
private JButton three = new JButton("3");
private JButton four = new JButton("4");
private JButton five = new JButton("5");
private JButton six = new JButton("6");
private JButton seven = new JButton("7");
private JButton eight = new JButton("8");
private JButton nine = new JButton("9");

private String num1 = "";//保存第一个运算数字
private String operator = "";//保存运算符号


public static void main(String[] args) {
	new Calculator();//new计算器实例
}

public Calculator(){
     //添加组件,布局
	inputPanel.add(input);
	f.add(inputPanel, BorderLayout.NORTH);
	
	operationPanel.add(backspace);
	operationPanel.add(CE);
	operationPanel.add(C);
	operationPanel.add(neg);
	operationPanel.setLayout(new GridLayout(1,4,3,3));
	f.add(operationPanel, BorderLayout.CENTER);

	buttonsPanel.add(seven);
	buttonsPanel.add(eight);
	buttonsPanel.add(nine);
	buttonsPanel.add(div);
	buttonsPanel.add(four);
	buttonsPanel.add(five);
	buttonsPanel.add(six);
	buttonsPanel.add(mul);
	buttonsPanel.add(one);
	buttonsPanel.add(two);
	buttonsPanel.add(three);
	buttonsPanel.add(sub);
	buttonsPanel.add(zero);
	buttonsPanel.add(decimal);
	buttonsPanel.add(equal);
	buttonsPanel.add(add);
	buttonsPanel.setLayout(new GridLayout(4,4,3,3));
	f.add(buttonsPanel, BorderLayout.SOUTH);
	
	f.setTitle("计算器");  
	f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//默认关闭操作
	f.setResizable(false);//窗体大小不可改变
	f.pack();
	f.setVisible(true);//设置窗口可见
	f.setLocationRelativeTo(null);//居中显示
	
	//注册各个组件监听事件
	backspace.addMouseListener(new OperationMouseListener());
	CE.addMouseListener(new OperationMouseListener());
	C.addMouseListener(new OperationMouseListener());
	decimal.addMouseListener(new OperationMouseListener());
	equal.addMouseListener(new OperationMouseListener());
	neg.addMouseListener(new OperationMouseListener());
	
	//注册四则运算鼠标监听
	add.addMouseListener(new CalcMouseListener());
	sub.addMouseListener(new CalcMouseListener());
	mul.addMouseListener(new CalcMouseListener());
	div.addMouseListener(new CalcMouseListener());
	
	//注册数字监听事件
	zero.addMouseListener(new NumberMouseListener());
	one.addMouseListener(new NumberMouseListener());
	two.addMouseListener(new NumberMouseListener());
	three.addMouseListener(new NumberMouseListener());
	four.addMouseListener(new NumberMouseListener());
	five.addMouseListener(new NumberMouseListener());
	six.addMouseListener(new NumberMouseListener());
	seven.addMouseListener(new NumberMouseListener());
	eight.addMouseListener(new NumberMouseListener());
	nine.addMouseListener(new NumberMouseListener());
			
}

5.课程设计感想

1.很多东西都是通过百度和求助别人获得和进行修改,很多功能还有待完善
2.课设让组员一起参与,大家需要一起讨论和研究,增加了学习氛围,也凝聚团队力量
3进行课程设计,有助于我们更好的运用所学的知识,将理论与实践相结合。

posted on 2017-06-24 00:48  王华俊  阅读(517)  评论(0编辑  收藏  举报