JAVA简单计算器乘法

package work1;

import java.awt.List;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.function.IntToDoubleFunction;

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

public class TestMain {
	JFrame jFrame;//成员变量,未设置访问类型,默认包内访问,先创建一个JF对象
	String stringx;//保存第一个变量
	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		TestMain testMain=new TestMain();
		testMain.initView();
	}
	public void initView() {
		jFrame=new JFrame("好好学习");//第一步,初始化
		jFrame.setBounds(500, 500, 600, 600);//第二步,设置位置大小
		jFrame.setLayout(null);//控制布局 null为不使用系统所给的任何一种布局
		final JTextField jTextField=new JTextField();
		jTextField.setBounds(50, 50,500, 50);
		jFrame.add(jTextField);
		
		
		//数字符******************************************************************
		JButton jButton0=new JButton("0");//********00000000000000000
		jButton0.setBounds(50, 100, 50, 50);
		jFrame.add(jButton0);
		jButton0.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO 自动生成的方法存根
				String string =jTextField.getText().trim();//去掉两端空格的代码trim
				if(!string.equals("0"))
				{
					string=string+"0";
					jTextField.setText(string);
				}
			}
		});
		
		JButton jButton1=new JButton("1");//**********111111111111111111111111
		jButton1.setBounds(100, 100, 50, 50);
		jFrame.add(jButton1);
		jButton1.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO 自动生成的方法存根
				String string= jTextField.getText().trim();
				if(string.equals("0"))
				{
					jTextField.setText("1");
				}
				else
				{
					string=string+"1";
					jTextField.setText(string);
				}
			}
			
		})	;
		JButton jButton2=new JButton("2");
		jButton2.setBounds(150, 100, 50, 50);
		jFrame.add(jButton2);
		jButton1.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO 自动生成的方法存根
				String string= jTextField.getText().trim();
				if(string.equals("0"))
				{
					jTextField.setText("2");
				}
				else
				{
					string=string+"2";
					jTextField.setText(string);
				}
			}
			
		})	;
		//数字符***************************数字符***************************************
		
		
		
		//运算符***************************运算符***************************************
		JButton jButton11=new JButton("+");
		jButton11.setBounds(150, 150, 50, 50);
		jButton11.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO 自动生成的方法存根
				jTextField.setText(jTextField.getText()+"+");
			}
		});
		jFrame.add(jButton11);
		//运算符***************************运算符***************************************
		
		
		
		//操作符***************************操作符***************************************
		JButton jButtonC=new JButton("C");//清零
		jButtonC.setBounds(50, 150, 50, 50);
		jButtonC.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO 自动生成的方法存根
				jTextField.setText("0");
			}
		});
		jFrame.add(jButtonC);//................................
		JButton jButtonD=new JButton(".");//加点
		jButtonD.setBounds(100,150, 50, 50);
		jButtonD.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO 自动生成的方法存根
			String string=jTextField.getText();
			if(!string.contains("."))
			{
				
				string=string+".";
			}
			}
		});
		jFrame.add(jButtonD);
		
		
		JButton jButtonE=new JButton("=");//结果
		jButtonE.setBounds(200,150, 50, 50);
		jButtonE.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO 自动生成的方法存根
			ArrayList<Integer> op=new ArrayList<Integer>();
			ArrayList<Double> sum=new ArrayList<Double>();
			String string=jTextField.getText()+"+";
			double num1=0;//乘除号前面的值
			int mark=0;//乘除标志词
			double t=0;//每个数的值
			
			int m=Integer.valueOf('*');
			int n=Integer.valueOf('/');
			int a=Integer.valueOf('0');
			int b=Integer.valueOf('9');
			int ad=Integer.valueOf('+');
			for(int i=0;i<string.length();i++)
			{
				int num=Integer.valueOf(string.charAt(i));
				//将i的字符转化为asc码
				if(a<=num&&num<=b)
					{
						t=t*10+(num-a);
					}
				else
					{
					if(mark!=0)
						t=(mark==1)?(t*num1):(t/num1);//求出乘除运算结果赋值给t
					
					if(num==m)
						{
								num1=t;
								mark=1;
								
						}//如果后面是*,则标记乘号,将乘号前的那个数赋值给num1保存
					else 
						if(num==n)
						{
								num1=t;
								mark=2;	
								
						}
					
						else {//加减 则保存
							mark=0;
							op.add(num);//存符号
							sum.add(t);//存数据
							
						}
					t=0;
					}
			
			}
			
			double ans=sum.get(0);//答案 4 2
			for(int i=1;i<sum.size();i++)
			{
				Double num2=sum.get(i);
				int op1=op.get(i-1);
				ans=(op1==ad)?(ans+num2):(ans-num2);
			}
			
			jTextField.setText(Double.toString(ans));
			}
			
			
		});
		jFrame.add(jButtonE);
		
		jFrame.setVisible(true);//第三部,让界面显示出来,这句话一定在最后
		//但凡是成对的元素,必须要先写出来再将光标移到中间写其他的代码
	}

}
简单的计算器 import java.awt.*; import java.awt.event.*; import javax.swing.*; public class MyCalculator extends Frame implements ActionListener { JTextField txt = new JTextField(""); StringBuffer copyBoard=new StringBuffer(20); private String cmd = "="; private double result = 0;// 运算结果 private boolean start = true; StringBuffer strs=new StringBuffer(); class Window { Window() { Frame help=new Frame("关于作者"); help.setBounds(400,200,200,200); help.setVisible(true); help.setResizable(false); help.addWindowListener(new WindowAdapter() //关闭窗口 { public void windowClosing(WindowEvent e) { ((Frame)e.getSource()).dispose(); } } ); TextArea title = new TextArea(" 软件125实训项目 一 制作:第二组 常志铭 朱靖 2013.5.10 ",10,8,TextArea.SCROLLBARS_NONE); title.setBounds(50,50,200,30); title.setEditable(false); help.add(title); } } MyCalculator() { this.setTitle("我的计算器"); this.setBounds(400,150,225,320); this.createMenu(); this.createMainPanel(); this.addWindowListener(new WindowAdapter() //关闭窗口 { public void windowClosing(WindowEvent e) { System.exit(0); } } ); this.setResizable(false); this.setVisible(true); } private void createMenu() { MenuBar bar = new MenuBar(); this.setMenuBar(bar); Menu EditMenu = new Menu("编辑(E)"); Menu helpMenu = new Menu("帮助(H)"); bar.add(EditMenu); bar.add(helpMenu); MenuItem copyItem = new MenuItem("复制"); copyItem.setShortcut(new MenuShortcut(KeyEvent.VK_C,false)); EditMenu.add(copyItem); copyItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { String temp = txt.getText().trim(); copyBoard.replace(0, copyBoard.length(), temp); } } ); MenuItem pasteItem = new MenuItem("粘帖"); pasteItem.setShortcut(new MenuShortcut(KeyEvent.VK_V,false)); EditMenu.add(pasteItem); pasteItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { txt.setText(copyBoard.toString()); result=(Double.parseDouble(copyBoard.toString())); } } ); MenuItem helpItem = new MenuItem("关于计算器"); helpItem.setShortcut(new MenuShortcut(KeyEvent.VK_H,false)); helpMenu.add(helpItem); helpItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { new Window(); } } ); } private void createMainPanel() { //整体面板 Panel Win = new Panel(null); Win.setSize(230, 330); Win.setVisible(true); //Win.setBackground(Color.black); this.add(Win); //显示 txt.setHorizontalAlignment(JTextField.RIGHT); txt.setBounds(5,5,210,50); txt.setFont(new java.awt.Font("Dialog", 1, 30)); txt.setEditable(false); Win.add(txt); //按键面板 Panel Butt = new Panel(null); Butt.setBounds(0, 50, 230, 280); //Butt.setBackground(Color.white); Butt.setVisible(true); Win.add(Butt); Button butx = new Button("C"); butx.setBounds(15,15,40,28); butx.addActionListener(this); butx.setFont(new java.awt.Font("Dialog", 1, 15)); Butt.add(butx); Button butz = new Button("←"); butz.setBounds(65,15,40,28); butz.setFont(new java.awt.Font("Dialog", 1, 20)); butz.addActionListener(this); Butt.add(butz); Button butcf = new Button("/"); butcf.setBounds(115,15,40,28); butcf.setFont(new java.awt.Font("Dialog", 0, 20)); butcf.addActionListener(this); Butt.add(butcf); Button butc = new Button("*"); butc.setBounds(165,15,40,28); butc.setFont(new java.awt.Font("Dialog", 1, 25)); butc.addActionListener(this); Butt.add(butc); Button but7 = new Button("7"); but7.setBounds(15,55,40,28); but7.setFont(new java.awt.Font("Dialog", 0, 15)); but7.addActionListener(this); Butt.add(but7); Button but8 = new Button("8"); but8.setBounds(65,55,40,28); but8.setFont(new java.awt.Font("Dialog", 0, 15)); but8.addActionListener(this); Butt.add(but8); Button but9 = new Button("9"); but9.setBounds(115,55,40,28); but9.setFont(new java.awt.Font("Dialog", 0, 15)); but9.addActionListener(this); Butt.add(but9); Button butjf = new Button("-"); butjf.setBounds(165,55,40,28); butjf.setFont(new java.awt.Font("Dialog", 0, 28)); butjf.addActionListener(this); Butt.add(butjf); Button but4 = new Button("4"); but4.setBounds(15,95,40,28); but4.setFont(new java.awt.Font("Dialog", 0, 15)); but4.addActionListener(this); Butt.add(but4); Button but5 = new Button("5"); but5.setBounds(65,95,40,28); but5.setFont(new java.awt.Font("Dialog", 0, 15)); but5.addActionListener(this); Butt.add(but5); Button but6 = new Button("6"); but6.setBounds(115,95,40,28); but6.setFont(new java.awt.Font("Dialog", 0, 15)); but6.addActionListener(this); Butt.add(but6); Button butj = new Button("+"); butj.setBounds(165,95,40,28); butj.setFont(new java.awt.Font("Dialog", 0, 20)); butj.addActionListener(this); Butt.add(butj); Button but1 = new Button("1"); but1.setBounds(15,135,40,28); but1.setFont(new java.awt.Font("Dialog", 0, 15)); but1.addActionListener(this); Butt.add(but1); Button but2 = new Button("2"); but2.setBounds(65,135,40,28); but2.setFont(new java.awt.Font("Dialog", 0, 15)); but2.addActionListener(this); Butt.add(but2); Button but3 = new Button("3"); but3.setBounds(115,135,40,28); but3.setFont(new java.awt.Font("Dialog", 0, 15)); but3.addActionListener(this); Butt.add(but3); Button bute = new Button("="); bute.setBounds(165,135,40,68); bute.setFont(new java.awt.Font("Dialog", 0, 25)); bute.addActionListener(this); Butt.add(bute); Button but0 = new Button("0"); but0.setBounds(15,175,90,28); but0.setFont(new java.awt.Font("Dialog", 0, 15)); but0.addActionListener(this); Butt.add(but0); Button butd = new Button("."); butd.setBounds(115,175,40,28); butd.setFont(new java.awt.Font("Dialog", 1, 25)); butd.addActionListener(this); Butt.add(butd); } public void actionPerformed(ActionEvent event) { String sf = event.getActionCommand(); if(sf.equals("9")||sf.equals("8")||sf.equals("7")||sf.equals("6")||sf.equals("5")||sf.equals("4")||sf.equals("3")||sf.equals("2")||sf.equals("1")||sf.equals("0")||sf.equals("C")||sf.equals("←")||sf.equals(".")) { String input = sf; if (start) { txt.setText(""); start = false; } if (input.equals("←")) { String str = txt.getText(); if (str.length() > 0) txt.setText(str.substring(0, str.length() - 1)); } else if (input.equals("C")) { txt.setText("0"); start = true; } else txt.setText(txt.getText() + input); } else { String command = sf; if (start) { cmd = command; } else { calculate(Double.parseDouble(txt.getText())); cmd = command; start = true; } } } public void calculate(double x) { if (cmd.equals("+")) result += x; else if (cmd.equals("-")) result -= x; else if (cmd.equals("*")) result *= x; else if (cmd.equals("/")) result /= x; else if (cmd.equals("=")) result = x; txt.setText("" + result); } public static void main (String[] args) { new MyCalculator(); } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值