Java Swing 计算器

时间:2024-04-11 16:28:52

 

Java Swing 计算器

做了最基本最简单的功能

思路参考了其他博客

这里就不赘述了

 

整个程序可以分为三个层次:

第一层:界面层,通过各种组件组成界面

具体怎么设计,随意

第二层:响应层,即使用监听器接受输入的数据

第三层:运算层,运算结果放回界面层

响应层和运算层被我设计在一起了

 

状态机:

本来画了个图,但是这个插入图片还不能旋转是什么鬼

由于时间有限,该状态机肯定有错误,例如出现多个小数点,必定报错

 

package calculator;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 * @author yangjiangling
 *
 */

public class MainFrame {

	double calculateResult=0;//计算结果
	int status;//状态机
	double number1;
	char c;
	double number2;
	String textString;
	private JPanel p1;//画板一
	private JPanel p2;//画板二
	private JPanel p3;//画板三
	
	private JFrame f;//框架
	private JTextField tf;//显示框和输入框
	private JButton[] bs;//17个按钮
	private String[] strs= {
			"Clear",
			"7","8","9","/",		
			"4","5","6","*",	
			"1","2","3","-",		
			"0",".","=","+"};
	private JLabel lNum1;
	private JLabel lNum2;
	private JLabel lResult;
	
	private JLabel lCal;
	
	int gridHeight=30;
	int gridWidth=60;
	
	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		MainFrame mf=new MainFrame();
	}

	public MainFrame() {
		super();
		status=0;
		number1=0;//输入的第一个数字
		number2=0;//输入的第二个数字
		textString=new String();	
		//画板三
		p3=new JPanel();
		
		lNum1=new JLabel("number1:");
		lNum2=new JLabel("number2:");
		lCal=new JLabel("运算符:");
		lResult=new JLabel("result:");

		p3.setLayout(new GridLayout(1,4));
		
		p3.add(lNum1);
		p3.add(lNum2);
		p3.add(lResult);
		p3.add(lCal);
		//画板一
		p1=new JPanel();
		
		tf=new JTextField(Double.toString(calculateResult),20);//文本框
		tf.setPreferredSize(new Dimension(gridWidth*3,gridHeight));
		
		bs=new JButton[17];
		bs[0]=new JButton(strs[0]);//按钮Clear
		bs[0].setPreferredSize(new Dimension(gridWidth*2,gridHeight));
		
		p1.add(tf);
		p1.add(bs[0]);
		
		p1.setLayout(new FlowLayout());
		
		//画板二
		p2=new JPanel();
		
		for (int i = 1; i < bs.length; i++) {//按钮1-16
			bs[i]=new JButton();
			bs[i].setText(strs[i]);
			bs[i].setPreferredSize(new Dimension(gridWidth,gridHeight));
			p2.add(bs[i]);
		}
		p2.setLayout(new GridLayout(4, 4,10,10));

		//主框架
		f=new JFrame();
		f.setSize(new Dimension(gridWidth*8,gridHeight*10));
		f.setLocation(200, 200);
		
		f.setLayout(new BorderLayout());
		f.add(p3,BorderLayout.NORTH);
		f.add(p1,BorderLayout.CENTER);
		f.add(p2,BorderLayout.SOUTH);
		
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f.setVisible(true);
		
		setListeners();
		
	}

	private void setListeners() {
		// TODO 自动生成的方法存根
		for (JButton jButton : bs) {
			jButton.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {
					// TODO 自动生成的方法存根
					String str=jButton.getText();

					if(str.equals(strs[0]))//清空按钮,任何状态都转为0状态
					{//只需做一个事情,字符串清空
						textString="";
						tf.setText(textString);
						lNum1.setText("number1:"+textString);
						lNum2.setText("number2:"+textString);
						lCal.setText("运算符:"+textString);
						lResult.setText("result:"+textString);
						status=0;
					}
					else if(str.charAt(0)-'0'>=0&&str.charAt(0)-'0'<=9)//点击数字按钮
					{
						textString+=str.charAt(0)-'0';//拼接
						tf.setText(textString);
						switch (status) {
						case 0:
						case 1:
						case 2:
							status=1;
							break;
						case 3:
						case 4:
						case 5:
							status=4;
							break;
						case 6:
							status=1;
							textString=String.valueOf(str.charAt(0)-'0');
							tf.setText(textString);
							break;
						default:
							break;
						}
						
					}
					else if(str.charAt(0)=='.')
					{
						switch (status) {
						case 1:
							textString+='.';//拼接
							tf.setText(textString);
							status=2;
							break;
						case 4:
							textString+='.';//拼接
							tf.setText(textString);
							status=5;
							break;

						default:
							break;
						}
					}
					else if(str.charAt(0)=='='&&status==4)//等号
					{
						status=6;
						number2=Double.parseDouble(tf.getText());
						lNum2.setText("number2:"+tf.getText());
						
						switch (c) {
						case '+':
							calculateResult=number1+number2;
							break;
						case '-':
							calculateResult=number1-number2;
							break;
						case '*':
							calculateResult=number1*number2;
							break;
						case '/':
							calculateResult=number1/number2;
							break;
						default:
							break;
						}
						lResult.setText("result:"+String.valueOf(calculateResult));
						tf.setText(String.valueOf(calculateResult));
					}
					else//四个运算符
					{
						switch (str.charAt(0)) {//记录算符
							case '+':
							case '-':
							case '*':
							case '/':
								c=str.charAt(0);
								break;
	
							default:
								break;
						}
						
						switch (status) {
						case 1:
							status=3;
							number1=Double.parseDouble(tf.getText());
							lNum1.setText("number1:"+tf.getText());
							lCal.setText("运算符:"+String.valueOf(c));
							textString="";
							tf.setText(textString);
							break;
						case 6:
							status=3;
							number1=calculateResult;
							lNum1.setText("number1:"+String.valueOf(number1));
							textString="";
							lNum2.setText("number2:"+textString);
							lCal.setText("运算符:"+String.valueOf(c));
							lResult.setText("result:"+textString);
							tf.setText(textString);
							break;
						default:
							break;
						}
					}
				}
			});
		}
	}

	
}