第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

計算器在計算時輸入后一個數(shù)時前一個就在顯示屏上自動消失了,想讓它顯示全部操作過程,

計算器在計算時輸入后一個數(shù)時前一個就在顯示屏上自動消失了,想讓它顯示全部操作過程,

慕雪6494856 2016-04-20 22:01:48
import javax.swing.*; ?import javax.swing.JTextField; ?import java.awt.*; ?import java.awt.event.*; ?import java.lang.*; ?import java.awt.Color; ?????public class A extends JFrame implements ActionListener { //JFrame(框架)為主程序窗口? ? private JPanel p1 = new JPanel(); ?//創(chuàng)建面板 ?? ? private JPanel p2 = new JPanel(); ?? ? private JTextField t1; ? ? ?//文本框1用來顯示輸入信息 ?? ? private JTextField t2; ? ? ?//文本框2用來顯示結(jié)果信息 ?? ? private JLabel label; ? ? ? //標簽信息 ?? ? StringBuffer str; ? ? ? ? ? //顯示屏所顯示的字符串 ??? ? double x,y; ? ? ? ? ? ? ? ? //x和y都是運算數(shù) ??? ? int z; ? ? ? ? ? ? ? ? ? ? ?//Z表示單擊了那一個運算符.0表示"+",1表示"-",2表示"*",3表示"/" ? ? ? ? ? ?? ? private JButton b[] = new JButton[12]; ?//創(chuàng)建一個有12個按鈕的數(shù)組 ?? ? private JButton b1,b2,b3,b4,b5,b6,b7,b8; //算術(shù)功能按鈕 ?????? ? public A() ?? ? { ?? ? ? ? super("A-Wa-JISUNQI"); ? ? //窗口名稱 ?? ? ? ? Container c = getContentPane(); //創(chuàng)建內(nèi)容面板對象 ???? ? ? ? t1 = new JTextField(30); ?? ? ? ? t1.setEditable(false); ?//只能顯示,不能編輯 ?? ? ? ? t2 = new JTextField(30); ?? ? ? ? t2.setEditable(false); ?//只能顯示,不能編輯?? ? ? ??? ? ? ? label = new JLabel("歡迎使用A-Wa計算器"); ?? ? ? ? label.setForeground(Color.blue); ???? ? ? ? //創(chuàng)建一個空字符串緩沖區(qū) ??? ? ? ? str=new StringBuffer(); ????? ? ? ? p2.add(label); ?//添加標簽到面板 ?? ? ? ? p2.add(t2); ? ? //添加文本框到面板 ?? ? ? ? p2.add(t1); ? ? //添加文本框到面板 ?? ? ? ? p2.setLayout(new GridLayout(4,1)); //把面扳布局為4行1列 ???? ? ? ? for(int i=0;i<10;i++) ? ?//分別為數(shù)組中0~9號的按鈕設(shè)置標簽,并注冊監(jiān)聽器 ?? ? ? ? { ?? ? ? ? ? ? String s=""+i; ?? ? ? ? ? ? b[i]= new JButton(s); ? ??? ? ? ? ? ? b[i].addActionListener(this); ? ??? ? ? ? } ?? ? ? ? ??? ? ? ? //實例化各個按鈕 ?? ? ? ? b[10]= new JButton("-/+"); ?b[11]= new JButton("."); ?? ? ? ? b1= new JButton("/"); ? ? ? b2= new JButton("Back"); ?? ? ? ? b3= new JButton("*"); ? ? ? b4= new JButton("C"); ?? ? ? ? b5= new JButton("+"); ? ? ? b6= new JButton("Sqrt"); ?? ? ? ? b7= new JButton("-"); ? ? ? b8= new JButton("="); ?? ? ? ? ??? ? ? ? //設(shè)置按鈕前景色 ?? ? ? ? for(int i=0;i<12;i++) ?? ? ? ? { ?? ? ? ? ? ? b[i].setForeground(Color.red); ?? ? ? ? } ???? ? ? ? b1.setForeground(Color.red); ? ?b3.setForeground(Color.red); ?? ? ? ? b5.setForeground(Color.red); ? ?b7.setForeground(Color.red); ?? ? ? ? b2.setForeground(Color.blue); ? b4.setForeground(Color.blue); ?? ? ? ? b6.setForeground(Color.red); ? ? b8.setForeground(Color.blue); ???? ? ? ? ??? ? ? ? //添加到面板 ?? ? ? ? p1.add(b[7]); p1.add(b[8]); p1.add(b[9]); p1.add(b1); p1.add(b2); ?? ? ? ? p1.add(b[4]); p1.add(b[5]); p1.add(b[6]); p1.add(b3); p1.add(b4); ?? ? ? ? p1.add(b[1]); p1.add(b[2]); p1.add(b[3]); p1.add(b5); p1.add(b6); ?? ? ? ? p1.add(b[0]); p1.add(b[10]); p1.add(b[11]);p1.add(b7);p1.add(b8); ?? ? ? ? p1.setLayout(new GridLayout(4,5,5,5)); ???? ? ? ? ??? ? ? ? //注冊監(jiān)聽器 ?? ? ? ? b[10].addActionListener(this); ?b[11].addActionListener(this); ?? ? ? ? b1.addActionListener(this); b2.addActionListener(this); ?? ? ? ? b3.addActionListener(this); b4.addActionListener(this); ?? ? ? ? b5.addActionListener(this); b6.addActionListener(this); ?? ? ? ? b7.addActionListener(this); b8.addActionListener(this); ?? ? ??? ? ? ? //將面板添加到內(nèi)容面板 ?? ? ? ? c.add(p2); ?? ? ? ? c.add(p1); ?? ? ? ? c.setLayout(new FlowLayout()); ?//設(shè)置為順序布局 ?? ? ? ? ??? ? ? ? ??? ? ? ? ??? ? ? ? setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //設(shè)置窗口關(guān)閉動作 ?? ? ? ? setVisible(true); ? //設(shè)置為可見 ?? ? ? ? setResizable(false); //禁止調(diào)整框架大小 ?? ? ? ? setSize(400,300); ? //設(shè)置窗口大小 ???? ? } ???? ? ??? ? //主方法實現(xiàn)創(chuàng)建一個窗口 ?? ? public static void main(String[] args) ?? ? { ?A f = new A(); } ???? ? ??? ? //按鈕的事件處理 ?? ? public void actionPerformed(ActionEvent e) ?? ? { ?? ? try ?? ? { ?? ? ? ? if(e.getSource()==b4) ?{ //選擇"C"清零 ? ?? ? ? ? ? ? t1.setText("0"); ?//把文本框清零 ?? ? ? ? ? ? t1.setHorizontalAlignment(JTextField.LEFT); ? ?//文本對齊右邊 ?? ? ? ? ? ? str.setLength(0); //清空字符串緩沖區(qū)以準備接收新的輸入運算數(shù) ?? ? ? ? } ?? ? ? ? else if(e.getSource()==b[10])//單擊"+/-"選擇輸入的運算數(shù)是正數(shù)還是負數(shù) ??? ? ? ? { ?? ? ? ? ? ? x=Double.parseDouble(t1.getText().trim());//trim函數(shù)作用是去掉字符串中的空格 ?? ? ? ? ? ? t1.setText(""+(-x)); ?? ? ? ? ? ? t1.setHorizontalAlignment(JTextField.RIGHT); ?? ? ? ? } ?? ? ? ? else if (e.getSource()==b5)//單擊加號按鈕獲得x的值和z的值并清空y的值 ?? ? ? ? { ?? ? ? ? ? ? x=Double.parseDouble(t1.getText().trim()); ?? ? ? ? ? ? str.setLength(0); ?? ? ? ? ? ? y=0d; ?? ? ? ? ? ? z=0; ?? ? ? ? } ?? ? ? ? else if(e.getSource()==b7)//單擊減號按鈕獲得x的值和z的值并清空y的值 ??? ? ? ? { ?? ? ? ? ? ? x=Double.parseDouble(t1.getText().trim()); ?? ? ? ? ? ? str.setLength(0); ?? ? ? ? ? ? y=0d; ?? ? ? ? ? ? z=1; ?? ? ? ? } ?? ? ? ? else if(e.getSource()==b3)//單擊乘號按鈕獲得x的值和z的值并清空y的值 ??? ? ? ? { ?? ? ? ? ? ? x=Double.parseDouble(t1.getText().trim()); ?? ? ? ? ? ? str.setLength(0); ?? ? ? ? ? ? y=0d; ?? ? ? ? ? ? z=2; ?? ? ? ? } ?? ? ? ? else if(e.getSource()==b1)//單擊除號按鈕獲得x的值和z的值并清空y的值 ??? ? ? ? { ?? ? ? ? ? ? x=Double.parseDouble(t1.getText().trim()); ?? ? ? ? ? ? str.setLength(0); ?? ? ? ? ? ? y=0d; ?? ? ? ? ? ? z=3; ?? ? ? ? } ?? ? ? ? else if(e.getSource()==b8)//單擊等號按鈕輸出計算結(jié)果 ??? ? ? ? { ?? ? ? ? ? ? str.setLength(0); ?? ? ? ? ? ? switch(z) ?? ? ? ? ? ? { ?? ? ? ? ? ? ? ? case 0: t1.setText(""+(x+y)); t1.setHorizontalAlignment(JTextField.RIGHT);break; ?? ? ? ? ? ? ? ? case 1: t1.setText(""+(x-y)); t1.setHorizontalAlignment(JTextField.RIGHT);break; ?? ? ? ? ? ? ? ? case 2: t1.setText(""+(x*y)); t1.setHorizontalAlignment(JTextField.RIGHT);break; ?? ? ? ? ? ? ? ? case 3: t1.setText(""+(x/y)); t1.setHorizontalAlignment(JTextField.RIGHT);break; ?? ? ? ? ? ? } ?? ? ? ? } ?? ? ? ? else if(e.getSource()==b[11])//單擊"."按鈕輸入小數(shù) ??? ? ? ? { ?? ? ? ? ? ? if(t1.getText().trim().indexOf('.')!=-1)//判斷字符串中是否已經(jīng)包含了小數(shù)點 ?? ? ? ? ? ? { ?? ? ? ? ? ? } ?? ? ? ? ? ? else //如果沒有小數(shù)點 ?? ? ? ? ? ? { ?? ? ? ? ? ? ? ? if(t1.getText().trim().equals("0"))//如果初時顯示為0 ??? ? ? ? ? ? ? ? { ?? ? ? ? ? ? ? ? ? ? t1.setText(str.append(e.getActionCommand()).toString()); ?? ? ? ? ? ? ? ? ? ? t1.setHorizontalAlignment(JTextField.RIGHT); ?? ? ? ? ? ? ? ? } ?? ? ? ? ? ? ? ? else if(t1.getText().trim().equals(""))//如果初時顯示為空則不做任何操作 ?? ? ? ? ? ? ? ? {} ?? ? ? ? ? ? ? ? else ??? ? ? ? ? ? ? ? { ??? ? ? ? ? ? ? ? ? ? t1.setText(str.append(e.getActionCommand()).toString()); ??? ? ? ? ? ? ? ? ? ? t1.setHorizontalAlignment(JTextField.RIGHT); ?? ? ? ? ? ? ? ? } ???? ? ? ? ? ? } ?? ? ? ? y=0d; ?? ? ? ? } ???? ? ? ? else if(e.getSource()==b6) //求平方根 ??? ? ? ? { ?? ? ? ? ? ? x=Double.parseDouble(t1.getText().trim()); ??? ? ? ? ? ? if(x<0) ?? ? ? ? ? ? { ?? ? ? ? ? ? ? ? t1.setText("數(shù)字格式異常"); ?? ? ? ? ? ? ? ? t1.setHorizontalAlignment(JTextField.RIGHT); ?? ? ? ? ? ? } ?? ? ? ? ? ? else ?? ? ? ? ? ? { ?? ? ? ? ? ? ? ? t1.setText(""+Math.sqrt(x)); ?? ? ? ? ? ? ? ? t1.setHorizontalAlignment(JTextField.RIGHT); ?? ? ? ? ? ? } ?? ? ? ? ? ? str.setLength(0); ?? ? ? ? ? ? y=0d; ?? ? ? ? } ???? ? ? ? else ?? ? ? ? { ?? ? ? ? ? ? if(e.getSource()==b[0])//如果選擇的是"0"這個數(shù)字鍵 ??? ? ? ? ? ? { ?? ? ? ? ? ? ? ? if(t1.getText().trim().equals("0"))//如果顯示屏顯示的為零不做操作 ??? ? ? ? ? ? ? ? {} ?? ? ? ? ? ? ? ? else ?? ? ? ? ? ? ? ? ? ? t1.setText(str.append(e.getActionCommand()).toString()); ?? ? ? ? ? ? ? ? ? ? t1.setHorizontalAlignment(JTextField.RIGHT); ?? ? ? ? ? ? ? ? ? ? y=Double.parseDouble(t1.getText().trim()); ??? ? ? ? ? ? } ?? ? ? ? ? ? else if (e.getSource()==b2) //選擇的是back鍵 ?? ? ? ? ? ? { ?? ? ? ? ? ? ? ? if(!t1.getText().trim().equals("0"))//如果顯示屏顯示的不是零 ??? ? ? ? ? ? ? ? { ?? ? ? ? ? ? ? ? if(str.length()!=1) ??? ? ? ? ? ? ? ? { ??? ? ? ? ? ? ? ? ? ? t1.setText(str.delete(str.length()-1,str.length()).toString());//可能拋出字符串越界異常 ??? ? ? ? ? ? ? ? ? ? t1.setHorizontalAlignment(JTextField.RIGHT); ?? ? ? ? ? ? ? ? } ?? ? ? ? ? ? ? ? else ??? ? ? ? ? ? ? ? { ??? ? ? ? ? ? ? ? ? ? t1.setText("0"); t1.setHorizontalAlignment(JTextField.RIGHT); ?? ? ? ? ? ? ? ? ? ? str.setLength(0); ??? ? ? ? ? ? ? ? } ??? ? ? ? ? ? } ??? ? ? ? ? ? y=Double.parseDouble(t1.getText().trim()); ??? ? ? ? ? ? } ?? ? ? ? ? ? else ??? ? ? ? ? ? ? ? { ?? ? ? ? ? ? ? ? t1.setText(str.append(e.getActionCommand()).toString()); ??? ? ? ? ? ? ? ? t1.setHorizontalAlignment(JTextField.RIGHT); ?? ? ? ? ? ? ? ? y=Double.parseDouble(t1.getText().trim()); ?? ? ? ? ? ? ? ? } ?? ? ? ? ? ? } ?? ? ? ? } ?? ? ? ? catch(NumberFormatException e1){ t1.setText("數(shù)字格式異常"); ?? ? ? ? t1.setHorizontalAlignment(JTextField.RIGHT); } ????? ? ? ? catch(StringIndexOutOfBoundsException e1){t1.setText("字符串索引越界"); ?? ? ? ? t1.setHorizontalAlignment(JTextField.RIGHT);} ??? ? } ?}?
查看完整描述

1 回答

?
_坐看云起時

TA貢獻9條經(jīng)驗 獲得超9個贊

你可以把要輸入的計算表達式用一個字符串記錄下來,然后進行分割、數(shù)據(jù)類型轉(zhuǎn)換。

我以前做過一個簡單的計算器,代碼如下:


import?java.awt.BorderLayout;
import?java.awt.EventQueue;

import?javax.swing.JFrame;
import?javax.swing.JPanel;
import?javax.swing.border.EmptyBorder;
import?javax.swing.JTextField;
import?javax.swing.JButton;
import?java.awt.Font;
import?java.awt.event.ActionListener;
import?java.awt.event.ActionEvent;

public?class?calculatorTest?extends?JFrame?{

	private?JPanel?contentPane;
	private?JTextField?textField;
	private?JButton?button_08;
	private?JButton?button_09;
	private?JButton?button_04;
	private?JButton?button_05;
	private?JButton?button_06;
	private?JButton?button_01;
	private?JButton?button_02;
	private?JButton?button_03;
	private?JButton?add;
	private?JButton?subtract;
	private?JButton?multiply;
	private?JButton?button_11;
	private?JButton?division;
	private?JButton?result;
	private?JButton?btnC;
	
	private?StringBuffer?buffer1=new?StringBuffer();	//用于存儲用戶輸入的計算表達式

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

	/**
	?*?Create?the?frame.
	?*/
	public?calculatorTest()?{
		setResizable(false);
		setTitle("\u8BA1\u7B97\u5668");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100,?100,?314,?325);
		contentPane?=?new?JPanel();
		contentPane.setBorder(new?EmptyBorder(5,?5,?5,?5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		textField?=?new?JTextField();
		textField.setEditable(false);
		textField.setFont(new?Font("宋體",?Font.BOLD,?25));
		textField.setBounds(0,?0,?308,?42);
		contentPane.add(textField);
		textField.setColumns(10);
		
		JButton?button_07?=?new?JButton("7");
		button_07.addActionListener(new?ActionListener()?{
			public?void?actionPerformed(ActionEvent?e)?{
				buffer1.append(7);	//向記錄運算表達式的字符串增加相應(yīng)的字符
				textField.setText(buffer1.toString());	//向文本框表達所輸入的內(nèi)容
			}
		});
		button_07.setFont(new?Font("宋體",?Font.BOLD,?12));
		button_07.setBounds(10,?52,?60,?40);
		contentPane.add(button_07);
		
		button_08?=?new?JButton("8");
		button_08.addActionListener(new?ActionListener()?{
			public?void?actionPerformed(ActionEvent?e)?{
				buffer1.append(8);
				textField.setText(buffer1.toString());
			}
		});
		button_08.setFont(new?Font("宋體",?Font.BOLD,?12));
		button_08.setBounds(80,?52,?60,?40);
		contentPane.add(button_08);
		
		button_09?=?new?JButton("9");
		button_09.addActionListener(new?ActionListener()?{
			public?void?actionPerformed(ActionEvent?e)?{
				buffer1.append(9);
				textField.setText(buffer1.toString());
			}
		});
		button_09.setFont(new?Font("宋體",?Font.BOLD,?12));
		button_09.setBounds(160,?52,?60,?40);
		contentPane.add(button_09);
		
		button_04?=?new?JButton("4");
		button_04.addActionListener(new?ActionListener()?{
			public?void?actionPerformed(ActionEvent?e)?{
				buffer1.append(4);
				textField.setText(buffer1.toString());
			}
		});
		button_04.setFont(new?Font("宋體",?Font.BOLD,?12));
		button_04.setBounds(10,?117,?60,?40);
		contentPane.add(button_04);
		
		button_05?=?new?JButton("5");
		button_05.addActionListener(new?ActionListener()?{
			public?void?actionPerformed(ActionEvent?e)?{
				buffer1.append(5);
				textField.setText(buffer1.toString());
			}
		});
		button_05.setFont(new?Font("宋體",?Font.BOLD,?12));
		button_05.setBounds(80,?117,?60,?40);
		contentPane.add(button_05);
		
		button_06?=?new?JButton("6");
		button_06.addActionListener(new?ActionListener()?{
			public?void?actionPerformed(ActionEvent?e)?{
				buffer1.append(6);
				textField.setText(buffer1.toString());
			}
		});
		button_06.setFont(new?Font("宋體",?Font.BOLD,?12));
		button_06.setBounds(160,?117,?60,?40);
		contentPane.add(button_06);
		
		button_01?=?new?JButton("1");
		button_01.addActionListener(new?ActionListener()?{
			public?void?actionPerformed(ActionEvent?e)?{
				buffer1.append(1);
				textField.setText(buffer1.toString());
			}
		});
		button_01.setFont(new?Font("宋體",?Font.BOLD,?12));
		button_01.setBounds(10,?179,?60,?40);
		contentPane.add(button_01);
		
		button_02?=?new?JButton("2");
		button_02.addActionListener(new?ActionListener()?{
			public?void?actionPerformed(ActionEvent?e)?{
				buffer1.append(2);
				textField.setText(buffer1.toString());
			}
		});
		button_02.setFont(new?Font("宋體",?Font.BOLD,?12));
		button_02.setBounds(80,?179,?60,?40);
		contentPane.add(button_02);
		
		button_03?=?new?JButton("3");
		button_03.addActionListener(new?ActionListener()?{
			public?void?actionPerformed(ActionEvent?e)?{
				buffer1.append(3);
				textField.setText(buffer1.toString());
			}
		});
		button_03.setFont(new?Font("宋體",?Font.BOLD,?12));
		button_03.setBounds(160,?179,?60,?40);
		contentPane.add(button_03);
		
		add?=?new?JButton("+");
		add.addActionListener(new?ActionListener()?{
			public?void?actionPerformed(ActionEvent?e)?{
				String?s=new?String(buffer1.toString());	//將運算表達式轉(zhuǎn)換成String類型
				//對所輸入的運算字符進行判斷
				if(buffer1.length()!=0&&s.indexOf('+')<0&&s.indexOf('-')<0&&s.indexOf('*')<0&&s.indexOf('/')<0&&s.indexOf('=')<0){
					buffer1.append("+");
					textField.setText(buffer1.toString());
				}
				else{}
			}
		});
		add.setFont(new?Font("宋體",?Font.BOLD,?12));
		add.setBounds(238,?52,?60,?40);
		contentPane.add(add);
		
		subtract?=?new?JButton("-");
		subtract.addActionListener(new?ActionListener()?{
			public?void?actionPerformed(ActionEvent?e)?{
				String?s=new?String(buffer1.toString());
				if(buffer1.length()!=0&&s.indexOf('+')<0&&s.indexOf('-')<0&&s.indexOf('*')<0&&s.indexOf('/')<0&&s.indexOf('=')<0){
					buffer1.append("-");
					textField.setText(buffer1.toString());
				}
				else{}
			}
		});
		subtract.setFont(new?Font("宋體",?Font.BOLD,?12));
		subtract.setBounds(238,?117,?60,?40);
		contentPane.add(subtract);
		
		multiply?=?new?JButton("*");
		multiply.addActionListener(new?ActionListener()?{
			public?void?actionPerformed(ActionEvent?e)?{
				String?s=new?String(buffer1.toString());
				if(buffer1.length()!=0&&s.indexOf('+')<0&&s.indexOf('-')<0&&s.indexOf('*')<0&&s.indexOf('/')<0&&s.indexOf('=')<0){
					buffer1.append("*");
					textField.setText(buffer1.toString());
				}
				else{}
			}
		});
		multiply.setFont(new?Font("宋體",?Font.BOLD,?12));
		multiply.setBounds(238,?179,?60,?40);
		contentPane.add(multiply);
		
		button_11?=?new?JButton("0");
		button_11.addActionListener(new?ActionListener()?{
			public?void?actionPerformed(ActionEvent?e)?{
				buffer1.append(0);
				textField.setText(buffer1.toString());
			}
		});
		button_11.setFont(new?Font("宋體",?Font.BOLD,?12));
		button_11.setBounds(10,?239,?60,?40);
		contentPane.add(button_11);
		
		division?=?new?JButton("/");
		division.addActionListener(new?ActionListener()?{
			public?void?actionPerformed(ActionEvent?e)?{
				String?s=new?String(buffer1.toString());
				if(buffer1.length()!=0&&s.indexOf('+')<0&&s.indexOf('-')<0&&s.indexOf('*')<0&&s.indexOf('/')<0&&s.indexOf('=')<0){
					buffer1.append("/");
					textField.setText(buffer1.toString());
				}
				else{}
			}
		});
		division.setFont(new?Font("宋體",?Font.BOLD,?12));
		division.setBounds(238,?239,?60,?40);
		contentPane.add(division);
		
		result?=?new?JButton("=");
		result.addActionListener(new?ActionListener()?{
			public?void?actionPerformed(ActionEvent?e)?{
				String?s=new?String(buffer1.toString());
				//對所輸入的運算字符進行判斷
				if(buffer1.length()!=0&&(s.indexOf('+')==0||s.indexOf('-')==0||s.indexOf('*')==0||s.indexOf('/')==0||s.indexOf('=')<0)){
					buffer1.append("=");
					textField.setText(buffer1.toString());
					String?str=new?String(buffer1.toString());
					String?str1[]=new?String[2];
					String?str2[]=new?String[2];
					//對運算符進行判斷,進行相應(yīng)的運算
					if(str.indexOf('+')>0){
						//對運算符前面字符串和運算符后面的字符串進行分割
						str1=str.split("\\+");
						//保存運算符前面的內(nèi)容并轉(zhuǎn)換數(shù)據(jù)類型
						double?number1=Double.parseDouble(str1[0]);
						str2=str1[1].split("\\=");
						//對分割后的字符串將等于號運算符前面字符串和等于號運算符后面的字符串進行分割并進行數(shù)據(jù)類型轉(zhuǎn)換
						double?number2=Double.parseDouble(str2[0]);
						double?number=number1+number2;
						textField.setText(str+number);
						//刪除運算表達式的內(nèi)容,以便下次運算
						buffer1.delete(0,?(buffer1.length()));
					}
					if(str.indexOf('-')>0){
						str1=str.split("\\-");
						double?number1=Double.parseDouble(str1[0]);
						str2=str1[1].split("\\=");
						double?number2=Double.parseDouble(str2[0]);
						double?number=number1-number2;
						textField.setText(str+number);
						buffer1.delete(0,?(buffer1.length()));
					}
					if(str.indexOf('*')>0){
						str1=str.split("\\*");
						double?number1=Double.parseDouble(str1[0]);
						str2=str1[1].split("\\=");
						double?number2=Double.parseDouble(str2[0]);
						double?number=number1*number2;
						textField.setText(str+number);
						buffer1.delete(0,?(buffer1.length()));
					}
					if(str.indexOf('/')>0){
						str1=str.split("\\/");
						double?number1=Double.parseDouble(str1[0]);
						str2=str1[1].split("\\=");
						double?number2=Double.parseDouble(str2[0]);
						double?number=number1/number2;
						textField.setText(str+number);
						buffer1.delete(0,?(buffer1.length()));
					}
				}
				else{}
			}
		});
		result.setFont(new?Font("宋體",?Font.BOLD,?12));
		result.setBounds(80,?239,?60,?40);
		contentPane.add(result);
		
		btnC?=?new?JButton("C");
		btnC.addActionListener(new?ActionListener()?{
			public?void?actionPerformed(ActionEvent?e)?{
				if(buffer1.length()>0){
					buffer1.deleteCharAt(buffer1.length()-1);
					textField.setText(buffer1.toString());
				}
				else{
					
				}
			}
		});
		btnC.setFont(new?Font("宋體",?Font.BOLD,?12));
		btnC.setBounds(160,?239,?60,?40);
		contentPane.add(btnC);
	}

}


查看完整回答
1 反對 回復(fù) 2016-04-21
  • 慕雪6494856
    慕雪6494856
    你能在我的代碼上把這個(把要輸入的計算表達式用一個字符串記錄下來,然后進行分割、數(shù)據(jù)類型轉(zhuǎn)換)給我演示一下嗎,我是菜鳥,確實不太會弄,謝謝大神哈哈
  • 1 回答
  • 0 關(guān)注
  • 3696 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號