简易计算器代码示例【部分】

这部分代码只是提供了一个流程的参考,完整的实现需要大家自己努力完善。


package cly.calc;

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

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

public class Calc {

	private JFrame frame;
	private JButton btn0;
	private JButton btn1;
	private JButton btn2;
	private JButton btn3;
	private JButton btn4;
	private JButton btn5;
	private JButton btn6;
	private JButton btn7;
	private JButton btn8;
	private JButton btn9;
	private JButton btnSign;
	private JButton btnAdd;
	private JButton btnSub;
	private JButton btnMulti;
	private JButton btnDiv;
	private JButton btnEqual;
	private JTextField tbMessage;
	
	private char currentAct='=';
	private boolean flag=false;
	private double theResult=0;
	private double currentNum=0;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Calc window = new Calc();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public Calc() {
		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(null);
		
		tbMessage = new JTextField();
		tbMessage.setText("0");
		tbMessage.setBounds(50, 10, 255, 23);
		frame.getContentPane().add(tbMessage);
		tbMessage.setColumns(10);
		tbMessage.setHorizontalAlignment(JTextField.RIGHT);
		
		btn0 = new JButton("0");
		btn0.addActionListener(new btnNumber());
		btn0.setActionCommand("0");
		btn0.setBounds(47, 164, 59, 23);
		frame.getContentPane().add(btn0);
		
		
		btn1 = new JButton("1");
		btn1.addActionListener(new btnNumber());
		btn1.setActionCommand("1");
		btn1.setBounds(47, 58, 59, 23);
		frame.getContentPane().add(btn1);
		
		btn2 = new JButton("2");
		btn2.addActionListener(new btnNumber());
		btn2.setActionCommand("2");
		btn2.setBounds(116, 58, 64, 23);
		frame.getContentPane().add(btn2);
		
		btn3 = new JButton("3");
		btn3.addActionListener(new btnNumber());
		btn3.setActionCommand("3");
		btn3.setBounds(190, 58, 57, 23);
		frame.getContentPane().add(btn3);
		
		btn4 = new JButton("4");
		btn4.addActionListener(new btnNumber());
		btn4.setActionCommand("4");
		btn4.setBounds(47, 91, 59, 23);
		frame.getContentPane().add(btn4);
		
		btn5 = new JButton("5");
		btn5.addActionListener(new btnNumber());
		btn5.setActionCommand("5");
		btn5.setBounds(116, 91, 64, 23);
		frame.getContentPane().add(btn5);
		
		btn6 = new JButton("6");
		btn6.addActionListener(new btnNumber());
		btn6.setActionCommand("6");
		btn6.setBounds(190, 91, 57, 23);
		frame.getContentPane().add(btn6);
		
		btn7 = new JButton("7");
		btn7.addActionListener(new btnNumber());
		btn7.setActionCommand("7");
		btn7.setBounds(47, 124, 59, 23);
		frame.getContentPane().add(btn7);
		
		btn8 = new JButton("8");
		btn8.addActionListener(new btnNumber());
		btn8.setActionCommand("8");
		btn8.setBounds(116, 124, 64, 23);
		frame.getContentPane().add(btn8);
		
		btn9 = new JButton("9");
		btn9.addActionListener(new btnNumber());
		btn9.setActionCommand("9");
		btn9.setBounds(190, 124, 57, 23);
		frame.getContentPane().add(btn9);
		
		btnSign = new JButton("+/-");
		btnSign.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(e.getActionCommand().equals("negative")){
					if(!tbMessage.getText().equals("0")){
						tbMessage.setText("-"+tbMessage.getText());
						
					}
					btnSign.setActionCommand("positive");
				}
				else
				{					
					tbMessage.setText(tbMessage.getText().substring(1,tbMessage.getText().length()));
					btnSign.setActionCommand("negative");
				}
				currentNum=Double.valueOf(tbMessage.getText());
			}
		});
		
		btnSign.setActionCommand("negative");
		btnSign.setBounds(116, 164, 131, 23);
		frame.getContentPane().add(btnSign);
		
		
		
		btnAdd = new JButton("+");
		btnAdd.addActionListener(new btnAction());
		btnAdd.setActionCommand("+");
		btnAdd.setBounds(258, 58, 47, 23);
		frame.getContentPane().add(btnAdd);
		
		btnSub = new JButton("-");
		btnSub.addActionListener(new btnAction());
		btnSub.setActionCommand("-");
		btnSub.setBounds(258, 91, 47, 23);
		frame.getContentPane().add(btnSub);
		
		btnMulti = new JButton("*");
		btnMulti.addActionListener(new btnAction());
		btnMulti.setActionCommand("*");
		btnMulti.setBounds(258, 124, 47, 23);
		frame.getContentPane().add(btnMulti);
		
		btnDiv = new JButton("/");
		btnDiv.addActionListener(new btnAction());
		btnDiv.setActionCommand("/");
		btnDiv.setBounds(258, 164, 47, 23);
		frame.getContentPane().add(btnDiv);
		
		btnEqual = new JButton("=");
		btnEqual.addActionListener(new btnAction());
		btnEqual.setActionCommand("=");
		btnEqual.setBounds(50, 197, 255, 23);
		frame.getContentPane().add(btnEqual);
	}
	
	class btnNumber implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent arg0) {
			// TODO Auto-generated method stub	
			
			String theNum=arg0.getActionCommand();
			String tbNum=tbMessage.getText();			
			if(tbNum.equals("0") || flag)
				tbMessage.setText(theNum);
			else
				tbMessage.setText(tbNum+theNum);
			currentNum=Double.valueOf(tbMessage.getText());
			flag=false;
		}
	}
	class btnAction implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent arg0) {
			// TODO Auto-generated method stub
			String theAct=arg0.getActionCommand();
			switch(currentAct)
			{
			case '+':				
				theResult+=currentNum;
				tbMessage.setText(String.valueOf(theResult));
				
				break;
			case '-':
				theResult-=currentNum;
				tbMessage.setText(String.valueOf(theResult));
				
				break;
			case '*':
				theResult*=currentNum;
				tbMessage.setText(String.valueOf(theResult));
				
				break;
			case '/':
				theResult/=currentNum;
				tbMessage.setText(String.valueOf(theResult));
				
				break;	
			case '=':
				theResult=Double.valueOf(tbMessage.getText());
				
				break;
			}
			flag=true;
			currentAct=theAct.charAt(0);
			currentNum=0;
		}
	}
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值