各种计算器java代码.docx

上传人:b****8 文档编号:9868709 上传时间:2023-02-07 格式:DOCX 页数:40 大小:59.39KB
下载 相关 举报
各种计算器java代码.docx_第1页
第1页 / 共40页
各种计算器java代码.docx_第2页
第2页 / 共40页
各种计算器java代码.docx_第3页
第3页 / 共40页
各种计算器java代码.docx_第4页
第4页 / 共40页
各种计算器java代码.docx_第5页
第5页 / 共40页
点击查看更多>>
下载资源
资源描述

各种计算器java代码.docx

《各种计算器java代码.docx》由会员分享,可在线阅读,更多相关《各种计算器java代码.docx(40页珍藏版)》请在冰豆网上搜索。

各种计算器java代码.docx

各种计算器java代码

第一种:

importjava.awt.*;

importjava.awt.event.*;

publicclassCal{

publicstaticvoidmain(String[]args){

CalFramef=newCalFrame();

}

}

classCalFrameextendsFrame{

doubled1,d2;

intop=-1;

TextFieldtf;

CalPanelLp1;

CalPanelRp2;

//Constructor

CalFrame(){

super("SmallCalculator");

setLayout(newFlowLayout(FlowLayout.CENTER,8,10));

setBackground(newColor(100,150,150));

setForeground(Color.white);

setResizable(false);

setSize(300,200);

tf=newTextField(22);

tf.setEditable(false);

tf.setBackground(newColor(108,118,103));

tf.setForeground(Color.white);

tf.setFont(newFont("Arial",Font.BOLD,16));

add(tf);

p1=newCalPanelL();

p2=newCalPanelR();

add(p1);

add(p2);

setVisible(true);

addWindowListener(newWclose());

}

//innerclass:

CalButton

classCalButtonextendsButton{

CalButton(Strings){

super(s);

setBackground(Color.gray);

}

}

//innerclass:

CalPanelL

classCalPanelLextendsPanel{

CalButtonb0,b1,b2,b3,

b4,b5,b6,b7,

b8,b9,bPN,bPoint;

CalPanelL(){

setLayout(newGridLayout(4,3));

setFont(newFont("TimesRoman",Font.BOLD,16));

b0=newCalButton("0");

b1=newCalButton("1");

b2=newCalButton("2");

b3=newCalButton("3");

b4=newCalButton("4");

b5=newCalButton("5");

b6=newCalButton("6");

b7=newCalButton("7");

b8=newCalButton("8");

b9=newCalButton("9");

bPN=newCalButton("+/-");

bPoint=newCalButton(".");

//加入按钮

add(b7);b7.addActionListener(newPressB7());

add(b8);b8.addActionListener(newPressB8());

add(b9);b9.addActionListener(newPressB9());

add(b4);b4.addActionListener(newPressB4());

add(b5);b5.addActionListener(newPressB5());

add(b6);b6.addActionListener(newPressB6());

add(b1);b1.addActionListener(newPressB1());

add(b2);b2.addActionListener(newPressB2());

add(b3);b3.addActionListener(newPressB3());

add(b0);b0.addActionListener(newPressB0());

add(bPN);bPN.addActionListener(newPressBPN());;

add(bPoint);bPoint.addActionListener(newPressBPoint());

}

}

classCalPanelRextendsPanel{

CalButtonbAdd,bSub,bMul,bDiv,

bSqrt,bSin,bCos,bYx,

bLn,bEqual,bCE,bBack;

CalPanelR(){

setLayout(newGridLayout(4,3));

setFont(newFont("TimesRoman",Font.BOLD,16));

bAdd=newCalButton("+");

bSub=newCalButton("-");

bMul=newCalButton("*");

bDiv=newCalButton("/");

bSqrt=newCalButton("sqrt");

bSin=newCalButton("sin");

bCos=newCalButton("cos");

bYx=newCalButton("y^x");

bLn=newCalButton("ln");

bEqual=newCalButton("=");

bCE=newCalButton("CE");

bBack=newCalButton("<-");

add(bDiv);bDiv.addActionListener(newPressBDiv());

add(bSqrt);bSqrt.addActionListener(newPressBSqrt());

add(bLn);bLn.addActionListener(newPressBLn());

add(bMul);bMul.addActionListener(newPressBMul());

add(bSin);bSin.addActionListener(newPressBSin());

add(bBack);bBack.addActionListener(newPressBBack());

add(bSub);bSub.addActionListener(newPressBSub());

add(bCos);bCos.addActionListener(newPressBCos());

add(bCE);bCE.addActionListener(newPressBCE());

add(bAdd);bAdd.addActionListener(newPressBAdd());

add(bYx);bYx.addActionListener(newPressBYx());

add(bEqual);bEqual.addActionListener(newPressBEqual());

}

}

classPressBAddimplementsActionListener{

publicvoidactionPerformed(ActionEvente){

try{

d1=Double.parseDouble(tf.getText());

op=0;

tf.setText("");

}catch(Exceptionee){}

}

}

classPressBSubimplementsActionListener{

publicvoidactionPerformed(ActionEvente){

try{

d1=Double.parseDouble(tf.getText());

op=1;

tf.setText("");

}catch(Exceptionee){}

}

}

classPressBMulimplementsActionListener{

publicvoidactionPerformed(ActionEvente){

try{

d1=Double.parseDouble(tf.getText());

op=2;

tf.setText("");

}catch(Exceptionee){}

}

}

classPressBDivimplementsActionListener{

publicvoidactionPerformed(ActionEvente){

try{

d1=Double.parseDouble(tf.getText());

op=3;

tf.setText("");

}catch(Exceptionee){}

}

}

classPressBYximplementsActionListener{

publicvoidactionPerformed(ActionEvente){

try{

d1=Double.parseDouble(tf.getText());

op=4;

tf.setText("");

}catch(Exceptionee){}

}

}

classPressBEqualimplementsActionListener{

publicvoidactionPerformed(ActionEvente){

try{

doubleresult=0;

d2=Double.parseDouble(tf.getText());

switch(op){

case0:

result=d1+d2;

break;

case1:

result=d1-d2;

break;

case2:

result=d1*d2;

break;

case3:

result=d1/d2;

break;

case4:

result=Math.pow(d1,d2);

break;

default:

}

tf.setText(String.valueOf(result));

}catch(Exceptionee){}

}

}

classPressBSqrtimplementsActionListener{

publicvoidactionPerformed(ActionEvente){

try{

doublex=Double.parseDouble(tf.getText());

doubley;

y=Math.sqrt(x);

tf.setText(y+"");

}catch(Exceptionee){}

}

}

classPressBLnimplementsActionListener{

publicvoidactionPerformed(ActionEvente){

try{

doublex=Double.parseDouble(tf.getText());

doubley;

y=Math.log(x);

tf.setText(y+"");

}catch(Exceptionee){}

}

}

classPressBSinimplementsActionListener{

publicvoidactionPerformed(ActionEvente){

try{

doublex=Double.parseDouble(tf.getText());

doubley;

y=Math.sin(x);

tf.setText(y+"");

}catch(Exceptionee){}

}

}

classPressBCosimplementsActionListener{

publicvoidactionPerformed(ActionEvente){

try{

doublex=Double.parseDouble(tf.getText());

doubley;

y=Math.cos(x);

tf.setText(y+"");

}catch(Exceptionee){}

}

}

classPressBBackimplementsActionListener{

publicvoidactionPerformed(ActionEvente){

try{

Stringtext=tf.getText();

text=text.substring(0,text.length()-1);

tf.setText(text);

}catch(Exceptionee){}

}

}

classPressBCEimplementsActionListener{

publicvoidactionPerformed(ActionEvente){

tf.setText("");

}

}

classPressBPNimplementsActionListener{

publicvoidactionPerformed(ActionEvente){

try{

Stringtext=tf.getText();

if(text!

="")

if(text.charAt(0)=='-')

tf.setText(text.substring

(1));

elseif(text.charAt(0)>='0'&&text.charAt(0)<='9')

tf.setText("-"+text.substring(0));

elseif(text.charAt(0)=='.')

tf.setText("-0"+text.substring(0));

}catch(Exceptionee){}

}}

classPressBPointimplementsActionListener{

publicvoidactionPerformed(ActionEvente){

Stringtext=tf.getText();

if(text.lastIndexOf(".")==-1)

tf.setText(text+".");

}

}

classPressB0implementsActionListener{

publicvoidactionPerformed(ActionEvente){

Stringtext=tf.getText();

tf.setText(text+"0");

}

}

classPressB1implementsActionListener{

publicvoidactionPerformed(ActionEvente){

Stringtext=tf.getText();

tf.setText(text+"1");

}

}

classPressB2implementsActionListener{

publicvoidactionPerformed(ActionEvente){

Stringtext=tf.getText();

tf.setText(text+"2");

}

}

classPressB3implementsActionListener{

publicvoidactionPerformed(ActionEvente){

Stringtext=tf.getText();

tf.setText(text+"3");

}

}

classPressB4implementsActionListener{

publicvoidactionPerformed(ActionEvente){

Stringtext=tf.getText();

tf.setText(text+"4");

}

}

classPressB5implementsActionListener{

publicvoidactionPerformed(ActionEvente){

Stringtext=tf.getText();

tf.setText(text+"5");

}

}

classPressB6implementsActionListener{

publicvoidactionPerformed(ActionEvente){

Stringtext=tf.getText();

tf.setText(text+"6");

}

}

classPressB7implementsActionListener{

publicvoidactionPerformed(ActionEvente){

Stringtext=tf.getText();

tf.setText(text+"7");

}

}

classPressB8implementsActionListener{

publicvoidactionPerformed(ActionEvente){

Stringtext=tf.getText();

tf.setText(text+"8");

}

}

classPressB9implementsActionListener{

publicvoidactionPerformed(ActionEvente){

Stringtext=tf.getText();

tf.setText(text+"9");

}

}

//class:

Wclose

classWcloseextendsWindowAdapter{

publicvoidwindowClosing(WindowEvente){

System.exit(0);

}

}

}

 

第二种:

importjava.awt.*;

importjava.awt.event.*;

importjava.applet.*;

importjavax.swing.JOptionPane;

publicclassCalculatorextendsAppletimplementsActionListener{

ButtonbackButton=newButton("退格");

ButtonclearButton=newButton("清除");

Panelp1=newPanel(newFlowLayout());

Panelp2=newPanel(newGridLayout(2,1));

Panelp3=newPanel(newGridLayout(4,5,5,5));

TextFieldt=newTextField("0");

floatnum1=0;

charch='#';

booleancan=false;

publicvoidinit(){

t.setFont(newFont("宋体",Font.BOLD,25));

backButton.setFont(newFont("黑体",Font.BOLD,15));

backButton.setForeground(Color.red);

clearButton.setFont(newFont("黑体",Font.BOLD,15));

clearButton.setForeground(Color.red);

p1.add(backButton);

p1.add(clearButton);

backButton.addActionListener(this);

clearButton.addActionListener(this);

p2.add(t);

p2.add(p1);

p2.setBackground(Color.black);

p3.setBackground(Color.black);

this.setLayout(newBorderLayout());

this.add(p2,"North");

this.add(p3,"Center");

StringbuttonStr="789/A456*B123-C0.D+=";

for(inti=0;i

this.addButton(p3,buttonStr.substring(i,i+1));

}

privatevoidaddButton(Containerc,Strings)

{Buttonb=newButton(s);

if(s.equals("

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高等教育 > 文学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1