首页>代码>java Swing编写简单计算器源码>/jisuanqi/src/jisuanqi/jisuanqi.java
package jisuanqi;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class jisuanqi extends JFrame implements ActionListener
{
	JFrame jf=new JFrame("计算器");
	GridLayout g=new GridLayout(5,4);
	JTextField jtf=new JTextField("0",20);
	boolean com=false; 
	int i=0; 
	String sum=""; 
	double total=0; 
	int xuhao=0;
	jisuanqi()
	{
		jf.add(jtf,BorderLayout.NORTH);
		JPanel jp=new JPanel();
		jp.setLayout(g);
		jf.add(jp);
		String[] lab = {"BackS","CE","C","+","7","8","9","-","4","5","6","*","1","2","3","/","0",".","+/-","="};
		for(int i=0;i<lab.length;i++)
		{
			JButton jb = new JButton(lab[i]);
			jp.add(jb);
			jb.addActionListener(this);
		}
		jf.setLocation(300, 300);
		jf.setSize(200,300);
		jf.setVisible(true);
		jf.pack();
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);	
	}
	public void actionPerformed(ActionEvent e)
	{
		String comm=e.getActionCommand();
		if("0123456789".indexOf(comm)!=-1)
		{
			if (com || i == 0)                                        
			{ 
				jtf.setText(comm); 
				com = false; 
				i = 1; 
			} 
			else
			{ 
				sum = jtf.getText(); 
				if("0".equals(comm)&&!(Double.parseDouble(sum) > 0 || Double.parseDouble(sum) < 0))
				{
					if (sum.trim().indexOf(".") == -1)
					{ 
						jtf.setText(sum); 
					} 
					else 
					{ 
						jtf.setText(sum + "0"); 
					} 
				}
				jtf.setText(sum + comm); 
			} 
		}
		else if ("CE".equals(comm)||"C".equals(comm)) 
		{ 
			jtf.setText(""); 
			i = 0; 
			com = true; 
		}  
		else if (e.getActionCommand().equals(".")) 
		{ 
			if (com || i == 0)
			{ 
				jtf.setText("0."); 
				com = false; 
				i = 1; 
			} 
			else 
			{ 
				sum = jtf.getText(); 
				if (sum.trim().indexOf(".") == -1)
				{
					jtf.setText(sum + "."); 
				} 
				else 
				{ 
					jtf.setText(sum); 
				} 
			} 
		}
		else if("+-*/".indexOf(comm)!=-1)
		{
			com = true; 
			i = 0; 
			total = Double.parseDouble(jtf.getText());
			if("+".equals(comm))
				xuhao = 1; 
			else if("-".equals(comm))
				xuhao=2;
			else if("*".equals(comm))
				xuhao = 3; 
			else if("/".equals(comm))
				xuhao=4;
		}
		else if("BackS".equals(comm))
		{
			String s=jtf.getText();
			if(s.length()>0)
			{
				jtf.setText(s.substring(0,s.length()-1));
			}
		}
		else if("+/-".equals(comm))
		{
			String s=jtf.getText();
			if(s.startsWith("-"))
			{
				jtf.setText(s.substring(1));
			}
			else
			{
				jtf.setText("-"+s);
			}
		}
		else if ("=".equals(comm)) 
		{ 
			switch (xuhao) 
			{ 
			case 1 : 
			{ 
				double ad = total + Double.parseDouble(jtf.getText()); 
				jtf.setText(ad + ""); 
				i = 0; 
				sum = ""; 
				break; 
			} 
			case 2 : 
			{ 
				double ad = total - Double.parseDouble(jtf.getText()); 
				jtf.setText(String.valueOf(ad)); 
				i = 0; 
				sum = ""; 
				break; 
			} 
			case 3 : 
			{ 
				double ad = total * Double.parseDouble(jtf.getText()); 
				jtf.setText(ad + ""); 
				i = 0; 
				sum = ""; 
				break; 
			} 
			case 4 : 
			{ 
				double ad = total / Double.parseDouble(jtf.getText()); 
				jtf.setText(ad + ""); 
				i = 0; 
				sum = ""; 
				break; 
			} 
		} 
		System.out.println(com); 
		repaint();
		} 
	}
	public static void main(String[] args) 
	{
		new jisuanqi();
	}
}
最近下载更多
jhhg25  LV1 2023年6月24日
iiiiIvan  LV1 2023年6月5日
luo110012  LV9 2023年5月15日
lsglsg9  LV22 2022年11月15日
weim629  LV3 2022年9月17日
echo邓杰  LV1 2022年8月26日
weilaix  LV2 2022年5月29日
huangxiaojie  LV9 2021年6月13日
dengge123  LV13 2021年6月6日
176110  LV1 2021年3月26日
最近浏览更多
芋妮奶茶 11小时前
暂无贡献等级
yyyyyh 2月21日
暂无贡献等级
马畑ASLMT 2月17日
暂无贡献等级
888888888866666666 1月17日
暂无贡献等级
lshlsh 2023年12月25日
暂无贡献等级
gk_xmj  LV1 2023年12月4日
fesfefe  LV13 2023年9月30日
hahaha-3021 2023年8月2日
暂无贡献等级
jhhg25  LV1 2023年6月24日
微信网友_6533174388953088  LV1 2023年6月24日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友