首页>代码>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();
	}
}
最近下载更多
yangctz  LV25 3月1日
鲁一宇  LV5 2024年12月23日
jhhg25  LV1 2023年6月24日
iiiiIvan  LV1 2023年6月5日
luo110012  LV9 2023年5月15日
lsglsg9  LV23 2022年11月15日
weim629  LV3 2022年9月17日
echo邓杰  LV1 2022年8月26日
weilaix  LV2 2022年5月29日
huangxiaojie  LV9 2021年6月13日
最近浏览更多
yangctz  LV25 3月1日
鲁一宇  LV5 2024年12月23日
暂无贡献等级
diannian  LV1 2024年9月10日
heyula 2024年6月18日
暂无贡献等级
qiyexi 2024年6月17日
暂无贡献等级
微信网友_7008818781589504  LV1 2024年5月25日
xlqb999  LV3 2024年4月29日
芋妮奶茶  LV1 2024年4月20日
yyyyyh 2024年2月21日
暂无贡献等级
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友