欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

用Java的swing组件写出简单计算器

程序员文章站 2024-01-31 12:58:58
...

做出来的效果是可以退格,简单计算,然后不合理的输入会不理会,同时实时更新计算
用Java的swing组件写出简单计算器

代码如下,试了一晚上,头秃
代码看起来很多,就是因为光暴力塑造ationPerformed函数了,花了400多行,如果想再简单点,可以自己再简化,一定要先构思好

package theSixUnit;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
import javax.swing.event.*;


public class Caculator extends JFrame implements ActionListener {
	private int flag = 0;
	double a = 0, b = 0;
	int index = -1;
	private JTextField text_num;
	private JTextField text_result;
	private JPanel showPanel, buttonPanel;
	private JButton[] buttons;
	public String[] str = {"AC", "退格", "+/-", "/", "7", "8", "9", "×", "4", "5", "6",
			"-", "1", "2", "3", "+", "%", "0", ".", "="};
	public Caculator()
	{
		super("幼儿园计算器");
		this.setBounds(300, 240, 480, 480);
		this.setBackground(Color.darkGray);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		
		showPanel = new JPanel(new GridLayout(3,1));
		JLabel label = new JLabel("规则:从左往右运算",JLabel.CENTER);
		label.setFont(new Font("楷体", 1, 35));
		this.showPanel.add(label);
		buttonPanel = new JPanel(new GridLayout(5,4));
		JSplitPane split = new JSplitPane(JSplitPane.VERTICAL_SPLIT, showPanel, buttonPanel);
		split.setDividerLocation(110);
		this.add(split);
		text_num = new JTextField("", 40);
		text_num.setFont(new Font("楷体", 1, 30));
		text_num.setHorizontalAlignment(JTextField.RIGHT);
		text_num.setEditable(false);
		this.showPanel.add(text_num);
		text_result = new JTextField("", 40);
		text_result.setHorizontalAlignment(JTextField.RIGHT);
		text_result.setEditable(false);//不可编辑
		text_result.setFont(new Font("楷体", 1, 30));
		this.showPanel.add(text_result);
		
		this.buttons = new JButton[str.length];
		for(int i = 0; i < str.length; i++) {
			this.buttons[i] = new JButton(str[i]);
			this.buttons[i].addActionListener(this);
			this.buttonPanel.add(this.buttons[i]);
		}
		this.setVisible(true);
	}
	
	public void actionPerformed(ActionEvent event) {
		for(int i = 0; i < str.length; i++) {
			if(event.getSource() == buttons[i]) {
				String s = str[i];
				switch(s) {
				case "AC":
					if(flag == 1 || flag == 2 || flag == 3 || flag == 4 || flag == 5 ||
					flag == 666) {
						this.text_num.setText("");
						this.text_result.setText("");
						flag = 0;
						a = 0;
						index = -1;
					}
					break;
				case "退格":
					if(flag == 1 && this.text_num.getText().length() > 0) {
						this.text_num.setText(this.text_num.getText().substring(
								 0,this.text_num.getText().length()-1));
						flag = 0;
					}
					else if(flag == 5 && this.text_num.getText().length()-1 > index) {
						this.text_num.setText(this.text_num.getText().substring(
								 0,this.text_num.getText().length()-1));
						if(this.text_num.getText().length()-1 == index) {
							flag = 2;
							this.text_result.setText(a+"");
						}
						else {
							b = Double.parseDouble(this.text_num.getText().substring(index+1,
									this.text_num.getText().length()));
							switch(this.text_num.getText().charAt(index)) {
							case '+':
								this.text_result.setText(a+b+"");
								break;
							case '-':
								this.text_result.setText(a-b+"");
								break;
							case '×':
								this.text_result.setText(a*b+"");
								break;
							case '/':
								this.text_result.setText(a/b+"");
								break;
							case '%':
								this.text_result.setText(a%b+"");
								break;
							}
						}
					}
					break;
				case "+":
					if(flag == 1) {
						flag = 2;
						this.text_num.setText(this.text_num.getText()+"+");
						index = this.text_num.getText().length()-1;
						//System.out.println(i+"");
						a = Double.parseDouble(this.text_num.getText().substring(0,index));
					}
					else if(flag == 3) {
						this.text_num.setText(this.text_num.getText()+"+");
						index = this.text_num.getText().length()-1;
						flag = 2;
					}
					else if(flag == 5) {
						b = Double.parseDouble(this.text_num.getText().substring(index+1,
								this.text_num.getText().length()));
						this.text_num.setText(this.text_num.getText()+"+");
						flag = 2;
						int j = index;
						index = this.text_num.getText().length()-1;
						switch(this.text_num.getText().charAt(j)) {
						case '+':
							a = a+b;
							this.text_result.setText(a+"");
							break;
						case '-':
							a = a-b;
							this.text_result.setText(a+"");
							break;
						case '×':
							a = a*b;
							this.text_result.setText(a+"");
							break;
						case '/':
							a = a/b;
							this.text_result.setText(a+"");
							break;
						case '%':
							a = a%b;
							this.text_result.setText(a+"");
							break;
						}
					}
					break;
				case "-":
					if(flag == 1) {
						flag = 2;
						this.text_num.setText(this.text_num.getText()+"-");
						index = this.text_num.getText().length()-1;
						a = Double.parseDouble(this.text_num.getText().substring(0,index));
					}
					else if(flag == 3) {
						this.text_num.setText(this.text_num.getText()+"-");
						index = this.text_num.getText().length()-1;
						flag = 2;
					}
					else if(flag == 5) {
						b = Double.parseDouble(this.text_num.getText().substring(index+1,
								this.text_num.getText().length()));
						this.text_num.setText(this.text_num.getText()+"-");
						flag = 2;
						int j = index;
						index = this.text_num.getText().length()-1;
						switch(this.text_num.getText().charAt(j)) {
						case '+':
							a = a+b;
							this.text_result.setText(a+"");
							break;
						case '-':
							a = a-b;
							this.text_result.setText(a+"");
							break;
						case '×':
							a = a*b;
							this.text_result.setText(a+"");
							break;
						case '/':
							a = a/b;
							this.text_result.setText(a+"");
							break;
						case '%':
							a = a%b;
							this.text_result.setText(a+"");
							break;
						}
					}
					break;
				case "/":
					if(flag == 1) {
						flag = 2;
						this.text_num.setText(this.text_num.getText()+"/");
						index = this.text_num.getText().length()-1;
						a = Double.parseDouble(this.text_num.getText().substring(0,index));
					}
					else if(flag == 3) {
						this.text_num.setText(this.text_num.getText()+"/");
						index = this.text_num.getText().length()-1;
						flag = 2;
					}
					else if(flag == 5) {
						b = Double.parseDouble(this.text_num.getText().substring(index+1,
								this.text_num.getText().length()));
						this.text_num.setText(this.text_num.getText()+"/");
						flag = 2;
						int j = index;
						index = this.text_num.getText().length()-1;
						switch(this.text_num.getText().charAt(j)) {
						case '+':
							a = a+b;
							this.text_result.setText(a+"");
							break;
						case '-':
							a = a-b;
							this.text_result.setText(a+"");
							break;
						case '×':
							a = a*b;
							this.text_result.setText(a+"");
							break;
						case '/':
							a = a/b;
							this.text_result.setText(a+"");
							break;
						case '%':
							a = a%b;
							this.text_result.setText(a+"");
							break;
						}
					}
					break;
				case "×":
					if(flag == 1) {
						flag = 2;
						this.text_num.setText(this.text_num.getText()+"×");
						index = this.text_num.getText().length()-1;
						a = Double.parseDouble(this.text_num.getText().substring(0,index));
					}
					else if(flag == 3) {
						this.text_num.setText(this.text_num.getText()+"×");
						index = this.text_num.getText().length()-1;
						flag = 2;
					}
					else if(flag == 5) {
						b = Double.parseDouble(this.text_num.getText().substring(index+1,
								this.text_num.getText().length()));
						this.text_num.setText(this.text_num.getText()+"×");
						flag = 2;
						int j = index;
						index = this.text_num.getText().length()-1;
						switch(this.text_num.getText().charAt(j)) {
						case '+':
							a = a+b;
							this.text_result.setText(a+"");
							break;
						case '-':
							a = a-b;
							this.text_result.setText(a+"");
							break;
						case '×':
							a = a*b;
							this.text_result.setText(a+"");
							break;
						case '/':
							a = a/b;
							this.text_result.setText(a+"");
							break;
						case '%':
							a = a%b;
							this.text_result.setText(a+"");
							break;
						}
					}
					
				case "+/-":
					if(flag == 1) {
						a = Double.parseDouble(this.text_num.getText())*(-1);
						this.text_result.setText(""+a);
						this.text_num.setText(this.text_num.getText()+"(+/-)");
						flag = 3;
					}
					else if(flag == 3) {
						a = -a;
						this.text_result.setText(""+a);
						this.text_num.setText(this.text_num.getText()+"(+/-)");
					}
					else if(flag == 5) {
						this.text_num.setText(this.text_num.getText()+"(+/-)");
						flag = 3;
						switch(this.text_num.getText().charAt(index)) {
						case '+':
							a = -(a+b);
							this.text_result.setText(a+"");
							break;
						case '-':
							a = -(a-b);
							this.text_result.setText(a+"");
							break;
						case '×':
							a = -a*b;
							this.text_result.setText(a+"");
							break;
						case '/':
							a = -a/b;
							this.text_result.setText(a+"");
							break;
						case '%':
							a = -a%b;
							this.text_result.setText(a+"");
							break;
					    }
					}
					break;
				case "=":
					if(flag == 0) {
						this.text_num.setText(this.text_num.getText()+"=");
						this.text_result.setText("0");
					}
					if(flag == 1) {
						a = Double.parseDouble(this.text_num.getText());
						this.text_result.setText(a+"");
					}
					else if(flag == 2) {
						this.text_result.setText(a+"");
						this.text_num.setText(a+"");
					}
					else if(flag == 3) {
						this.text_num.setText(a+"");
					}
					else if(flag == 5) {
						b = Double.parseDouble(this.text_num.getText().substring(index+1,
								this.text_num.getText().length()));
						switch(this.text_num.getText().charAt(index)) {
						case '+':
							a = a+b;
							this.text_result.setText(a+"");
							this.text_num.setText(a+"");
							break;
						case '-':
							a = a-b;
							this.text_result.setText(a+"");
							this.text_num.setText(a+"");
							break;
						case '×':
							a = a*b;
							this.text_result.setText(a+"");
							this.text_num.setText(a+"");
							break;
						case '/':
							a = a/b;
							this.text_result.setText(a+"");
							this.text_num.setText(a+"");
							break;
						case '%':
							a = a%b;
							this.text_result.setText(a+"");
							this.text_num.setText(a+"");
							break;
						}
					}
					flag = 3;
					break;
				case "%":
					if(flag == 1) {
						flag = 2;
						this.text_num.setText(this.text_num.getText()+"%");
						index = this.text_num.getText().length()-1;
						a = Double.parseDouble(this.text_num.getText().substring(0,index));
					}
					else if(flag == 3) {
						this.text_num.setText(this.text_num.getText()+"%");
						index = this.text_num.getText().length()-1;
						flag = 2;
					}
					else if(flag == 5) {
						b = Double.parseDouble(this.text_num.getText().substring(index+1,
								this.text_num.getText().length()));
						this.text_num.setText(this.text_num.getText()+"%");
						flag = 2;
						int j = index;
						index = this.text_num.getText().length()-1;
						switch(this.text_num.getText().charAt(j)) {
						case '+':
							a = a+b;
							this.text_result.setText(a+"");
							break;
						case '-':
							a = a-b;
							this.text_result.setText(a+"");
							break;
						case '×':
							a = a*b;
							this.text_result.setText(a+"");
							break;
						case '/':
							a = a/b;
							this.text_result.setText(a+"");
							break;
						case '%':
							a = a%b;
							this.text_result.setText(a+"");
							break;
						}
					}
					break;
				case "0":
				case "1":
				case "2":
				case "3":
				case "4":
				case "5":
				case "6":
				case "7":
				case "8":
				case "9":
				case ".":
					if(flag == 0 || flag == 1) {
						this.text_num.setText(this.text_num.getText()+s);
						flag = 1;
					}
					else if(flag == 2) {
						this.text_num.setText(this.text_num.getText()+s);
						b = Double.parseDouble(this.text_num.getText().substring(index+1,this.text_num.getText().length()));
						switch(this.text_num.getText().charAt(index)) {
						case '+':
							this.text_result.setText(a+b+"");
							break;
						case '-':
							this.text_result.setText(a-b+"");
							break;
						case '×':
							this.text_result.setText(a*b+"");
							break;
						case '/':
							this.text_result.setText(a/b+"");
							break;
						case '%':
							this.text_result.setText(a%b+"");
							break;
						}
						flag = 5;
					}
					else if(flag == 5) {
						this.text_num.setText(this.text_num.getText()+s);
						b = Double.parseDouble(this.text_num.getText().substring(index+1,
								this.text_num.getText().length()));
						switch(this.text_num.getText().charAt(index)) {
						case '+':
							this.text_result.setText(a+b+"");
							break;
						case '-':
							this.text_result.setText(a-b+"");
							break;
						case '×':
							this.text_result.setText(a*b+"");
							break;
						case '/':
							this.text_result.setText(a/b+"");
							break;
						case '%':
							this.text_result.setText(a%b+"");
							break;
						}
					}
					break;
				
			    }
			}
		}
	}
	public static void main(String[] args) {
		new Caculator();
	}
}



相关标签: java swing switch