workout.java

来自「用JAVA编写的取整计算器源码」· Java 代码 · 共 186 行

JAVA
186
字号
package com;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;

public class WorkOut implements ActionListener{
	
	MainPanel hf;
	
	int num2 = 0,b = 0;
	
	int num;
	
	int op;
	
	String a;
	
	String StringNum = "";
	
	boolean newNum = false;
	boolean newOp = true;
	boolean newResualt = false;
	
	public WorkOut(MainPanel hf){
		this.hf = hf;
	}

	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		
		if(((JButton)e.getSource()).equals(hf.buttons[0])){
			a = "7";
			workNum();
		}
		if(((JButton)e.getSource()).equals(hf.buttons[1])){
			a = "8";
			workNum();
		}
		if(((JButton)e.getSource()).equals(hf.buttons[2])){
			a = "9";
			workNum();
		}
		if(((JButton)e.getSource()).equals(hf.buttons[3])){
			op = 1;//"+";
			workOp();
		}
		if(((JButton)e.getSource()).equals(hf.buttons[4])){
			a = "4";
			workNum();
		}
		if(((JButton)e.getSource()).equals(hf.buttons[5])){
			a = "5";
			workNum();
		}
		if(((JButton)e.getSource()).equals(hf.buttons[6])){
			a = "6";
			workNum();
		}
		if(((JButton)e.getSource()).equals(hf.buttons[7])){
			op = 2;//"-";
			workOp();
		}
		if(((JButton)e.getSource()).equals(hf.buttons[8])){
			a = "1";
			workNum();
		}
		if(((JButton)e.getSource()).equals(hf.buttons[9])){
			a = "2";
			workNum();
		}
		if(((JButton)e.getSource()).equals(hf.buttons[10])){
			a = "3";
			workNum();
		}
		if(((JButton)e.getSource()).equals(hf.buttons[11])){
			op = 3;//"*";
			workOp();
		}
		if(((JButton)e.getSource()).equals(hf.buttons[12])){
			a = "0";
			workNum0();
		}
		if(((JButton)e.getSource()).equals(hf.buttons[13])){
			clearall();
		}
		if(((JButton)e.getSource()).equals(hf.buttons[14])){
			op = 4;//"/";
			workOp();
		}
		if(((JButton)e.getSource()).equals(hf.buttons[15])){
			resualt();
		}
		
		
	}

	private void resualt() {
		// TODO Auto-generated method stub
		
		if(newResualt == true){
			
			switch (op){
				case 0:
					break;
				case 1:
					num = Integer.parseInt(StringNum)+num;
					break;
				case 2:
					num = num-Integer.parseInt(StringNum);
					break;
				case 3:
					num = num*Integer.parseInt(StringNum);
					break;
				case 4:
					num = num/Integer.parseInt(StringNum);
					break;
				
			}
		}
		
		newNum = true;
		
		newResualt = false;
		
		StringNum = Integer.toString(num);
		
		showNums();
	}

	private void workOp() {
		// TODO Auto-generated method stub
		if(newOp){
			
			op = 0;
			return;
		}
		newNum = true;
	}

	private void workNum0() {
		// TODO Auto-generated method stub
		if(hf.showNum.getText() != "0"){
			workNum();
		}
	}

	private void clearall() {
		// TODO Auto-generated method stub
		StringNum = "0";
		showNums();
		newNum = true;
	}

	private void workNum() {
		// TODO Auto-generated method stub
		
		newOp = false;
		if(hf.showNum.getText() == "0"){
			StringNum = "";
			showNums();
		}
		
		if(newNum == false){
			StringNum = StringNum+a;
			showNums();
//			newResualt = true;
		}
		if(newNum == true){
			
			num = Integer.parseInt(StringNum);
			StringNum = a;
			showNums();
			newNum = false;
			newResualt = true;
		}
	}

	private void showNums() {
		// TODO Auto-generated method stub
		hf.showNum.setText(StringNum);
	}

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?