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

📄 calculater_gridlayout.java

📁 一个关于java的计算器的程序
💻 JAVA
字号:
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class Calculater_GridLayout extends Applet implements ActionListener 
{
	TextField tf=new TextField(12);
	Panel p1=new Panel();
	Panel p2=new Panel();
	Panel p3=new Panel();
	Button bt[];
	String names[]={"1","2","3","+","4","5","6","-","7","8","9","*","0",".","=","/"};
	Button b_c=new Button("          Clear         ");
	Button b_e=new Button("  Exit  ");//
	Color c=new Color(251,197,103 );
	float a,b,result;
	int type;
	boolean toggle=true;
    public void init()
	{
		p1.add(tf);
//		p2.setLayout(new GridLayout(4,4,5,5));
		p2.setLayout(new GridLayout(4,4,5,5));
		bt=new Button[names.length];
		for(int i=0;i<names.length;i++)
		{
			bt[i]=new Button(names[i]);
			bt[i].addActionListener(this);
			p2.add(bt[i]);
		}
		//setLayout(new GridLayout(3,1,5,5));
		p3.add(b_c);
		//p3.add(b_e);
		add(p1);
		add(p2);
		add(p3);//
		this.setBackground(c);
		b_c.addActionListener(this);
		b_e.addActionListener(this);

	}
	public void actionPerformed(ActionEvent e)
	{
		for(int i=0;i<10;i++)
		if(e.getActionCommand().equals(String.valueOf(i)))
			if(toggle==false)
		{tf.setText(String.valueOf(i));toggle=true;}
		else
			tf.setText(tf.getText()+i);
		if(e.getActionCommand().equals("."))
			if(toggle==false)
		{tf.setText(".");toggle=true;}
		else
			tf.setText(tf.getText()+".");
		if(e.getActionCommand().equals("+")||e.getActionCommand().equals("-")||e.getActionCommand().equals("*")||e.getActionCommand().equals("/"))
		{
			a=(Float.valueOf(tf.getText())).floatValue();
			toggle=false;
			//tf.setText("");
			if(e.getActionCommand().equals("+"))
			    type=1;
			if(e.getActionCommand().equals("-"))
		        type=2;
			if(e.getActionCommand().equals("*"))
				type=3;
			if(e.getActionCommand().equals("/"))
				type=4;
		}
		if(e.getActionCommand().equals("="))
		{
			//tf.setText("");
			b=(Float.valueOf(tf.getText())).floatValue();
            tf.setText("");
			if(type==1)
			    result=a+b;
			if(type==2)
				result=a-b;
			if(type==3)
				result=a*b;
			if(type==4)
				result=a/b;
			tf.setText(String.valueOf(result));
			repaint();
			type=0;/**/
			toggle=false;
		}
		if(e.getSource()==b_c)
		//if(e.getSourse(" Clear "))
			tf.setText("");
			if(e.getSource()==b_e)
		//if(e.getActionCommand().equals("  Exit  "))
			System.exit(0);
	}
}

⌨️ 快捷键说明

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