⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mycaculator.java

📁 用Java编写的简易计算器
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MyCaculator extends JFrame 
{
	//public static final int TEXT_CURSOR
	private JPanel p1,p2,p3;
	private JTextField text;  //显示框
	private JButton btn[];
	private JButton bBack,bSqrt,bAdd,bSub,bMul,bDiv,bClear,bResult,bPoint,bEqu;
	private Container container;
	private String lastCommand;//保存+,-,*,/,=命令 
	private double result;//保存计算结果 
	private boolean start;//判断是否为数字的开始 
		
	public MyCaculator()
	{
		super( "Windows计算器");
		container=getContentPane();
		container.setLayout(new FlowLayout());
		
		start=true; 
        result=0;
        lastCommand = "="; 
		
		p1=new JPanel(new GridLayout(1,1,1,1));
		p2=new JPanel(new GridLayout(1,2,1,1));
		
		text=new JTextField(" ",17);
		text.setHorizontalAlignment(JTextField.RIGHT);
		//Cursor cursor=text;
		//text.beforeFirst(); // move cursor before the first row


		btn=new JButton[16];
		String b[]={"1","2","3","+","4","5","6","-","7","8","9","*","0",".","+/-","/"};
		for(int i=0;i<16;i++)
		{
			btn[i]=new JButton(b[i]);
	    }
	    
	    ActionListener insert = new InsertAction(); 
		ActionListener command = new CommandAction();
		 
		for(int i=0;i<3;i++)
		{
			btn[i].addActionListener(insert);
		}
		btn[3].addActionListener(command);
		
		for(int i=4;i<7;i++)
		{
			btn[i].addActionListener(insert);
		}
		btn[7].addActionListener(command);
		
		for(int i=8;i<11;i++)
		{
			btn[i].addActionListener(insert);
		}
		btn[11].addActionListener(command);
		
		btn[12].addActionListener(insert);
		
		btn[13].addActionListener(insert);
		
		btn[14].addActionListener(insert);
		
		btn[15].addActionListener(command);
	    
	    bBack=new JButton("←");
	    bClear=new JButton("Clear");
	    bResult=new JButton("=");
	    
	    bBack.addActionListener(insert);
	    
	    bClear.addActionListener(insert);
	    
	    bResult.addActionListener(command);
	    
	    
	    btn[3].setForeground(Color.red);//设置背景色
	    btn[7].setForeground(Color.red);
	    btn[11].setForeground(Color.red);
	    btn[15].setForeground(Color.red);
	    
		p1.add(text);
		
		p2.add(bBack);p2.add(bClear);p2.add(bResult);
			
		p3=new JPanel(new GridLayout(4,4,1,1));
		
		for(int i=0;i<16;i++)
		{
			p3.add(btn[i]);
		}
	
		container.add(p1);
		container.add(p2);
		container.add(p3);
		
		setSize(210,220);
      	setVisible( true );
    }
    
	private class InsertAction implements ActionListener 
	{ 

		public void actionPerformed(ActionEvent event) 
		{
			String input=event.getActionCommand(); 
			if (start) 
			{ 
				text.setText(""); 
				start=false; 
				if(input.equals("+/-")) 
				text.setText(text.getText()+"-"); 
		    }
			if(!input.equals("+/-")) 
			{
				if(input.equals("←")) 
				{ 
					String str=text.getText(); 
					if(str.length()>0) 
					text.setText(str.substring(0,str.length()-1));//str.substring(begin,end)返回指定的子字符串.第一个字符开始编号为0,返回字符串长度为end-begin. 
				}
				else if(input.equals("Clear")) 
				{ 
					text.setText("0"); 
					start=true; 
				}
				else 
					text.setText(text.getText()+input); 
			} 
		} 
	} 

	private class CommandAction implements ActionListener 
	{
		public void actionPerformed(ActionEvent evt) 
		{ 
			String command=evt.getActionCommand(); 
			if(start) 
			{ 
				lastCommand=command; 
			}
			else 
			{ 
				calculate(Double.parseDouble(text.getText())); 
				lastCommand=command; 
				start=true; 
			} 
		} 
	} 

	public void calculate(double x) 
	{ 
		if (lastCommand.equals("+")) result+= x; 
		else if (lastCommand.equals("-")) result-=x; 
		else if (lastCommand.equals("*")) result*=x; 
		else if (lastCommand.equals("/")) result/=x; 
		else if (lastCommand.equals("=")) result=x; 
		text.setText(""+ result); 
	} 
     
    public static void main( String args[] )
   {
      MyCaculator application = new MyCaculator();
      application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
   }  			
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -