今天是:
 
您现在的 位置: 浩特个人网 >> 文章频道 >> 编程源码 >> JAVA源代码 >> 正文 本站公告:
注册会员体验更多精彩!  [2006-08-17 09:54:52]      浩特个人网介绍  [2006-08-17 09:52:30]      浩特个人网全新改版!  [2006-07-24 13:27:58]
  java实现计算器(算加法)    5星级
java实现计算器(算加法)
[ 作者:无     来源:搜集整理     点击数:     更新时间:2006-05-22     文章录入:
【字体: 字体颜色
/*********************************************************************** 
 * Module:  Caculater.java 
 * Author:  Administrator 
 * Created: 2004-12-7 23:48:21 
 * Purpose:  
 ***********************************************************************/ 
package org.heixue.test.applet; 

/** 
 * @author Administrator 
 */ 
import javax.swing.*; 
import java.awt.event.*; 
import java.awt.*; 
public class Calculater extends JApplet { 
 JMenu[] menus = { new JMenu("选项"),new JMenu("关于")}; 
 JMenuItem[] items = {new JMenuItem("关于"),new JMenuItem("设置背景色") }; 
 JButton b1 = new JButton("运算"); 
 JOptionPane b2 = new JOptionPane("运算..."); 
 JTextField txt1 = new JTextField("100",10); 
 JTextField txt2 = new JTextField("200",10); 
 JTextField txt3 = new JTextField(20); 
 JLabel[] label = {new JLabel("+"),new JLabel("=")}; 
 JColorChooser jcc = new JColorChooser(); 
  
 ActionListener al = new ActionListener() { 
  public void actionPerformed(ActionEvent e){ 
   //String name = ((JButton)e.getSource()).getText(); 
   int m = JOptionPane.showConfirmDialog(null, "继续进行运算吗?", "确定",JOptionPane.YES_NO_OPTION); 
   if(m==0){ 
    String op1 = txt1.getText(); 
    String op2 = txt2.getText(); 
    txt3.setText(bussiness(op1,op2)); 
    //txt3.setText(m+""); 
   } 
  } 
 }; 
 ActionListener al2 = new ActionListener() { 
  public void actionPerformed(ActionEvent e){ 
   JOptionPane.showMessageDialog(null, "作者:黑雪小组", "关于...",JOptionPane.INFORMATION_MESSAGE); 
  } 
 }; 
 ActionListener al3 = new ActionListener() { 
  public void actionPerformed(ActionEvent e){ 
    
   JColorChooser.createDialog(null,"背景色...",true,jcc,al4,null).show(); 
  } 
 }; 
 ActionListener al4 = new ActionListener() { 
  public void actionPerformed(ActionEvent e){ 
   changeFace(0,jcc.getColor()); 
  } 
 }; 
 public void init() { 
  //initialize layout. 
  Container cp = getContentPane(); 
  cp.setLayout(new FlowLayout()); 
  //set menu bar. 
  menus[0].add(items[1]); 
  items[1].addActionListener(al3); 
  menus[1].add(items[0]); 
  items[0].addActionListener(al2); 
  JMenuBar mb = new JMenuBar(); 
  for(int i = 0; i < menus.length; i++) 
   mb.add(menus[i]); 
  setJMenuBar(mb); 
   
  //add 3 JTextfield. 
  cp.add(txt1); 
  cp.add(label[0]); 
  cp.add(txt2); 
  cp.add(label[1]); 
  cp.add(txt3); 
   
  //add 1 button with JOptionPane 
  cp.add(b1); 
  b1.addActionListener(al); 
 } 
 void changeFace(int i,Object o){ 
  if(i==0){ 
   //设置背景色 
   this.getContentPane().setBackground((Color)o); 
   //setBackground((Color)o);   
  } 
 } 
 String bussiness(String op1,String op2){ 
  int result=0; 
  int op11=0; 
  int op21=0; 
  try{ 
   op11=Integer.parseInt(op1); 
  }catch(Exception e1){ 
     
  } 
  try{ 
   op21=Integer.parseInt(op2); 
  }catch(Exception e1){ 
     
  } 
  result=op11+op21; 
  return ""+result; 
 } 
 public static void main(String[] args) { 
  JApplet applet = new Calculater(); 
  JFrame frame = new JFrame("计算器"); 
  // To close the application: 
  //Console.setupClosing(frame); 
  frame.getContentPane().add(applet); 
  frame.setSize(260,140); 
  applet.init(); 
  applet.start(); 
  frame.setVisible(true); 
 } 
}  
  • 上一篇文章: 对象串行化的方法--保存对象

  • 下一篇文章: java编写TCP方式的通信程序
  • 发表评论   告诉好友   打印此文  收藏此页  关闭窗口  返回顶部
     最新5篇热点文章
     面对面有时不一定是相遇(...
     Windows Vista 主题安装包...
     两首《你到底爱谁》歌词:...
     四首《童话》歌词:光良、...
     情人节手机短信搞笑大全
     
     最新5篇推荐文章
     面对面有时不一定是相遇(...
     现在的女孩子满脑子都是色...
     千万别找太聪明的美眉做女...
     fireworks logo教程
     电话英语生活应用12大类
     
     相 关 文 章

      网友评论:(只显示最新5条。评论内容只代表网友观点,与本站立场无关!)


    制作:笨超 Copyright©2006-2007
    陇ICP备05005205号