java计算器

windows application程序-->

1.设计面板、按键、文本框,基本操作不多说。提一下几个重要的变量:

//opt 为上一个运算符,如果不懂,请看后面;

String opt = “=”;

//用来保存结果的变量,如果你学过大学计算机的话,就相当于寄存器;

double result = 0;

//因为当按数字的时候,要知道是否是一个完整的数字,比如 按1再按2 ,第二次到底是显示2呢还是显示12,如果显示2就代表不是一个完整的数字,如果是12就代表还是一个完整的数字;

boolean isfirt=true;

说明:因为我们计算的是中缀表达式,而计算机是将其转化了计算了的,这里就转化为后缀计算,例如:1+2 的后缀表达式就是:1 2 + ; 所以,opt应定义为上一个运算符;

按键是数字,就直接将其放入。

2.为各个按键添加事件,这里我们把主类实现一个接口 implements ActionListener,方便添加事件,因为直接在design里面加的话,他会自动实现匿名类,new ActionListener ()... 将它替换成 this。

package practice3;

import java.awt.EventQueue;

import javax.swing.JFrame;
import java.awt.BorderLayout;
import javax.swing.JTextField;
import java.awt.FlowLayout;
import javax.swing.JPanel;
import java.awt.GridLayout;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Color;

public class Carculator implements ActionListener{

	private JFrame frame;
	private JTextField view;
	private String opt = "=";
	private double result=0;
	//如果不是一个数字,当按数字键的时候就直接重新设置文本,否则直接加进去。
	boolean isfirst = true;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Carculator window = new Carculator();
					window.frame.setTitle("Carculator");
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public Carculator() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 450, 300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(new BorderLayout(0, 0));
		
		JPanel panel = new JPanel();
		frame.getContentPane().add(panel, BorderLayout.NORTH);
		FlowLayout fl_panel = new FlowLayout(FlowLayout.CENTER, 5, 5);
		panel.setLayout(fl_panel);
		
		view = new JTextField();
		view.setEditable(false);
		view.setText("0");
		view.setHorizontalAlignment(JTextField.RIGHT);
		panel.add(view);
		view.setColumns(35);
		
		JPanel panel_1 = new JPanel();
		frame.getContentPane().add(panel_1, BorderLayout.CENTER);
		panel_1.setLayout(new GridLayout(4, 4, 3, 3));
		
		JButton button_5 = new JButton("1");
		button_5.addActionListener(this);
		panel_1.add(button_5);
		
		JButton button_8 = new JButton("2");
		button_8.addActionListener(this);
		panel_1.add(button_8);
		
		JButton button_7 = new JButton("3");
		button_7.addActionListener(this);
		panel_1.add(button_7);
		
		JButton button_6 = new JButton("+");
		button_6.addActionListener(this);
		panel_1.add(button_6);
		
		JButton button_4 = new JButton("4");
		button_4.addActionListener(this);
		panel_1.add(button_4);
		
		JButton button_3 = new JButton("5");
		button_3.addActionListener(this);
		panel_1.add(button_3);
		
		JButton button = new JButton("6");
		button.addActionListener(this);
		panel_1.add(button);
		
		JButton button_1 = new JButton("-");
		button_1.addActionListener(this);
		panel_1.add(button_1);
		
		JButton button_2 = new JButton("7");
		button_2.addActionListener(this);
		panel_1.add(button_2);
		
		JButton btnNewButton = new JButton("8");
		btnNewButton.addActionListener(this);
		panel_1.add(btnNewButton);
		
		JButton button_9 = new JButton("9");
		button_9.addActionListener(this);
		panel_1.add(button_9);
		
		JButton button_11 = new JButton("*");
		button_11.addActionListener(this);
		panel_1.add(button_11);
		
		JButton button_10 = new JButton("C");
		button_10.addActionListener(this);
		panel_1.add(button_10);
		
		JButton button_12 = new JButton("0");
		button_12.addActionListener(this);
		panel_1.add(button_12);
		
		JButton button_13 = new JButton("=");
		button_13.addActionListener(this);
		button_13.setBackground(new Color(255, 0, 0));
		button_13.setForeground(Color.BLACK);
		panel_1.add(button_13);
		
		JButton button_14 = new JButton("/");
		button_14.addActionListener(this);
		panel_1.add(button_14);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		String str = e.getActionCommand();
		//是数字
		if("0123456789".indexOf(str)>=0){
			HandNum(str);
		}
		else if(str.equals("C")){
			HandleC();
		}
		else {
		//运算符
			HandCMD(str);
		}
	}
	// Clear 按钮 ,清空
	public void HandleC() {
		view.setText("0");
		isfirst = true;
		opt = "=";
	}
	//处理数字
	public void HandNum(String str) {
		//如果是第一个数字,直接
		if(isfirst) {
			view.setText(str);
		}
		else {
			view.setText(view.getText()+str);
		}
		isfirst = false;
	}
	//处理运算符
	public void HandCMD(String cmd) {
		if(opt.equals("=")) {
			//将文本框中的数字放入result中
			result = GetNumFromView();
			
		}
		else if(opt.equals("+")) {
			result+=GetNumFromView();
		}
		else if(opt.equals("-")) {
			result-=GetNumFromView();
		}
		else if(opt.equals("*")) {
			result*=GetNumFromView();
		}
		else if(opt.equals("/")) {
			result/=GetNumFromView();
		}
		long t1 = (long)result;
		if((t1 - result)==0) {
			view.setText(String.valueOf(t1));
		}
		else view.setText(String.valueOf(result));
		opt = cmd;
		isfirst = true;
	}
	public double GetNumFromView() {
		return Double.parseDouble(view.getText()); 		
	}

}


猜你喜欢

转载自blog.csdn.net/qq_40883132/article/details/80702797