| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1266 人关注过本帖
标题:[分享]计算器原代码
只看楼主 加入收藏
kingarden
Rank: 2
等 级:论坛游民
威 望:1
帖 子:517
专家分:40
注 册:2004-12-8
收藏
 问题点数:0 回复次数:4 
[分享]计算器原代码
import lundin.SymbolicMath.Eval; import lundin.SymbolicMath.Derive; import java.applet.Applet; import java.util.*; import java.awt.*; public class Calculator extends Applet{ Panel p = null; Panel p1 = null; Panel p2 = null; Panel p3 = null; Label lbl = null; Eval ev = null; // Derive de = null; //??等号 TextField tf = null; Button info = null; Button clear = null; Button equa = null; Button plus = null; Button minus = null; Button divide = null; Button mult = null; Button zerow,one,two,three,four,five,six,seven,eight,nine,point; Hashtable values = null; Font fo = null; FontMetrics fm = null; public void init() { int color = 0x999966; GridBagLayout gridb = new GridBagLayout(); GridBagLayout gridb2 = new GridBagLayout(); GridBagLayout gridb3 = new GridBagLayout(); GridBagConstraints constr = new GridBagConstraints(); String temp = ""; ev = new Eval(); de = new Derive(); values = new Hashtable(); fo = new Font("Times Roman", Font.PLAIN, 9 ); this.setFont( fo ); fm = this.getFontMetrics( fo ); p = new Panel(); p1 = new Panel(); p2 = new Panel(); p3 = new Panel(); tf = new TextField(); info = new Button( "Info" ); clear = new Button( "Clear" ); plus = new Button( "+" ); minus = new Button( "-" ); mult = new Button( "*" ); divide = new Button( "/" ); equa = new Button( "=" ); point = new Button("."); zerow = new Button("0"); one = new Button("1"); two = new Button("2"); three = new Button("3"); four = new Button("4"); five = new Button("5"); six = new Button("6"); seven = new Button("7"); eight = new Button("8"); nine = new Button("9"); this.setBackground( new Color(color) ); //设置背景颜色 p2.setLayout( gridb ); p2.setBackground( new Color(color) ); p2.setForeground( Color.black ); p3.setBackground( new Color(color) ); p3.setForeground( Color.black ); tf.setBackground( Color.white ); tf.setForeground( Color.black ); constr.fill = GridBagConstraints.NONE; constr.anchor = GridBagConstraints.CENTER; constr.gridx = 0; constr.gridy = 0; constr.ipadx = 3; constr.ipady = 3; gridb.setConstraints( seven , constr ); p2.add( seven); constr.fill = GridBagConstraints.NONE; constr.anchor = GridBagConstraints.CENTER; constr.gridx = 1; constr.gridy = 0; gridb.setConstraints( eight , constr ); p2.add( eight ); constr.fill = GridBagConstraints.NONE; constr.anchor = GridBagConstraints.CENTER; constr.gridx = 2; constr.gridy = 0; gridb.setConstraints( nine , constr ); p2.add( nine ); constr.fill = GridBagConstraints.NONE; constr.anchor = GridBagConstraints.CENTER; constr.gridx = 3; constr.gridy = 0; gridb.setConstraints( plus , constr ); p2.add( plus ); constr.fill = GridBagConstraints.NONE; constr.anchor = GridBagConstraints.CENTER; constr.gridx = 0; constr.gridy = 1; gridb.setConstraints( four , constr ); p2.add( four ); constr.fill = GridBagConstraints.NONE; constr.anchor = GridBagConstraints.CENTER; constr.gridx = 1; constr.gridy = 1; gridb.setConstraints( five , constr ); p2.add( five ); constr.fill = GridBagConstraints.NONE; constr.anchor = GridBagConstraints.CENTER; constr.gridx = 2; constr.gridy = 1; gridb.setConstraints( six , constr ); p2.add( six ); constr.fill = GridBagConstraints.NONE; constr.anchor = GridBagConstraints.CENTER; constr.gridx = 3; constr.gridy = 1; gridb.setConstraints( minus , constr ); p2.add( minus ); constr.fill = GridBagConstraints.NONE; constr.anchor = GridBagConstraints.CENTER; constr.gridx = 0; constr.gridy = 2; gridb.setConstraints( one , constr ); p2.add( one ); constr.fill = GridBagConstraints.NONE; constr.anchor = GridBagConstraints.CENTER; constr.gridx = 1; constr.gridy = 2; gridb.setConstraints( two , constr ); p2.add( two ); constr.fill = GridBagConstraints.NONE; constr.anchor = GridBagConstraints.CENTER; constr.gridx = 2; constr.gridy = 2; gridb.setConstraints( three , constr ); p2.add( three ); constr.fill = GridBagConstraints.NONE; constr.anchor = GridBagConstraints.CENTER; constr.gridx = 3; constr.gridy = 2; gridb.setConstraints( mult , constr ); p2.add( mult ); constr.fill = GridBagConstraints.NONE; constr.anchor = GridBagConstraints.CENTER; constr.gridx = 0; constr.gridy = 3; gridb.setConstraints( equa , constr ); p2.add( equa ); constr.fill = GridBagConstraints.NONE; constr.anchor = GridBagConstraints.CENTER; constr.gridx = 1; constr.gridy = 3; gridb.setConstraints( zerow , constr ); p2.add( zerow ); constr.fill = GridBagConstraints.NONE; constr.anchor = GridBagConstraints.CENTER; constr.gridx = 2; constr.gridy = 3; gridb.setConstraints( point , constr ); p2.add( point ); constr.fill = GridBagConstraints.NONE; constr.anchor = GridBagConstraints.CENTER; constr.gridx = 3; constr.gridy = 3; gridb.setConstraints( divide , constr ); p2.add( divide ); p1.setLayout( gridb3 ); p1.setBackground( new Color(color) ); p1.setForeground( Color.black ); constr.fill = GridBagConstraints.NONE; constr.anchor = GridBagConstraints.CENTER; constr.gridx = 0; constr.gridy = 0; gridb3.setConstraints( clear , constr ); p1.add( clear ); constr.fill = GridBagConstraints.NONE; constr.anchor = GridBagConstraints.CENTER; constr.gridx = 1; constr.gridy = 0; gridb3.setConstraints( info , constr ); p1.add( info ); p.setLayout( gridb2 ); p.setBackground( new Color(color) ); p.setForeground( Color.black ); constr.fill = GridBagConstraints.HORIZONTAL; constr.anchor = GridBagConstraints.CENTER; constr.gridx = 0; constr.gridy = 0; gridb2.setConstraints( tf , constr ); p.add( tf ); constr.fill = GridBagConstraints.HORIZONTAL; constr.anchor = GridBagConstraints.CENTER; constr.gridx = 0; constr.gridy = 1; gridb2.setConstraints( p2 , constr );
搜索更多相关主题的帖子: 计算器 代码 分享 
2005-01-12 09:31
kingarden
Rank: 2
等 级:论坛游民
威 望:1
帖 子:517
专家分:40
注 册:2004-12-8
收藏
得分:0 
p.add( p2 ); constr.fill = GridBagConstraints.NONE; constr.anchor = GridBagConstraints.CENTER; constr.gridx = 0; constr.gridy = 2; gridb2.setConstraints( p1 , constr ); p.add( p1 ); constr.fill = GridBagConstraints.NONE; constr.anchor = GridBagConstraints.CENTER; constr.gridx = 0; constr.gridy = 3; gridb2.setConstraints( p3 , constr ); p.add( p3 ); this.add( p ); try{ System.out.println("\n\nApplet Calculator Made by Patrik Lundin, f95-plu@nada.kth.se\n\n"); }catch(Exception ex){} } void evaluate() { String temp = ""; String tmp = ""; int ind1 = 0; int ind2 = 0; try{ tmp = prepare( tf.getText().toLowerCase() ); if( tmp.equals("") ){ return; } // check if is diff, set or clear() command. if( ( ind1 = tmp.indexOf("diff(")) != -1 ){ temp = de.diff( tmp.substring( ind1 + 5, tmp.lastIndexOf(")")))[0]; if( de.VARIABLES.indexOf(";") != -1 ){ tf.setText("Error: Multiple variables"); return; }else{ tf.setText( temp ); return; } }else if( ( ind1 = tmp.indexOf("set(") ) != -1 ){ try{ ind2 = tmp.lastIndexOf(")"); temp = tmp.substring( ind1 + 4, ind2 ); values.put( temp.substring( 0,temp.indexOf("=")) , temp.substring( temp.indexOf("=") + 1, temp.length())); tf.setText("Value set, " + getValues()); }catch(Exception ex){ tf.setText("Syntax error, " + tmp); } return; }else if( ( ind1 = tmp.indexOf("clear(")) != -1 ){ if( (ind2 = tmp.lastIndexOf(")")) == -1 ){ tf.setText("Non matching brackets"); return; } temp = tmp.substring( ind1 + 6 , ind2 ); values.remove( temp ); tf.setText( "Value cleared, " + getValues() ); return; }else if( tmp.indexOf("memory") != -1 ){ tf.setText( getValues() ); return; } // normal, evaluate. tf.setText( String.valueOf(ev.eval( tmp , values))); }catch(Exception ex){ tf.setText( ex.getMessage()); } } String getValues() { String temp = ""; String tmp = ""; Enumeration en = values.keys(); while( en.hasMoreElements() ){ tmp = (String)en.nextElement(); if( temp.equals("") ){ temp = tmp + "=" + (String)values.get( tmp ); }else{ temp += ";" + tmp + "=" + (String)values.get( tmp ); } } return temp; } public boolean keyDown(Event event, int key) { char c = (char)key; tf.requestFocus(); if( c == '\n' ){ evaluate(); return true; } return false; } public boolean handleEvent(Event event) { if( event.id == Event.ACTION_EVENT ){ if( event.target == info ){ new Msg("Info", information() ); return true; }else if( event.target == equa ){ evaluate(); return true; }else if( event.target == clear ){ tf.setText(""); return true; } } return super.handleEvent(event); } public boolean action(Event event, Object ob) { if( event.target instanceof Button ){ tf.setText( tf.getText() + (String)ob ); return true; } return false; } String information() { String message = "wwmm2xll" return message; } String prepare(String str) { String nstr = ""; int i = 0; int count = 0; while( i < str.length() ){ if(str.charAt( i ) == '('){ count++; }else if(str.charAt( i ) == ')'){ count--; } if( str.charAt( i ) != ' ' ){ nstr += str.charAt( i ); } i++; } if( count != 0 ){ tf.setText("Non matching brackets, " + str); return ""; } return nstr; } }// end class calculator. class Msg extends Frame{ public Msg(String title, String message) { super(); GridBagLayout gridb = new GridBagLayout(); GridBagConstraints constr = new GridBagConstraints(); Msg a = new Msg(title); a.setLayout( gridb ); TextArea p = new TextArea( message ); p.resize( 250 , 250 ); Button b = new Button("OK, Close This Window"); constr.fill = GridBagConstraints.BOTH; constr.anchor = GridBagConstraints.NORTH; constr.gridx = 0; constr.gridy = 0; gridb.setConstraints( p , constr ); a.add( p ); constr.fill = GridBagConstraints.HORIZONTAL; constr.anchor = GridBagConstraints.CENTER; constr.gridx = 0; constr.gridy = 1; gridb.setConstraints( b , constr ); a.add( b ); a.pack(); a.show(); } public Msg(String s) { super(s); } public boolean action(Event ev, Object o) { this.hide(); this.dispose(); return true; } } // end class Msg.

〖ΦγЙ┅给梦一把梯子,现实与梦想之间的距离即可忽略,不可跨越的迢迢银河举步便可迈过Й┅Φ〗
2005-01-12 09:33
esffor
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2005-11-7
收藏
得分:0 
你不把自己的包发上来 我们怎么编译??
2005-11-07 17:13
曾阿牛
Rank: 1
等 级:新手上路
帖 子:68
专家分:0
注 册:2005-10-14
收藏
得分:0 
弄下来看看 哎 好多类和方法原来都没见过 搬着书挨个查 学的好艰辛啊

什么时候才能成为论坛游民啊....
2005-11-10 12:13
白展堂
Rank: 1
等 级:新手上路
帖 子:62
专家分:0
注 册:2007-1-2
收藏
得分:0 

有错误


2007-01-18 20:53
快速回复:[分享]计算器原代码
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.042877 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved