java 学习 ——计算器小程序

简易计算器小程序代码:

package jisuanqi;

//声明须要插入的包
import java.awt.*;
import java.lang.Object;
import java.lang.String;
import javax.swing.*;
import java.awt.event.*;
import java.awt.TextComponent;
//声明一个主类jisuanqi。继续窗体类Frame
public class jisuanqi extends Frame{
	
	//声明16个Button
	Button anjian1, anjian2, anjian3, anjian4, anjian5, anjian6, anjian7, anjian8, anjian9, anjian10, anjian11, anjian12, anjian13, anjian14,anjian15,anjian16;
	//声明文本框。用于接收数字和计算
	TextField numText;
	//声明面板容器,用于存放各种button
	Panel anjianPanel;
	//构造方法初始化button
	jisuanqi(String title){
		
		super(title);
		anjian1=new Button("0");
		anjian2=new Button("1");
		anjian3=new Button("2");
		anjian4=new Button("3");
		anjian5=new Button("4");
		anjian6=new Button("5");
		anjian7=new Button("6");
		anjian8=new Button("7");
		anjian9=new Button("8");
		anjian10=new Button("9");
		anjian11=new Button("+");
		anjian12=new Button("-");
		anjian13=new Button("*");
		anjian14=new Button("/");
		anjian15=new Button("=");
		anjian16=new Button("清空");
		//初始化文本框。将文本的值设置为“0”
		numText=new TextField("0");
		//实例化面板;
		anjianPanel=new Panel();
		//设置button
		anjianPanel.setLayout(new FlowLayout());
		//实例化button
		anjian1.addActionListener(new anjianAction());
		anjian2.addActionListener(new anjianAction());
		anjian3.addActionListener(new anjianAction());
		anjian4.addActionListener(new anjianAction());
		anjian5.addActionListener(new anjianAction());
		anjian6.addActionListener(new anjianAction());
		anjian7.addActionListener(new anjianAction());
		anjian8.addActionListener(new anjianAction());
		anjian9.addActionListener(new anjianAction());
		anjian10.addActionListener(new anjianAction());
		anjian11.addActionListener(new anjianAction());
		anjian12.addActionListener(new anjianAction());
		anjian13.addActionListener(new anjianAction());
		anjian14.addActionListener(new anjianAction());
		anjian15.addActionListener(new anjianAction());
		anjian16.addActionListener(new anjianAction());
		//将各个button加入到面板容器中
		anjianPanel.add(anjian1);
		anjianPanel.add(anjian2);
		anjianPanel.add(anjian3);
		anjianPanel.add(anjian4);
		anjianPanel.add(anjian5);
		anjianPanel.add(anjian6);
		anjianPanel.add(anjian7);
		anjianPanel.add(anjian8);
		anjianPanel.add(anjian9);
		anjianPanel.add(anjian10);
		anjianPanel.add(anjian11);
		anjianPanel.add(anjian12);
		anjianPanel.add(anjian13);
		anjianPanel.add(anjian14);
		anjianPanel.add(anjian15);
		anjianPanel.add(anjian16);
		//为文本框加入事件
		numText.addTextListener(new TextListener() {
			
			public void textValueChanged(TextEvent e) {
				
				if(numText.getText().indexOf("0",0)!=-1){
					
					numText.getText().replace("0","");
				}
			}
		});
		
		addWindowListener(new WindowAdapter() {
			
			public void windowClosing(WindowEvent e) {
				
				System.exit(0);
			}
		});
		//将文本框放置在顶部
		add(numText,BorderLayout.NORTH);
		//加入button
		add(anjianPanel,BorderLayout.CENTER);
		//设置文本框属性
		numText.setEditable(false);
	}
	//没有不论什么操作的值
	int num=0;
	//numStr是文本框的值
	String numStr=null;
	Boolean chinage=false;
	//设置button
	public int check(){
	numStr = numText.getText();
	if (numStr.indexOf("+") == 0) {
		
		numStr = numStr.replace('+', '0');
		num = num + Integer.parseInt(numStr);
		return num;
	}
	else if (numStr.indexOf("-") != -1) {
		
		numStr = numStr.replace("-", "0");
		num = num - Integer.parseInt(numStr);
		return num;
	}
	else if(numStr.indexOf("*")!=-1){
		
		numStr=numStr.replace('*','0');
		num = num * Integer.parseInt(numStr);
		return num;
	}
	else if(numStr.indexOf("/")!=-1){
		
		numStr=numStr.replace('/','0');
		try{
			
			num = num / Integer.parseInt(numStr);
			return num;
		}
		catch(ArithmeticException e){
			
			JOptionPane.showMessageDialog(null,"除数不能为空!","消息!",1);
			return num;
		}
	}
	else return num=Integer.parseInt(numStr);
	}
	//创建事件
	private class anjianAction implements ActionListener{
		
		public void actionPerformed(ActionEvent event){
		
			if(event.getActionCommand()=="0"){
		
				if(!numText.getText().equals("0"))
					numText.setText(numText.getText()+0);
			}
	
			if(event.getActionCommand()=="1")
				numText.setText(numText.getText()+1);
			if(event.getActionCommand()=="2")
				numText.setText(numText.getText()+2);
			if(event.getActionCommand()=="3")
				numText.setText(numText.getText()+3);
			if(event.getActionCommand()=="4")
				numText.setText(numText.getText()+4);
			if(event.getActionCommand()=="5")
				numText.setText(numText.getText()+5);
			if(event.getActionCommand()=="6")
				numText.setText(numText.getText()+6);
			if(event.getActionCommand()=="7")
				numText.setText(numText.getText()+7);
			if(event.getActionCommand()=="8")
				numText.setText(numText.getText()+8);
			if(event.getActionCommand()=="9")
				numText.setText(numText.getText()+9);
	
			if(event.getActionCommand()=="清空"){
		
				numText.setText("0");
				num=0;
			}
			if(event.getActionCommand()=="+"){
		
				//调用check();方法
				check();
				numText.setText("+");
			}
			if(event.getActionCommand()=="-"){
				
				check();
				numText.setText("-");
			}
			if(event.getActionCommand()=="*"){
		
				check();
				numText.setText("*");
			}
			if(event.getActionCommand()=="/"){
				
				if(numStr=="/0"){
		
					JOptionPane.showMessageDialog(null,"除数不能为空!","消息!",1);
				}
				check();
				numText.setText("/");
			}
			if(event.getActionCommand()=="="){
		
				check();
				numText.setText(""+num);
			}
		}
	}
	public static void main(String[] args){
		
		jisuanqi jisuanqi=new jisuanqi("计算器");
		jisuanqi.setBackground(Color.CYAN);
		jisuanqi.setSize(300,280);
		//设置窗体大小
		jisuanqi.setLocation(550,350);
		//设置界面属性
		jisuanqi.setVisible(true);
	}
}


展开阅读全文

程序:加法计算器

06-09

你们好,这个计算器我用MyEclipse编译没有错误,但是运行的时候算不了加法,大家能帮我看看吗,谢谢了!rnimport javax.swing.*;rnimport java.awt.*;rnimport java.awt.event.*;rnimport java.applet.*;rnpublic class calculator extends JPanel implements ActionListenerrnJButton bt1,bt2,bt3,bt4,bt5,bt6,bt7,bt8,bt9,bt0;rnJButton addbutton;rnJButton reset;rnJButton equalbutton;rnJTextField result;rnint flag =0,calresult;rnInteger rt,rs;rnString current; //当前值rnString tem; //显示屏上的值rnStringBuffer number;rnpublic calculator( ) rnrt = new Integer(0);rnrs =new Integer(0);rnnumber =new StringBuffer( );rnresult = new JTextField(8);rnresult.setHorizontalAlignment(JTextField.RIGHT);rnbt1 =new JButton("1");rnbt1.addActionListener(this);rnbt2 =new JButton("2");rnbt2.addActionListener(this);rnbt3 =new JButton("3");rnbt3.addActionListener(this);rnbt4 =new JButton("4");rnbt4.addActionListener(this);rnbt5 =new JButton("5");rnbt5.addActionListener(this);rnbt6 =new JButton("6");rnbt6.addActionListener(this);rnbt7 =new JButton("7");rnbt7.addActionListener(this);rnbt8 =new JButton("8");rnbt8.addActionListener(this);rnbt9 =new JButton("9");rnbt9.addActionListener(this);rnbt0 =new JButton("0");rnbt0.addActionListener(this);rnaddbutton=new JButton("+");rnaddbutton.addActionListener(this);rnreset = new JButton("reset");rnreset.addActionListener(this);rnsetLayout(new BorderLayout(20,20));rnadd(result,BorderLayout.NORTH);rnJPanel numberpanel=new JPanel();rnnumberpanel.setLayout(new GridLayout(4,3,5,5));rnnumberpanel.add(bt1);rnnumberpanel.add(bt2);rnnumberpanel.add(bt3);rnnumberpanel.add(bt4);rnnumberpanel.add(bt5);rnnumberpanel.add(bt6);rnnumberpanel.add(bt7);rnnumberpanel.add(bt8);rnnumberpanel.add(bt9);rnnumberpanel.add(bt0);rnnumberpanel.add(addbutton);rnadd(numberpanel,BorderLayout.CENTER);rnJPanel controlpanel=new JPanel( );rncontrolpanel.setLayout(new GridLayout(1,2));rncontrolpanel.add(reset);rnequalbutton=new JButton("=");rnequalbutton.addActionListener(this);rnequalbutton.setEnabled(false);rncontrolpanel.add(equalbutton);rnadd(controlpanel,BorderLayout.SOUTH);rnrnpublic void actionPerformed(ActionEvent evt)rnObject source =evt.getSource();rnif(source == bt1)number.append("1");rnelse if (source == bt2)number.append("2");rnelse if (source == bt3)number.append("3");rnelse if (source == bt4)number.append("4");rnelse if (source == bt5)number.append("5");rnelse if (source ==bt6)number.append("6");rnelse if (source ==bt7)number.append("7");rnelse if (source ==bt8)number.append("8");rnelse if (source ==bt9)number.append("9");rnelse if (source ==bt0)number.append("0");rnresult.setText(number.toString());rnif(flag ==0)rncurrent = number.toString();rnequalbutton.setEnabled(false);rnrnif(flag == 1)rnrntem=number.toString();rnequalbutton.setEnabled(true);rnrnif(source == addbutton)rnflag = 1;number=new StringBuffer();rnequalbutton.setEnabled(false);rnif(source == equalbutton)rnrnrs=Integer.valueOf(tem);rnrt=Integer.valueOf("");rnif(flag== 1)rncalresult=rt.intValue()+rs.intValue();rnresult.setText(String.valueOf(calresult));rnflag=0;rnequalbutton.setEnabled(false);rnnumber=new StringBuffer();rnrnif(source == reset)rnnumber = new StringBuffer();rnequalbutton.setEnabled(false);rnflag=0;rnrnrnpublic static void main(String set[ ])rnJFrame count = new JFrame("计算器");rncount.addWindowListener(new WindowAdapter( )rnpublic void windowClosing(WindowEvent e)rnSystem.exit(0);rnrn);rncalculator cou =new calculator( );rncount.getContentPane( ).add(cou);rncount.setSize(200,200);rncount.setVisible(true);rnrnrnrn 论坛

没有更多推荐了,返回首页