博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
用Java设计简易的计算器
阅读量:4322 次
发布时间:2019-06-06

本文共 5273 字,大约阅读时间需要 17 分钟。

运行初始状态:

 

计算结果如下:

 

 

 

 

 

 

代码如下:

package jisuanqi;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JComboBox;import javax.swing.JFrame;class counter1 extends JFrame {public counter1()	 {	super("计算器");    this.setSize(400,100);    this.setLocation(300,240);    this.setLayout(new FlowLayout());   //确定窗口格式为流输入    TextField text1=new TextField(4);    text1.setText("1");    this.add(text1);        String proList[] = { "+","-","x" ,"%"};   //将"+","-","x" ,"%"作为列表元素    TextField text;    JComboBox comboBox;         //创建下拉复选框    Container conPane = getContentPane();   //创建一个名为conPane的容器    comboBox = new JComboBox(proList);    //把列表元素加入下拉复选框    comboBox.setEditable(true);     //使复选框变为可操作    conPane.add(comboBox);    //将复选框加入容器中        TextField text2=new TextField(4);    text2.setText("1");    this.add(text2);    JButton button = new JButton("=");    this.add(button);    TextField text3=new TextField(4);    text3.setText("2");        button.addActionListener(new ActionListener(){    //添加按钮监听事件    	public void actionPerformed(ActionEvent e)    //创建事件响应函数    	{     		String s=comboBox.getEditor().getItem().toString();    //获取复选框中的元素    		double a= Integer.parseInt(text1.getText());     //将两个文本框中的字符串强制转换成浮点型,以便于之后的计算    		double b= Integer.parseInt(text2.getText());    		if(s.equals("+")) {    //判断复选框中的元素种类    			double t=a+b;    			String m=String.valueOf(t);  //由于文本框中的数据流只能为字符串,这里就需要将计算得到的浮点型数据强制转换成字符串型    			    			text3.setText(m);    //将最后的结果放在文本框中    		}    		else if(s.equals("-"))  //后面的与之前的类似,不在注释    		{double t=a-b;			String m=String.valueOf(t);						text3.setText(m);}    		else if(s.equals("x"))    		{double t=a*b;    		String m=String.valueOf(t);						text3.setText(m);}    		else    		{double t=a/b;    		String m=String.valueOf(t);						text3.setText(m);}    	    	}});    conPane.add(text3);    this.setVisible(true);    //将窗口设置为可视化	 }}public class Counter {	public static void main(String[] args)	{			new counter1();			}}

  

 

修改版如下:

 

 

 

 

 

 

源码如下:

package jisuanqi;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.FocusEvent;import java.awt.event.FocusListener;import javax.swing.JButton;import javax.swing.JComboBox;import javax.swing.JFrame;import javax.swing.JLabel;class counter1 extends JFrame {public counter1()	 {	super("计算器");    this.setSize(400,130);    this.setLocation(300,240);    this.setLayout(new FlowLayout());   //确定窗口格式为流输入    TextField text1=new TextField(4);    text1.setText("1");    JLabel label1 = new JLabel("");    text1.addFocusListener(new FocusListener() {  //添加焦点事件监听器		public void focusGained(FocusEvent arg0) {   //设置获得焦点时的事件			label1.setText("正在输入中...");					}		public void focusLost(FocusEvent arg0) {     //设置失去焦点时的事件			if(!text1.getText().matches("\\d+"))  //使用正则表达式判断该字符串是否为数字,第一个\是转义符,\d+表示匹配1个或 //多个连续数字,"+"和"*"类似,"*"表示0个或多个				label1.setText("输入的第一个数据有误");			else label1.setText("");					}    });    this.add(text1);      String proList[] = { "+","-","x" ,"%"};   //将"+","-","x" ,"%"作为列表元素    TextField text;    JComboBox comboBox;         //创建下拉复选框    Container conPane = getContentPane();   //创建一个名为conPane的容器    comboBox = new JComboBox(proList);    //把列表元素加入下拉复选框    comboBox.setEditable(true);     //使复选框变为可操作    conPane.add(comboBox);    //将复选框加入容器中        TextField text2=new TextField(4);    text2.setText("1");    this.add(text2);    text2.addFocusListener(new FocusListener() {		public void focusGained(FocusEvent arg0) {			label1.setText("正在输入中...");					}		public void focusLost(FocusEvent arg0) {			if(!text2.getText().matches("\\d+"))				label1.setText("输入的第二个数据有误");			else label1.setText("");					}    });    JButton button = new JButton("=");    this.add(button);    TextField text3=new TextField(4);    text3.setText("2");        button.addActionListener(new ActionListener(){    //添加按钮监听事件    	public void actionPerformed(ActionEvent e)    //创建事件响应函数    	{     		String s=comboBox.getEditor().getItem().toString();    //获取复选框中的元素    		double a= Integer.parseInt(text1.getText());     //将两个文本框中的字符串强制转换成浮点型,以便于之后的计算    		double b= Integer.parseInt(text2.getText());    		if(s.equals("+")) {    //判断复选框中的元素种类    			double t=a+b;    			String m=String.valueOf(t);  //由于文本框中的数据流只能为字符串,这里就需要将计算得到的浮点型数据强制转换成字符串型    			    			text3.setText(m);    //将最后的结果放在文本框中    		}    		else if(s.equals("-"))  //后面的与之前的类似,不在注释    		{double t=a-b;			String m=String.valueOf(t);						text3.setText(m);}    		else if(s.equals("x"))    		{double t=a*b;    		String m=String.valueOf(t);						text3.setText(m);}    		else    		{double t=a/b;    		String m=String.valueOf(t);						text3.setText(m);}    	    	}});    conPane.add(text3);    this.add(label1);    this.setVisible(true);    //将窗口设置为可视化	 }}public class Counter {	public static void main(String[] args)	{			new counter1();			}}

  

总结心得:

(1)在创建选项框时,要将所有的选项放在一个“容器”里,并把这个容器添加到程序中,这里我用的容器为JComboBox comboBox,同时需要导入包javax.swing.ComboBoxEditor和javax.swing.JComboBox;

(2)由于设置了按钮响应功能,所以要设置按键动作和响应,这里导入了包java.awt.event.ActionEvent和java.awt.event.ActionListener

(3)因为文本框中输入读取到的是字符串,所以要进行计算时,要先将其转为整形,在文本框输出时,同理要将整形转换为字符串

(4)注意:当光标移动到文本框上面时,获得焦点事件,当光标移走时,便失去焦点事件,所以要注意两个函数的作用。

转载于:https://www.cnblogs.com/fjcy/p/10939931.html

你可能感兴趣的文章
goto语句
查看>>
简单的车票管理系统
查看>>
2016年10月25 草堂随笔1 ModelState.IsValid
查看>>
Jenkins Pipelines Summary
查看>>
倾斜摄影 实景三维建模软件photoscan教程
查看>>
Actiion Func ;Donet framework 中已经定义好的委托
查看>>
Python 模块之 xlrd (Excel文件读写)
查看>>
再探@font-face及webIcon制作
查看>>
BZOJ.4212.神牛的养成计划(Trie 可持久化Trie)
查看>>
【unityd--初始学习四--3d世界中的物体移动及角度变换】
查看>>
电脑cmos是什么?和bois的区别?
查看>>
REST WCF 使用Stream进行Server与Client交互
查看>>
Python数据分析之Matplotlib绘制柱状图
查看>>
Django组件之contenttype
查看>>
刘江的博客
查看>>
SpringBoot入门教程(二)CentOS部署SpringBoot项目从0到1
查看>>
Unity 2D游戏开发教程之为游戏场景添加多个地面
查看>>
Java日期时间处理
查看>>
hyperledger fabric 1.0.5 分布式部署 (七)
查看>>
BZOJ3670: [Noi2014]动物园
查看>>