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

📄 jisuanqi_2.java

📁 采用JAVA界面来实现模拟WINDOWS自带的计算器,具有简单的数值处理能力,使用于初学者.
💻 JAVA
字号:

import java.awt.*; 
import javax.swing.*; 
import java.awt.event.*; 
import javax.swing.JOptionPane;
import java.math.*;

public class Jisuanqi_2 extends WindowAdapter implements ActionListener{

 public static int jishu=-1;//对应符号:+:1 -:2  *:3  /:4  =:-1
 public static int endIndex=0;//用于记录显示数字中的最后位置
 static JFrame fam;	
 JPanel pnl_one,pnl_two,pnl_1,pnl_2,pnl_3,pnl_4,pnl_5;
 double j_1=0.0,j_2=0.0;//用于处理运算
 public static String temp;//暂存显示的信息(数字)
 JButton [] btn=new JButton[18];//所有的按钮
 JTextField f;//用于显示数字及计算结果



Jisuanqi_2(){//构造函数
fam=new JFrame("计算器");
temp="";
fam.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});

fam.setSize(245,280); //设置窗口大小
fam.setLocation(200,200);//设置窗口显示位置
fam.setFont(new Font("Arial",Font.PLAIN,13)); //设置字体

pnl_one=new JPanel();
pnl_two=new JPanel();
pnl_1=new JPanel();
pnl_2=new JPanel();
pnl_3=new JPanel();
pnl_4=new JPanel();
pnl_5=new JPanel();

btn[1]=new JButton(" 1 "); btn[2]=new JButton(" 2 ");btn[3]=new JButton(" 3 ");
btn[4]=new JButton(" 4 ");btn[5]=new JButton(" 5 ");btn[6]=new JButton(" 6 ");
btn[7]=new JButton(" 7 ");btn[8]=new JButton(" 8 ");btn[9]=new JButton(" 9 ");
btn[0]=new JButton(" 0 ");btn[10]=new JButton("  + ");btn[11]=new JButton(" - ");
btn[12]=new JButton(" * ");btn[13]=new JButton(" / ");btn[14]=new JButton(" = ");
btn[15]=new JButton(" 退格 ");btn[16]=new JButton(" 清0 ");
btn[17]=new JButton(" . ");

f=new JTextField("0.");
f.setHorizontalAlignment(JTextField.RIGHT); 
f.setEditable(false);

pnl_one.add(new Label("JAVA计算器",Label.CENTER));
pnl_one.add(f);
pnl_one.setLayout(new GridLayout(2,1));
 
pnl_1.add(btn[15]);
pnl_1.add(btn[16]);
pnl_1.setLayout(new GridLayout(1,2,5,2));

pnl_2.add(btn[7]);pnl_2.add(btn[8]);pnl_2.add(btn[9]);pnl_2.add(btn[13]);
pnl_2.setLayout(new GridLayout(1,4,5,2));
pnl_3.add(btn[6]);pnl_3.add(btn[5]);pnl_3.add(btn[4]);pnl_3.add(btn[12]);
pnl_3.setLayout(new GridLayout(1,4,5,2));
pnl_4.add(btn[1]);pnl_4.add(btn[2]);pnl_4.add(btn[3]);pnl_4.add(btn[11]);
pnl_4.setLayout(new GridLayout(1,4,5,2));
pnl_5.add(btn[0]);pnl_5.add(btn[14]);pnl_5.add(btn[17]);pnl_5.add(btn[10]);
pnl_5.setLayout(new GridLayout(1,4,5,2));

pnl_two.add(pnl_1);
pnl_two.add(pnl_2);
pnl_two.add(pnl_3);
pnl_two.add(pnl_4);
pnl_two.add(pnl_5);
pnl_two.setLayout(new GridLayout(5,1,2,5));

for(int i=0;i<18;i++){btn[i].addActionListener(this);
}

pnl_1.setVisible(true);
pnl_2.setVisible(true);
pnl_3.setVisible(true);
pnl_4.setVisible(true);
pnl_5.setVisible(true);
pnl_one.setVisible(true);
pnl_two.setVisible(true);


fam.add(pnl_one,BorderLayout.NORTH);
fam.add(pnl_two);
fam.setVisible(true);
fam.setResizable(false);


}

public static void main(String args[]) {//主方法测试

new Jisuanqi_2();
}


public void actionPerformed(ActionEvent e) {

for(int i=0;i<10;i++){//处理显示数字
	if(e.getSource()==btn[i])
	{
		f.setText(temp+i);
		temp=f.getText();
		j_1=Double.parseDouble(f.getText());
		++endIndex;//位数计数器增加
	}
	  
}

if(e.getSource()==btn[17]){//处理显示小数点
		f.setText(temp+".");
		temp=f.getText();
		j_1=Double.parseDouble(f.getText());
		++endIndex;//位数计数器增加
}

if (e.getSource()==btn[14]){//处理等号信息
	if(jishu==1){
		 f.setText(""+ (j_1+j_2));
	}
	if(jishu==2){
		f.setText(""+ (j_2-j_1));
	}
	if(jishu==3){
		f.setText(""+ (j_1*j_2));
	}
	if(jishu==4){
		if(j_1==0){		
		JOptionPane.showMessageDialog(fam, "分母不能为零");
		   temp="";
           f.setText("0.");
           endIndex=0;
           jishu=-1;
           j_1=0.0;
           j_2=0.0;
           return;
		}
		f.setText(""+ (j_2/j_1));
	}
   temp="";
   endIndex=0;
   j_1=Double.parseDouble(f.getText());;
   jishu=-1;
   return;
}

for(int i=10;i<13;i++){//处理+,-,*
	if (e.getSource()==btn[i]){
	if(jishu==-1)j_2=j_1;
	if(jishu==1)j_2=j_1+j_2;
	if(jishu==2)j_2=j_2-j_1;
	if(jishu==3)j_2=j_1*j_2;
	if(jishu==4)j_2=j_2/j_1;		
	temp="";
	f.setText(""+j_2);
	jishu=(i+1)%10;
	return;
}
}

if(e.getSource()==btn[13]){//处理除法,除法要单独考虑
	if(jishu==-1)j_2=j_1;
	if(jishu==1)j_2=j_1+j_2;
	if(jishu==2)j_2=j_2-j_1;
	if(jishu==3)j_2=j_1*j_2;
	
	if(jishu==4){
			if(j_1==0){		
		JOptionPane.showMessageDialog(fam, "分母不能为零");
		   temp="";
           f.setText("0.");
           endIndex=0;
           jishu=-1;
           j_1=0.0;
           j_2=0.0;
           	return;
		}
		j_2=j_2/j_1;
		}
	temp="";
	jishu=4;
	f.setText(""+j_2);
	return;
}

if(e.getSource()==btn[15]){//处理退格

	if(endIndex==0){
	JOptionPane.showMessageDialog(fam, "不能再退了,极限了");
    temp="";
    f.setText("0.");
    endIndex=0;
    jishu=-1;
    j_1=0.0;
    j_2=0.0;	
	return;	

	}
	endIndex--;		
    f.setText(temp.substring(0,endIndex));
    
	temp=f.getText();  

	if(endIndex>0)j_1=Double.parseDouble(f.getText());
	else j_1=0.0;
}



if(e.getSource()==btn[16]){//处理清0
   temp="";
   f.setText("0.");
   endIndex=0;
   jishu=-1;
   j_1=0.0;
   j_2=0.0;
}

}
}


⌨️ 快捷键说明

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