您好,欢迎来到星星旅游。
搜索
您的当前位置:首页计算器

计算器

来源:星星旅游
程序代码:

import java.awt.*;

import java.awt.event.*; import javax.swing.*;

public class calculator extends JApplet{

Container contentPane;

JButton btn0 = new JButton(); JButton btn1 = new JButton(); JButton btn2 = new JButton(); JButton btn3 = new JButton(); JButton btn4 = new JButton(); JButton btn5 = new JButton(); JButton btn6 = new JButton(); JButton btn7 = new JButton(); JButton btn8 = new JButton(); JButton btn9 = new JButton();

//特殊按钮

JButton btnC = new JButton(); //清除 JButton btnEq = new JButton(); //等于

JButton btnAdd = new JButton(); //+操作符 JButton btnMin = new JButton(); //-操作符 JButton btnMul = new JButton(); //*操作符 JButton btnDiv = new JButton(); //除操作符

JTextField texResult = new JTextField();

boolean flag=false; String op1; String op2; double result; String action;

public void init(){

contentPane = getContentPane(); contentPane.setLayout(null); setSize(250, 250);

btn0.setBounds(50, 230, 45, 45);

btn0.setFont(new Font(\"Tahoma\", Font.PLAIN, 14)); btn0.setText(\"0\");

btn0.addActionListener(new btn0(this));

//清除操作符

btnC.setBounds(100,230,45,45);

btnC.setFont(new Font(\"Tahoma\",Font.PLAIN,14)); btnC.setText(\"C\");

btnC.addActionListener(new btnClear(this));

//=操作符

btnEq.setBounds(150,230,45,45);

btnEq.setFont(new Font(\"Tahoma\",Font.PLAIN,14)); btnEq.setText(\"=\");

btnEq.addActionListener(new btnEqual(this));

//+操作符

btnAdd.setBounds(200,230,45,45);

btnAdd.setFont(new Font(\"Tahoma\",Font.PLAIN,14)); btnAdd.setText(\"+\");

btnAdd.addActionListener(new btnPlus(this));

btn1.setBounds(50,180,45,45);

btn1.setFont(new Font(\"Tahoma\",Font.PLAIN,14)); btn1.setText(\"1\");

btn1.addActionListener(new btn1(this));

btn2.setBounds(100,180,45,45);

btn2.setFont(new Font(\"Tahoma\",Font.PLAIN,14)); btn2.setText(\"2\");

btn2.addActionListener(new btn2(this));

btn3.setBounds(150,180,45,45);

btn3.setFont(new Font(\"Tahoma\",Font.PLAIN,14)); btn3.setText(\"3\");

btn3.addActionListener(new btn3(this));

//-操作符

btnMin.setBounds(200,180,45,45);

btnMin.setFont(new Font(\"Tahoma\",Font.PLAIN,16)); btnMin.setText(\"-\");

btnMin.addActionListener(new btnMinus(this));

btn4.setBounds(50,130,45,45);

btn4.setFont(new Font(\"Tahoma\",Font.PLAIN,14)); btn4.setText(\"4\");

btn4.addActionListener(new btn4(this));

btn5.setBounds(100,130,45,45);

btn5.setFont(new Font(\"Tahoma\",Font.PLAIN,14)); btn5.setText(\"5\");

btn5.addActionListener(new btn5(this));

btn6.setBounds(150,130,45,45);

btn6.setFont(new Font(\"Tahoma\",Font.PLAIN,14)); btn6.setText(\"6\");

btn6.addActionListener(new btn6(this));

//*操作符

btnMul.setBounds(200,130,45,45);

btnMul.setFont(new Font(\"Tahoma\",Font.PLAIN,14)); btnMul.setText(\"*\");

btnMul.addActionListener(new btnMultiply(this));

btn7.setBounds(50,80,45,45);

btn7.setFont(new Font(\"Tahoma\",Font.PLAIN,14)); btn7.setText(\"7\");

btn7.addActionListener(new btn7(this));

btn8.setBounds(100,80,45,45);

btn8.setFont(new Font(\"Tahoma\",Font.PLAIN,14)); btn8.setText(\"8\");

btn8.addActionListener(new btn8(this));

btn9.setBounds(150,80,45,45);

btn9.setFont(new Font(\"Tahoma\",Font.PLAIN,14)); btn9.setText(\"9\");

btn9.addActionListener(new btn9(this));

//除操作符

btnDiv.setBounds(200,80,45,45);

btnDiv.setFont(new Font(\"Tahoma\",Font.PLAIN,14)); btnDiv.setText(\"/\");

btnDiv.addActionListener(new btnDivide(this));

texResult.setText(\"\");

texResult.setBounds(40, 35, 215, 35);

texResult.setFont(new Font(\"Tahoma\", Font.PLAIN, 14)); texResult.setHorizontalAlignment(JTextField.RIGHT);

contentPane.add(btn0); contentPane.add(btn1); contentPane.add(btn2); contentPane.add(btn3); contentPane.add(btn4); contentPane.add(btn5); contentPane.add(btn6); contentPane.add(btn7); contentPane.add(btn8); contentPane.add(btn9);

contentPane.add(btnC); contentPane.add(btnEq); contentPane.add(btnAdd); contentPane.add(btnMin); contentPane.add(btnMul); contentPane.add(btnDiv);

contentPane.add(texResult); }

//0的事件监听器

public void btn0_actionPerformed(ActionEvent e){

if(flag){

texResult.setText(btn0.getActionCommand()); flag = false; }

else{

texResult.setText(texResult.getText()+btn0.getActionCommand()); } }

class btn0 implements ActionListener{

private calculator adaptee;

btn0(calculator adaptee){ this.adaptee=adaptee; }

public void actionPerformed(ActionEvent e){ adaptee.btn0_actionPerformed(e);

} }

//1的事件监听器

public void btn1_actionPerformed(ActionEvent e){

if(flag){

texResult.setText(btn1.getActionCommand()); flag = false; }

else{

texResult.setText(texResult.getText()+btn1.getActionCommand()); } }

class btn1 implements ActionListener{

private calculator adaptee;

btn1(calculator adaptee){ this.adaptee=adaptee; }

public void actionPerformed(ActionEvent e){ adaptee.btn1_actionPerformed(e); } }

//2的事件监听器

public void btn2_actionPerformed(ActionEvent e){

if(flag){

texResult.setText(btn2.getActionCommand()); flag = false; }

else{

texResult.setText(texResult.getText()+btn2.getActionCommand()); } }

class btn2 implements ActionListener{

private calculator adaptee;

btn2(calculator adaptee){ this.adaptee=adaptee; }

public void actionPerformed(ActionEvent e){ adaptee.btn2_actionPerformed(e); } }

//3的事件监听器

public void btn3_actionPerformed(ActionEvent e){

if(flag){

texResult.setText(btn3.getActionCommand()); flag = false; }

else{

texResult.setText(texResult.getText()+btn3.getActionCommand()); } }

class btn3 implements ActionListener{

private calculator adaptee;

btn3(calculator adaptee){ this.adaptee=adaptee; }

public void actionPerformed(ActionEvent e){ adaptee.btn3_actionPerformed(e); } }

//4的事件监听器

public void btn4_actionPerformed(ActionEvent e){

if(flag){

texResult.setText(btn4.getActionCommand()); flag = false; }

else{

texResult.setText(texResult.getText()+btn4.getActionCommand()); } }

class btn4 implements ActionListener{

private calculator adaptee;

btn4(calculator adaptee){ this.adaptee=adaptee; }

public void actionPerformed(ActionEvent e){ adaptee.btn4_actionPerformed(e); } }

//5的事件监听器

public void btn5_actionPerformed(ActionEvent e){

if(flag){

texResult.setText(btn5.getActionCommand()); flag = false; }

else{

texResult.setText(texResult.getText()+btn5.getActionCommand()); } }

class btn5 implements ActionListener{

private calculator adaptee;

btn5(calculator adaptee){ this.adaptee=adaptee; }

public void actionPerformed(ActionEvent e){ adaptee.btn5_actionPerformed(e); } }

//6的事件监听器

public void btn6_actionPerformed(ActionEvent e){

if(flag){

texResult.setText(btn6.getActionCommand()); flag = false; }

else{

texResult.setText(texResult.getText()+btn6.getActionCommand()); } }

class btn6 implements ActionListener{

private calculator adaptee;

btn6(calculator adaptee){ this.adaptee=adaptee; }

public void actionPerformed(ActionEvent e){ adaptee.btn6_actionPerformed(e); } }

//7的事件监听器

public void btn7_actionPerformed(ActionEvent e){

if(flag){

texResult.setText(btn7.getActionCommand()); flag = false; }

else{

texResult.setText(texResult.getText()+btn7.getActionCommand()); } }

class btn7 implements ActionListener{

private calculator adaptee;

btn7(calculator adaptee){ this.adaptee=adaptee;

}

public void actionPerformed(ActionEvent e){ adaptee.btn7_actionPerformed(e); } }

//8的事件监听器

public void btn8_actionPerformed(ActionEvent e){

if(flag){

texResult.setText(btn8.getActionCommand()); flag = false; }

else{

texResult.setText(texResult.getText()+btn8.getActionCommand()); } }

class btn8 implements ActionListener{

private calculator adaptee;

btn8(calculator adaptee){ this.adaptee=adaptee; }

public void actionPerformed(ActionEvent e){ adaptee.btn8_actionPerformed(e); } }

//9的事件监听器

public void btn9_actionPerformed(ActionEvent e){

if(flag){

texResult.setText(btn9.getActionCommand()); flag = false; }

else{

texResult.setText(texResult.getText()+btn9.getActionCommand()); } }

class btn9 implements ActionListener{

private calculator adaptee;

btn9(calculator adaptee){ this.adaptee=adaptee; }

public void actionPerformed(ActionEvent e){ adaptee.btn9_actionPerformed(e); } }

//清除操作

public void btnClear_actionPerformed(ActionEvent e){

texResult.setText(\"\"); }

class btnClear implements ActionListener{

private calculator adaptee;

btnClear(calculator adaptee){ this.adaptee=adaptee; }

public void actionPerformed(ActionEvent e){ adaptee.btnClear_actionPerformed(e); } }

//+操作事件监听器

public void btnPlus_actionPerformed(ActionEvent e){

op1 = texResult.getText(); action = \"plus\"; flag = true; }

class btnPlus implements ActionListener{

private calculator adaptee;

btnPlus(calculator adaptee){ this.adaptee=adaptee; }

public void actionPerformed(ActionEvent e){ adaptee.btnPlus_actionPerformed(e); } }

//-操作事件监听器

public void btnMinus_actionPerformed(ActionEvent e){

op1 = texResult.getText(); action = \"minus\"; flag = true; }

class btnMinus implements ActionListener{

private calculator adaptee;

btnMinus(calculator adaptee){ this.adaptee=adaptee; }

public void actionPerformed(ActionEvent e){ adaptee.btnMinus_actionPerformed(e); } }

//*操作事件监听器

public void btnMultiply_actionPerformed(ActionEvent e){

op1 = texResult.getText(); action = \"multiply\"; flag = true; }

class btnMultiply implements ActionListener{

private calculator adaptee;

btnMultiply(calculator adaptee){ this.adaptee=adaptee; }

public void actionPerformed(ActionEvent e){ adaptee.btnMultiply_actionPerformed(e); } }

//除操作事件监听器

public void btnDivide_actionPerformed(ActionEvent e){

op1 = texResult.getText(); action = \"divide\"; flag = true; }

class btnDivide implements ActionListener{

private calculator adaptee;

btnDivide(calculator adaptee){ this.adaptee=adaptee; }

public void actionPerformed(ActionEvent e){ adaptee.btnDivide_actionPerformed(e); } }

//=操作事件监听器

public void btnEqual_actionPerformed(ActionEvent e){

double digit1; double digit2;

op2 = texResult.getText();

if(!flag){

if(action.equals(\"plus\")){

digit1 = Double.parseDouble(op1); digit2 = Double.parseDouble(op2); result = digit1 + digit2;

texResult.setText(\"\" + result); flag = true; }

else if(action.equals(\"minus\")){

digit1 = Double.parseDouble(op1); digit2 = Double.parseDouble(op2); result = digit1 - digit2;

texResult.setText(\"\"+result); flag = true; }

else if(action.equals(\"multiply\")){

digit1 = Double.parseDouble(op1); digit2 = Double.parseDouble(op2); result = digit1 * digit2;

texResult.setText(\"\"+result); flag = true; }

else if(action.equals(\"divide\")){

digit1 = Double.parseDouble(op1); digit2 = Double.parseDouble(op2); result = digit1 / digit2;

texResult.setText(\"\"+result); flag = true; } } }

class btnEqual implements ActionListener{

private calculator adaptee;

btnEqual(calculator adaptee){ this.adaptee=adaptee; }

public void actionPerformed(ActionEvent e){ adaptee.btnEqual_actionPerformed(e); } }

public static void main(String args[]){ JApplet cal = new calculator(); JFrame frame = new JFrame(); cal.init();

frame.setTitle(\"小计算器\");

frame.getContentPane().add(cal); frame.setVisible(true); }

}

运行结果:

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- stra.cn 版权所有

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务