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

📄 jishou.java

📁 JAVA学习后做的一个计算器,花了两个星期,效果还蛮好的
💻 JAVA
字号:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class jishou extends JFrame implements ActionListener
{
	JPanel jp;
	//申明要用到的组件。
	static JTextField jt1;
	JButton jb1,jb2,jb3,jb4,jb5,jb6,jb7,jb8,jb9,jb10,jb11,jb12,jb13;
	JButton jb14,jb15,jb16;
	JButton jb_out;
	
	GridBagLayout gb;
	GridBagConstraints gbc;
	//申明一数组来接收用户输入的数。
	static String sum="";
	//申明一个数来存储sum[0].
	static String returnString1="",returnString2="",returnString;;
	static boolean doubleClick=false;//判断输入了一个数。
	static boolean firstFlag=false;//用来判断是不是第二次输入。
	static boolean twoOpNumber=false;
	static int i=0;
	static char op;
	public jishou()
	{
		super("欢迎使用!");
		jp=new JPanel();
		setContentPane(jp);
		setDefaultCloseOperation(EXIT_ON_CLOSE);

		Font f=new Font("黑体",Font.BOLD,13);//设置字体属性

		//LEFT
		jt1=new JTextField("0",12);		
		//设置数字从右边出来。还有LEFT,CENTER;
		jt1.setHorizontalAlignment(jt1.RIGHT);
		jt1.setText("0");
		jb1=new JButton("1");
		jb1.addActionListener(this);
		jb1.setFont(f);

		jb2=new JButton("2");
		jb2.addActionListener(this);
		jb2.setFont(f);

		jb3=new JButton("3");
		jb3.addActionListener(this);
		jb3.setFont(f);

		jb4=new JButton("4");
		jb4.addActionListener(this);
		jb4.setFont(f);

		jb5=new JButton("5");
		jb5.addActionListener(this);
		jb5.setFont(f);

		jb6=new JButton("6");
		jb6.addActionListener(this);
		jb6.setFont(f);

		jb7=new JButton("7");
		jb7.addActionListener(this);
		jb7.setFont(f);

		jb8=new JButton("8");
		jb8.addActionListener(this);
		jb8.setFont(f);

		jb9=new JButton("9");
		jb9.addActionListener(this);
		jb9.setFont(f);

		jb10=new JButton("0");
		jb10.addActionListener(this);
		jb10.setFont(f);
		
		jb15=new JButton(".");
		jb15.addActionListener(this);
		jb15.setFont(f);

		jb11=new JButton("+");
		jb11.setForeground(Color.red);
		jb11.addActionListener(this);

		jb12=new JButton("-");
		jb12.setForeground(Color.red);
		jb12.addActionListener(this);

		jb13=new JButton("*");
		jb13.setForeground(Color.red);
		jb13.addActionListener(this);

		jb14=new JButton("/");
		jb14.setForeground(Color.red);
		jb14.addActionListener(this);	

		jb16=new JButton("=");
		jb16.setForeground(Color.red);
		jb16.addActionListener(this);

		jb_out=new JButton("C");
		jb_out.setForeground(Color.red);//设置字体颜色
		jb_out.addActionListener(this);
		
		gb=new GridBagLayout();
		gbc=new GridBagConstraints();

		set(1,1,jt1);set(11,1,jb_out);
		set(1,3,jb7);set(4,3,jb8);set(7,3,jb9);set(10,3,jb14);
		set(1,5,jb4);set(3,5,jb5);set(5,5,jb6);set(10,5,jb13);
		set(1,7,jb1);set(3,7,jb2);set(5,7,jb3);set(10,7,jb12);
		set(1,9,jb10);set(3,9,jb15);set(5,9,jb16);set(10,9,jb11);

		setSize(220,220);
		setVisible(true);
		setLocation(200,150);
		//setResizable(false);
	}
	public void set(int i,int j,Component con)
	{
		gbc.anchor=GridBagConstraints.CENTER;
		gbc.gridx=i;
		gbc.gridy=j;
		gb.setConstraints(con,gbc);
		jp.add(con);
	}
	public void actionPerformed(ActionEvent event)
	{
		Object obj=event.getSource();
		if (i==1&&doubleClick)
		{
			System.out.println("4");
			twoOpNumber=true;
		}
		if (obj==jb1)//点击“1”
		{
			sum=sum+jb1.getText();
			doubleClick=true;
		}
		if (obj==jb2)//点击“2”
		{
			sum=sum+jb2.getText();
			doubleClick=true;
		}
		if (obj==jb3)//点击“3”
		{
			sum=sum+jb3.getText();
			doubleClick=true;
		}
		if (obj==jb4)//点击“4”
		{
			sum=sum+jb4.getText();
			doubleClick=true;
		}
		if (obj==jb5)//点击“5”
		{
			sum=sum+jb5.getText();
			doubleClick=true;
		}
		if (obj==jb6)//点击“6”
		{
			sum=sum+jb6.getText();
			doubleClick=true;
		}
		if (obj==jb7)//点击“7”
		{
			sum=sum+jb7.getText();
			doubleClick=true;
		}
		if (obj==jb8)//点击“8”
		{
			sum=sum+jb8.getText();
			doubleClick=true;
		}
		if (obj==jb9)//点击“9”
		{
			sum=sum+jb9.getText();
			doubleClick=true;
		}
		if (obj==jb10)//点击“0”
		{
			sum=sum+jb10.getText();
			doubleClick=true;
		}
		if (obj==jb15)//加入小数点时,数据类型是不是就变了。
		{
			sum=sum+jb15.getText();
		}
		//当点击“+”号前,要把获取的值取出来。
		//因为有四种算法,所以用一个类来装载方法,且要分四种可能来运算,所以用了switch来减少冗余。
		jt1.setText(sum);
		if (obj==jb11)//点击“+”号,要把jt1清空,还要把数取出来。
		{
			System.out.println("1");
			startCalcul();
			doubleClick=false;
			op='+';
			twoOpNumber=true;//设置此值为真,则可以常加下去。
		}	
		if (obj==jb12)
		{
			startCalcul();
			doubleClick=false;
			op='-';
			twoOpNumber=true;
		}
		if (obj==jb13)
		{
			startCalcul();
			doubleClick=false;
			op='*';
			twoOpNumber=true;
		}
		if (obj==jb14)
		{
			startCalcul();
			doubleClick=false;
			op='/';
			twoOpNumber=true;
		}
		if (obj==jb16) //执行=号操作
		{
			System.out.println("5");
			if (twoOpNumber)
			{
				startCalcul();
				twoOpNumber=false;
			}
			//是不是高为假时有的不能执行。
			doubleClick=false;
		}
		if (obj==jb_out)//点击清空按钮。
		{
			sum="";
			jt1.setText("0");
			returnString1="";
			firstFlag=false;
		}		
	}
	static void startCalcul()
	{
		System.out.println("2");
		if (firstFlag)//第一次不运算。
			{
			//获取第二个值。
			System.out.println("6");
				returnString2=sum;
				sum="";
				if (doubleClick)
				{			
					returnString=calcul();
					jt1.setText(returnString);
				}
				returnString1=String.valueOf(returnString);//让运算后的值永为returnString1,可以累运算。
				i=1;//以便累运算。
			}
			else
			{
				System.out.println("3");
				returnString1=sum;			
				sum="";
				firstFlag=true;
				i=1;
			}
	}
	static String calcul()
	{
		float result=0;
		switch(op)
		{
			case('+'):
			result=Float.parseFloat(returnString1)+Float.parseFloat(returnString2);
			break;
			case('-'):
			result=Float.parseFloat(returnString1)-Float.parseFloat(returnString2);
			break;
			case('*'):
			result=Float.parseFloat(returnString1)*Float.parseFloat(returnString2);
			break;
			case('/'):
			result=Float.parseFloat(returnString1)/Float.parseFloat(returnString2);
			break;
		}
		return(String.valueOf(result));//输出结果。*/
	}
	public static void main(String a[])
	{
		new jishou();
	}
}

/*程序记录:
原先认为这个程序蛮简单的,不就一个+、-、*、/吗!但动手做之后才发现好难,要求的逻辑思维比较强,确实被它郁闷了几天。
原准备用getCommandaction来获取数来计算,但不知道怎么取出来用于计算,后就改为了数组,最后用了最简单的一个数来接收。
前面几个jishou只是用来做设计的,先只做出了页面,先试着做了"+"号运算,遇到了一些问题:一开始不知怎么接收第二个数的值。
后来遇到了不能连加,思路遇到了阻碍,一片空白,乱极了,不知道怎么下手了。于是就重新做了一个文件夹,一步一步做,又做了3个jishou文件,再解决了连加减的问题。
这个jishou2用不尔值来做循环基本上实现了四个运算,可以连续的运算了,可以清空,连等。
有一问题:结果不会从右边出来。
后用了jt1.setHorizontalAlignment(jt1.RIGHT);来完成了。
不过还有一小问题:当点击等号后点击运算符,jt1会为空,但运算可以实现。

程序心语:
常用到的转换:Integer.parseInt;Float.parseFloat
*/

//又发现一个问题,连等的时候他会出现问题。

⌨️ 快捷键说明

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