计算器

源代码下载:http://pan.baidu.com/s/1mghYaHa

运行结果如图:



import java.awt.*;
import java.awt.event.*;
import java.net.URI;
import java.text.DecimalFormat;

import javax.swing.*;

class MyFrame extends Frame //implements ActionListener
{
	//以下这句话没有用,不写的话就有一个  waring
	private static final long serialVersionUID = 1L;

	public void actionPerformed(ActionEvent e) {
		
	}
}
public class Jisuanqi extends JLabel implements ActionListener, MouseListener
{	
	//声明各个控件
	DecimalFormat   df =new DecimalFormat("#####0.000");
	private static final long serialVersionUID = 1L;//没有用
	public TextArea tf;										//显示文区域
	public Button b1,b2,b3,b4,b5,b6,b7,b9,b10,b8;			//按钮
	public Button b11,b12,b13,b14,b15,b16,b17,b19,b20,b18;//按钮
	public String a,b,copyTxt;										//接收输入的内容
	public double first,second;								//将输入的内容转换为Double类型
	public double luo;										//标记,
	public String ch;										//记录你输入的运算符
	public static Menu View;
	public static Menu Edit;								//三个菜单
	public static Menu  Help;
	public static Panel pj;
	public static TextArea ta;								//弹出窗体的文本域
	public static MenuItem Info,SeeHelp,copy,parse,cut,AuthorInfo;//下拉列表
	public static Frame Jump; 								//弹出的窗体
	public static JLabel linkLabel;							//弹出的窗体上面的超链接label
	public static String ustr="http://www.baidu.com";
	public Jisuanqi(){}
	public Jisuanqi(String ustr,String ttt){
			Jisuanqi.ustr=ustr;
			this.setForeground(Color.red);//设置链接颜色
			this.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));//设置鼠标样式
	}
	public static void main(String[] args)throws Exception
	{	
		Jisuanqi ji=new Jisuanqi();
		ji.init();		     
	}
	
	//初始化主窗体
	public void init(){
		Frame f=new Frame("计算器");
		
		Panel p1=new Panel();
		Panel p2=new Panel();
		
		 Jump=new Frame("关于计算器");
		 Jump.setBounds(500, 200, 300, 200);
		 pj=new Panel();
		ta=new TextArea("",9,39,TextArea.SCROLLBARS_NONE);
		ta.setFont(new Font("黑体",Font.CENTER_BASELINE,12));
		linkLabel=new Jisuanqi("http://www.baidu.com","百度一下");
		//linkLabellab.setText("
		Jump.setBackground(new Color(240,240,240));
		linkLabel.addMouseListener(this);
		ta.setEditable(false);
		
		tf=new TextArea("",3,21,TextArea.SCROLLBARS_NONE);
		tf.setFont(new Font("黑体",Font.BOLD,14));
		//TextArea.SCROLLBARS_HORIZONTAL_ONLY();
		//tf.setFocusTraversalKeysEnabled(true);
		 
		//级联菜单
		 Info=new MenuItem("关于计算器");
		 SeeHelp=new MenuItem("查看帮助");
		 copy=new MenuItem("复制",new MenuShortcut(KeyEvent.VK_C,false));
		 parse=new MenuItem("粘贴",new MenuShortcut(KeyEvent.VK_V,false));
		 cut=new MenuItem("剪切",new MenuShortcut(KeyEvent.VK_X,false));
		 AuthorInfo=new MenuItem("作者信息");
		
		MenuBar mb=new MenuBar();
		Menu m=new Menu();
		
		f.setMenuBar(mb);
		View=new Menu("查看(v)");
		Edit=new Menu("编辑(E)");
		Help=new Menu("帮助(H)");	
		mb.add(m);
		mb.add(View);
		mb.add(Edit);
		mb.add(Help);
		Help.add(Info);
		Help.add(SeeHelp);
		Edit.add(copy);
		Edit.add(parse);
		Edit.add(cut);
		View.add(AuthorInfo);
		
		
		//m.setFont(new Font("Serif",Font.BOLD|Font.ITALIC,8));
		//System.out.println(m.getFont());获取字体
		
		//初始化控件
		 b1=new Button("X^2");
		 b2=new Button("√X");
		 b3=new Button("%");
		 b4=new Button("C");
		 b5=new Button("7");
		b5.setBackground(new Color(211,211,211));
		 b6=new Button("8");
		b6.setBackground(new Color(211,211,211));
		 b7=new Button("9");
		b7.setBackground(new Color(211,211,211));
		 b8=new Button("/");
		
		 b9=new Button("4");
		b9.setBackground(new Color(211,211,211));
		 b10=new Button("5");
		b10.setBackground(new Color(211,211,211));
		 b11=new Button("6");
		b11.setBackground(new Color(211,211,211));
		 b12=new Button("-");	
		 b13=new Button("1");
		b13.setBackground(new Color(211,211,211));
		 b14=new Button("2");
		b14.setBackground(new Color(211,211,211));
		 b15=new Button("3");
		b15.setBackground(new Color(211,211,211));
		 b16=new Button("*");
		 b17=new Button("0");
		b17.setBackground(new Color(211,211,211));
		 b18=new Button("·");
		 b19=new Button("=");
		 b20=new Button("+");
		 
		//监听事件
		b1.addActionListener(this);
		b2.addActionListener(this);
		b3.addActionListener(this);
		b4.addActionListener(this);
		b5.addActionListener(this);
		b6.addActionListener(this);
		b7.addActionListener(this);
		b8.addActionListener(this);
		b9.addActionListener(this);
		b10.addActionListener(this);
		b11.addActionListener(this);
		b12.addActionListener(this);
		b13.addActionListener(this);
		b14.addActionListener(this);
		b15.addActionListener(this);
		b16.addActionListener(this);
		b17.addActionListener(this);
		b18.addActionListener(this);
		b19.addActionListener(this);
		b20.addActionListener(this);
		
		//为下拉菜单设置监听
		//Info.addActionListener(this);
		//SeeHelp.addActionListener(this);
		copy.addActionListener(this);
		parse.addActionListener(this);
		cut.addActionListener(this);
		AuthorInfo.addActionListener
		(
				new ActionListener()
			{
				public void actionPerformed(ActionEvent arg0) 
				{
					Jump.add(pj);
					Jump.remove(linkLabel);
					ta.setText("  作者: 马建杰,大学本科毕业于河北科技师范学院,祖籍廊坊,农村人,QQ:592357262     电话号码:  15227290166");
					Jump.setTitle("作者信息");
					pj.add(ta);
					Jump.setVisible(true);		
				}
			}
		);
		Info.addActionListener
		(
				new ActionListener()
			{
				public void actionPerformed(ActionEvent arg0) 
				{
					Jump.add(pj);
					Jump.remove(linkLabel);
					Jump.setTitle("关于计算器");
					ta.setText("  版本 1.0,根据中华人民共和国法律规定,本产品正规,所有权利归 majianjie 所有,经授权后方可使用。");
					pj.add(ta);
					Jump.setVisible(true);		
				}
			}
		);
		SeeHelp.addActionListener
		(
				new ActionListener()
			{
				public void actionPerformed(ActionEvent arg0) 
				{
					Jump.remove(pj);
					ta=null;
					ta=new TextArea("",4,39,TextArea.SCROLLBARS_NONE);
					ta.setFont(new Font("黑体",Font.CENTER_BASELINE,12));
					ta.setText("小试牛刀。此版本正在开发中,以后功能会更加完善,敬请期待!如果有疑难问题请访问--> 友情链接:");
					Jump.add(linkLabel,"Center");
					
					Jump.add(ta,"North");
					Jump.setTitle("帮助");
					linkLabel.setText("<html><p></p></p>http://www.baidu.com</p></html>");
					
					Jump.setVisible(true);	
					
				}
			}
		);
		copy.addActionListener
		(
				new ActionListener()
			{
				public void actionPerformed(ActionEvent arg0) 
				{
					copyTxt=tf.getText();
				}
			}
		);
		parse.addActionListener
		(
				new ActionListener()
			{
				public void actionPerformed(ActionEvent arg0) 
				{
					tf.setText(copyTxt);
				}
			}
		);
		cut.addActionListener
		(
				new ActionListener()
			{
				public void actionPerformed(ActionEvent arg0) 
				{
					copyTxt=tf.getText();
					tf.setText("");
				}
			}
		);
		tf.setBounds(20, 30, 30, 40);
		
		p2.setLayout(new GridLayout(5,5,5,10));
		
		p1.add(tf);
		p2.setBounds(300, 300, 180, 200);
		
		p2.setSize(240, 300);
		p2.add(b1);
		p2.add(b2);
		p2.add(b3);
		p2.add(b4);
		p2.add(b5);
		p2.add(b6);
		p2.add(b7);
		p2.add(b8);
		p2.add(b9);
		p2.add(b10);
		p2.add(b11);
		p2.add(b12);
		p2.add(b13);
		p2.add(b14);
		p2.add(b15);
		p2.add(b16);
		p2.add(b17);
		p2.add(b18);
		p2.add(b19);
		p2.add(b20);
			
		
		//p3.setSize(150, 30);
		//p3.setLayout(new FlowLayout(FlowLayout.CENTER));	
		
		f.add(p1,"Center");
		f.add(p2,"South");
		
		//f.add(p3,"Center");
		
		f.setBackground(new Color(217,228,241));
		f.setResizable(false);
		Jump.setResizable(false);
		
		//f.setLayout(new FlowLayout());
		f.setBounds(400, 400, 200, 280);
		f.setVisible(true);
		
		//设置图标
        Image icon = Toolkit.getDefaultToolkit().getImage("H:/1.jpg");
        f.setIconImage(icon);
        //窗口监听器
		f.addWindowListener
			(new WindowAdapter()
				{
					public void windowClosing(WindowEvent e)
						{
							System.exit(0);
						}
				}
			);
		
		Jump.addWindowListener
		(new WindowAdapter()
			{
				public void windowClosing(WindowEvent e)
					{
						Jump.setVisible(false);
					}
			}
		);
		
		
	}
	//@Deprecated	//说明下面的方法过时了
	public void actionPerformed(ActionEvent e)
	{
		
				if(e.getSource()==b4)//清除功能
		        {
		        	if(!"".equals(tf.getText())){
						tf.setText("");
					}
		        }
					// getSource得到的组件的名称,而getActionCommand得到的是标签。
				 if('0'<=e.getActionCommand().charAt(0)&&e.getActionCommand().charAt(0)<='9')
				    {		 	
				        tf.setText(tf.getText()+e.getActionCommand());//未点击运算符号时,将输入的数字接在后面写    
				    }  
				if(e.getSource()==b20)//实现加法
				     {
					if( "".equals(tf.getText())){
						tf.setText("请输入数值后在进行运算!!");
					 }else{
						 	ch=e.getActionCommand();
					        a=tf.getText();//强制转换为双精度
					        tf.setText("");
					        luo=1;
					 }
						
				      }
				 if(e.getSource()==b12)//实现减法
				     {
					 if( "".equals(tf.getText())){
							tf.setText("请输入数值后在进行运算!!");
						 }else{
							 ch=e.getActionCommand();
							 //System.out.println(e.getActionCommand().charAt(0));
					         a=tf.getText();
					         tf.setText("");
					         luo=2; 
						 }
						 
				      }
			     if(e.getSource()==b16)//实现乘法运算
			          {
			    	 if( "".equals(tf.getText())){
							tf.setText("请输入数值后在进行运算!!");
						 }else{
							 ch=e.getActionCommand();
				             a=tf.getText();
				             tf.setText("");
				             luo=3;
						 }
						
			          }
				  if(e.getSource()==b8)//实现除法运算
			         {
					  if( "".equals(tf.getText())){
							tf.setText("请输入数值后在进行运算!!");
						 }else{
							 ch=e.getActionCommand();
				             a=tf.getText();
				             tf.setText("");
				             luo=4;
						 }
						 
			         }
				  
					  if(e.getSource()==b3)//实现模运算
				        {
						  
							 if( "".equals(tf.getText())){
								tf.setText("请输入数值后在进行运算!!");
							 }else{
								 ch=e.getActionCommand();
						            a=tf.getText();
						            tf.setText("");
						            luo=5;
							 }
				    
				        }
					   if(e.getSource()==b2)//实现求平方根法运算
				        {
						   if("".equals(tf.getText())){
							   tf.setText("请输入数值后在进行运算符输入!");
						   }else{
							    ch=e.getActionCommand();
					            a=tf.getText();
					            second=Double.parseDouble(a);
					            tf.setText(""+Math.sqrt(second)); 
						   }
						
				        }
				        if(e.getSource()==b1)//实现求平方法运算
				        {
				        	if("".equals(tf.getText())){
				        		tf.setText("请输入数值后在进行运算符输入!");
				        	}else{
				        		ch=e.getActionCommand();
					            a=tf.getText();
					            second=Double.parseDouble(a);
					            tf.setText(second*second+" ");
				        	}	
				                    
				        } 
			    	
			        if(e.getSource()==b18)
				 	{
			        	ch=e.getActionCommand();
			        	
			        	//tf.append("0");
					 	tf.append(".");
				 	}
			        if(e.getSource()==b19)
				    {
			        	if("".equals(tf.getText())){
			        		tf.setText("请输入数值后在进行运算符输入!");
			        	}else{
			        		b=tf.getText();
					        if(luo==1)
					        {
					        	first=Double.parseDouble(a);
					        	second=Double.parseDouble(b);			        	
					        	tf.setText(a + " "+ch+" "+ b+" = "+df.format(first+second));
					        }
					        if(luo==2)
					        {
					        	first=Double.parseDouble(a);
					        	second=Double.parseDouble(b);
					        	tf.setText(a + " "+ch+" "+ b+" = "+df.format(first-second));
					        }
					        if(luo==3)
					        {
					        	first=Double.parseDouble(a);
					        	second=Double.parseDouble(b);
					        	tf.setText(a + " "+ch+" "+ b+" = "+df.format(first*second));
					        }
					        if(luo==4)
					        {
					        	first=Double.parseDouble(a);
					        	second=Double.parseDouble(b);
					        	tf.setText(a + " "+ch+" "+ b+" = "+df.format(first/second));
					        }
					        if(luo==5)
					        {
					        	first=Double.parseDouble(a);
					        	second=Double.parseDouble(b);
					        	tf.setText(a + " "+ch+" "+ b+" = "+df.format(first%second));
					        }
					       
				       	
			        	}
			        	
			        
		}//以上是监听ActionListener的尾括号
			        
	}//初始化init的尾括号
	
			public void mouseClicked(MouseEvent arg0) {
				 try {
					   Desktop.getDesktop().browse(new URI(ustr));
					  } catch (Exception e1) {
						  e1.printStackTrace();
					  }
			}
			public void mouseEntered(MouseEvent arg0) {}
			public void mouseExited(MouseEvent arg0) {}
			public void mousePressed(MouseEvent arg0) {}
			public void mouseReleased(MouseEvent arg0) {}
	

}


  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值