当前位置:首页 » 安卓系统 » android计算器源码

android计算器源码

发布时间: 2023-06-29 09:42:15

‘壹’ 开发一个简易的计算器APP程序 Android源代码

下面是效果展示:

复制代码代码如下:


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="s/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tvResult"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:height="50dp"
android:text="@string/tvResult"
/>
</LinearLayout>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/btnBackspace"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="150dp"
android:layout_marginLeft="10dp"
android:text="@string/btnbackspace"/>
<Button
android:id="@+id/btnCE"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="150dp"
android:text="@string/btnCE"/>
</LinearLayout>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/btn7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:width="75dp"
android:text="@string/btn7"/>
<Button
android:id="@+id/btn8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="75dp"
android:text="@string/btn8"/>
<Button
android:id="@+id/btn9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="75dp"
android:text="@string/btn9"/>
<Button
android:id="@+id/btnDiv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="75dp"
android:text="@string/btnDiv"/>
</LinearLayout>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/btn4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:width="75dp"
android:text="@string/btn4"/>
<Button
android:id="@+id/btn5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="75dp"
android:text="@string/btn5"/>
<Button
android:id="@+id/btn6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="75dp"
android:text="@string/btn6"/>
<Button
android:id="@+id/btnMul"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="75dp"
android:text="@string/btnMul"/>
</LinearLayout>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:width="75dp"
android:text="@string/btn1"/>
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="75dp"
android:text="@string/btn2"/>
<Button
android:id="@+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="75dp"
android:text="@string/btn3"/>
<Button
android:id="@+id/btnAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="75dp"
android:text="@string/btnAdd"/>
</LinearLayout>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/btn0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:width="75dp"
android:text="@string/btn0"/>
<Button
android:id="@+id/btnC"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="75dp"
android:text="@string/btnC"/>
<Button
android:id="@+id/btnEqu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="75dp"
android:text="@string/btnEqu"/>
<Button
android:id="@+id/btnSub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="75dp"
android:text="@string/btnSub"/>
</LinearLayout>
</LinearLayout>

复制代码代码如下:


package com.example.week2;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.app.Activity;

public class MainActivity extends Activity implements OnClickListener{

//声明一些控件
Button btn0=null;
Button btn1=null;
Button btn2=null;
Button btn3=null;
Button btn4=null;
Button btn5=null;
Button btn6=null;
Button btn7=null;
Button btn8=null;
Button btn9=null;
Button btnBackspace=null;
Button btnCE=null;
Button btnC=null;
Button btnAdd=null;
Button btnSub=null;
Button btnMul=null;
Button btnDiv=null;
Button btnEqu=null;
TextView tvResult=null;
//声明两个参数。接收tvResult前后的值
double num1=0,num2=0;
double Result=0;//计算结果
int op=0;//判断操作数,
boolean isClickEqu=false;//判断是否按了“=”按钮

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//从布局文件中获取控件,
btn0=(Button)findViewById(R.id.btn0);
btn1=(Button)findViewById(R.id.btn1);
btn2=(Button)findViewById(R.id.btn2);
btn3=(Button)findViewById(R.id.btn3);
btn4=(Button)findViewById(R.id.btn4);
btn5=(Button)findViewById(R.id.btn5);
btn6=(Button)findViewById(R.id.btn6);
btn7=(Button)findViewById(R.id.btn7);
btn8=(Button)findViewById(R.id.btn8);
btn9=(Button)findViewById(R.id.btn9);
btnBackspace=(Button)findViewById(R.id.btnBackspace);
btnCE=(Button)findViewById(R.id.btnCE);
btnC=(Button)findViewById(R.id.btnC);
btnEqu=(Button)findViewById(R.id.btnEqu);
btnAdd=(Button)findViewById(R.id.btnAdd);
btnSub=(Button)findViewById(R.id.btnSub);
btnMul=(Button)findViewById(R.id.btnMul);
btnDiv=(Button)findViewById(R.id.btnDiv);
tvResult=(TextView)findViewById(R.id.tvResult);

//添加监听
btnBackspace.setOnClickListener(this);
btnCE.setOnClickListener(this);

btn0.setOnClickListener(this);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);
btn4.setOnClickListener(this);
btn5.setOnClickListener(this);
btn6.setOnClickListener(this);
btn7.setOnClickListener(this);
btn8.setOnClickListener(this);
btn9.setOnClickListener(this);


btnAdd.setOnClickListener(this);
btnSub.setOnClickListener(this);
btnMul.setOnClickListener(this);
btnDiv.setOnClickListener(this);
btnEqu.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
//btnBackspace和CE--------------------
case R.id.btnBackspace:
String myStr=tvResult.getText().toString();
try {
tvResult.setText(myStr.substring(0, myStr.length()-1));
} catch (Exception e) {
tvResult.setText("");
}

break;
case R.id.btnCE:
tvResult.setText(null);
break;

//btn0--9---------------------------
case R.id.btn0:
if(isClickEqu)
{
tvResult.setText(null);
isClickEqu=false;
}
String myString=tvResult.getText().toString();
myString+="0";
tvResult.setText(myString);
break;
case R.id.btn1:
if(isClickEqu)
{
tvResult.setText(null);
isClickEqu=false;
}
String myString1=tvResult.getText().toString();
myString1+="1";
tvResult.setText(myString1);
break;
case R.id.btn2:
if(isClickEqu)
{
tvResult.setText(null);
isClickEqu=false;
}
String myString2=tvResult.getText().toString();
myString2+="2";
tvResult.setText(myString2);
break;
case R.id.btn3:
if(isClickEqu)
{
tvResult.setText(null);
isClickEqu=false;
}
String myString3=tvResult.getText().toString();
myString3+="3";
tvResult.setText(myString3);
break;
cas

‘贰’ android怎么在我做的小计算器里添加一个SQLite,让他储存数据,下面是源码

首先你要获得本机中可读写的SQlite,然后建一个属于你该程序的一张表用来存储数据。通过SQL语句去进行增、删、查、改,你可以去看看这个贴,上面对Sqlite的很多基本操作都做好了范例,你可以照着先练练手,http://blog.csdn.net/liuhe688/article/details/6715983

‘叁’ 简单计算器的源代码

package calculator;

import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JTextField;
import java.awt.Rectangle;
import javax.swing.JButton;
import javax.swing.BorderFactory;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.SystemColor;
import java.awt.Color;

/**
* <p>Title:</p>
*
* <p>Description: 这个类用来实现一个简单的计算器</p>
*
* <p>Copyright: Copyright (c) 2006年</p>
*
* <p>Company: </p>
*
* @author
* @version 1.0
*/
public class CalculatorFrame extends JFrame {

boolean flag = false;
String operand1;
String operand2;
double result;
String action;

BorderLayout borderLayout1 = new BorderLayout();
JTextField txtresult = new JTextField();
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 btn0 = new JButton();
JButton btnplus = new JButton();
JButton btnminus = new JButton();
JButton btnjultiply = new JButton();
JButton btndivide = new JButton();
JButton btnclear = new JButton();
JButton btnequal = new JButton();

public CalculatorFrame() {
try {
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}

private void jbInit() throws Exception {
getContentPane().setLayout(null);
txtresult.setBackground(SystemColor.WHITE);
txtresult.setBorder(BorderFactory.createLoweredBevelBorder());
txtresult.setDisabledTextColor(Color.black);
txtresult.setEditable(false);
txtresult.setHorizontalAlignment(JTextField.RIGHT);
txtresult.setBounds(new Rectangle(50, 50, 305, 25));
btn1.setBounds(new Rectangle(50, 200, 65, 30));
btn1.setBorder(BorderFactory.createRaisedBevelBorder());
btn1.setText("1");
btn1.addActionListener(new CalculatorFrame_btn1_actionAdapter(this));
btn2.setBounds(new Rectangle(130, 200, 65, 30));
btn2.setBorder(BorderFactory.createRaisedBevelBorder());
btn2.setText("2");
btn2.addActionListener(new CalculatorFrame_btn2_actionAdapter(this));
btn3.setBounds(new Rectangle(210, 200, 65, 30));
btn3.setBorder(BorderFactory.createRaisedBevelBorder());
btn3.setText("3");
btn3.addActionListener(new CalculatorFrame_btn3_actionAdapter(this));
btn4.setBounds(new Rectangle(50, 150, 65, 30));
btn4.setBorder(BorderFactory.createRaisedBevelBorder());
btn4.setText("4");
btn4.addActionListener(new CalculatorFrame_btn4_actionAdapter(this));
btn5.setBounds(new Rectangle(130, 150, 65, 30));
btn5.setBorder(BorderFactory.createRaisedBevelBorder());
btn5.setText("5");
btn5.addActionListener(new CalculatorFrame_btn5_actionAdapter(this));
btn6.setBounds(new Rectangle(210, 150, 65, 30));
btn6.setBorder(BorderFactory.createRaisedBevelBorder());
btn6.setText("6");
btn6.addActionListener(new CalculatorFrame_btn6_actionAdapter(this));
btn7.setBounds(new Rectangle(50, 100, 65, 30));
btn7.setBorder(BorderFactory.createRaisedBevelBorder());
btn7.setText("7");
btn7.addActionListener(new CalculatorFrame_btn7_actionAdapter(this));
btn8.setBounds(new Rectangle(130, 100, 65, 30));
btn8.setBorder(BorderFactory.createRaisedBevelBorder());
btn8.setText("8");
btn8.addActionListener(new CalculatorFrame_btn8_actionAdapter(this));
btn9.setBounds(new Rectangle(210, 100, 65, 30));
btn9.setBorder(BorderFactory.createRaisedBevelBorder());
btn9.setText("9");
btn9.addActionListener(new CalculatorFrame_btn9_actionAdapter(this));
btn0.setBounds(new Rectangle(50, 250, 65, 30));
btn0.setBorder(BorderFactory.createRaisedBevelBorder());
btn0.setText("0");
btn0.addActionListener(new CalculatorFrame_btn0_actionAdapter(this));
btnplus.setBounds(new Rectangle(290, 250, 65, 30));
btnplus.setBorder(BorderFactory.createRaisedBevelBorder());
btnplus.setText("+");
btnplus.addActionListener(new CalculatorFrame_btnplus_actionAdapter(this));
btnminus.setBounds(new Rectangle(290, 200, 65, 30));
btnminus.setBorder(BorderFactory.createRaisedBevelBorder());
btnminus.setText("-");
btnminus.addActionListener(new CalculatorFrame_btnminus_actionAdapter(this));
btnjultiply.setBounds(new Rectangle(290, 150, 65, 30));
btnjultiply.setBorder(BorderFactory.createRaisedBevelBorder());
btnjultiply.setText("*");
btnjultiply.addActionListener(new
CalculatorFrame_btnjultiply_actionAdapter(this));
btndivide.setBounds(new Rectangle(290, 100, 65, 30));
btndivide.setBorder(BorderFactory.createRaisedBevelBorder());
btndivide.setText("/");
btndivide.addActionListener(new CalculatorFrame_btndivide_actionAdapter(this));
btnclear.setBounds(new Rectangle(130, 250, 65, 30));
btnclear.setBorder(BorderFactory.createRaisedBevelBorder());
btnclear.setText("C");
btnclear.addActionListener(new CalculatorFrame_btnclear_actionAdapter(this));
btnequal.setBounds(new Rectangle(210, 250, 65, 30));
btnequal.setBorder(BorderFactory.createRaisedBevelBorder());
btnequal.setText("=");
btnequal.addActionListener(new CalculatorFrame_btnequal_actionAdapter(this));
this.getContentPane().setBackground(UIManager.getColor(
"MenuItem.background"));
this.setTitle("计算器");
this.getContentPane().add(txtresult);
this.getContentPane().add(btn1);
this.getContentPane().add(btn0);
this.getContentPane().add(btnclear);
this.getContentPane().add(btnplus);
this.getContentPane().add(btnequal);
this.getContentPane().add(btn2);
this.getContentPane().add(btn3);
this.getContentPane().add(btnminus);
this.getContentPane().add(btn4);
this.getContentPane().add(btn7);
this.getContentPane().add(btn5);
this.getContentPane().add(btn6);
this.getContentPane().add(btnjultiply);
this.getContentPane().add(btndivide);
this.getContentPane().add(btn9);
this.getContentPane().add(btn8);
}

public void btnplus_actionPerformed(ActionEvent e) {
action = "plus";
operand1 = txtresult.getText();
txtresult.setText("");
flag = true;
}

public void btnminus_actionPerformed(ActionEvent e) {
action = "minus";
operand1 = txtresult.getText();
txtresult.setText("");
flag = true;
}

public void btnjultiply_actionPerformed(ActionEvent e) {
action = "multiply";
operand1 = txtresult.getText();
txtresult.setText("");
flag = true;
}

public void btndivide_actionPerformed(ActionEvent e) {
action = "divide";
operand1 = txtresult.getText();
txtresult.setText("");
flag = true;
}

public void btnclear_actionPerformed(ActionEvent e) {
txtresult.setText("");
}

public void btn1_actionPerformed(ActionEvent e) {
if (flag) {
txtresult.setText(btn1.getActionCommand());
flag=false;
} else {
txtresult.setText(txtresult.getText() + btn1.getActionCommand());
}

}

public void btn2_actionPerformed(ActionEvent e) {
if (flag) {
txtresult.setText(btn2.getActionCommand());
flag=false;
} else {
txtresult.setText(txtresult.getText() + btn2.getActionCommand());
}

}

public void btn3_actionPerformed(ActionEvent e) {
if (flag) {
txtresult.setText(btn3.getActionCommand());
flag=false;
} else {
txtresult.setText(txtresult.getText() + btn3.getActionCommand());
}

}

public void btn4_actionPerformed(ActionEvent e) {
if (flag) {
txtresult.setText(btn4.getActionCommand());
flag=false;
} else {
txtresult.setText(txtresult.getText() + btn4.getActionCommand());
}

}

public void btn5_actionPerformed(ActionEvent e) {
if (flag) {
txtresult.setText(btn5.getActionCommand());
flag=false;
} else {
txtresult.setText(txtresult.getText() + btn5.getActionCommand());
}

}

public void btn6_actionPerformed(ActionEvent e) {
if (flag) {
txtresult.setText(btn6.getActionCommand());
flag=false;
} else {
txtresult.setText(txtresult.getText() + btn6.getActionCommand());
}

}

public void btn7_actionPerformed(ActionEvent e) {
if (flag) {
txtresult.setText(btn7.getActionCommand());
flag=false;
} else {
txtresult.setText(txtresult.getText() + btn7.getActionCommand());
}

}

public void btn8_actionPerformed(ActionEvent e) {
if (flag) {
txtresult.setText(btn8.getActionCommand());
flag=false;
} else {
txtresult.setText(txtresult.getText() + btn8.getActionCommand());
}

}

public void btn9_actionPerformed(ActionEvent e) {
if (flag) {
txtresult.setText(btn9.getActionCommand());
flag=false;
} else {
txtresult.setText(txtresult.getText() + btn9.getActionCommand());
}

}

public void btn0_actionPerformed(ActionEvent e) {
if (flag) {
txtresult.setText(btn0.getActionCommand());
flag=false;
} else {
txtresult.setText(txtresult.getText() + btn0.getActionCommand());
}

}

public void btnequal_actionPerformed(ActionEvent e) {
double digit1, digit2;
operand2 = txtresult.getText();
if (flag == false) {
if (action.equals("plus")) {
digit1 = Double.parseDouble(operand1);
digit2 = Double.parseDouble(operand2);
result = digit1 + digit2;
txtresult.setText(String.valueOf((int) result));
flag = true;
} else if (action.equals("minus")) {
digit1 = Double.parseDouble(operand1);
digit2 = Double.parseDouble(operand2);
result = digit1 - digit2;
txtresult.setText(String.valueOf((int) result));
flag = true;
} else if (action.equals("multiply")) {
digit1 = Double.parseDouble(operand1);
digit2 = Double.parseDouble(operand2);
result = digit1 * digit2;
txtresult.setText(String.valueOf((int) result));
flag = true;
} else if (action.equals("divide")) {
digit1 = Double.parseDouble(operand1);
digit2 = Double.parseDouble(operand2);
result = digit1 / digit2;
if (digit2==0) {
txtresult.setText("ERROR");
flag=true;
} else {
txtresult.setText(String.valueOf((int) result));
flag = true;
}
}

}

}

class CalculatorFrame_btnequal_actionAdapter implements ActionListener {
private CalculatorFrame adaptee;
CalculatorFrame_btnequal_actionAdapter(CalculatorFrame adaptee) {
this.adaptee = adaptee;
}

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

class CalculatorFrame_btn0_actionAdapter implements ActionListener {
private CalculatorFrame adaptee;
CalculatorFrame_btn0_actionAdapter(CalculatorFrame adaptee) {
this.adaptee = adaptee;
}

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

class CalculatorFrame_btn9_actionAdapter implements ActionListener {
private CalculatorFrame adaptee;
CalculatorFrame_btn9_actionAdapter(CalculatorFrame adaptee) {
this.adaptee = adaptee;
}

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

class CalculatorFrame_btn8_actionAdapter implements ActionListener {
private CalculatorFrame adaptee;
CalculatorFrame_btn8_actionAdapter(CalculatorFrame adaptee) {
this.adaptee = adaptee;
}

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

class CalculatorFrame_btn7_actionAdapter implements ActionListener {
private CalculatorFrame adaptee;
CalculatorFrame_btn7_actionAdapter(CalculatorFrame adaptee) {
this.adaptee = adaptee;
}

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

class CalculatorFrame_btn6_actionAdapter implements ActionListener {
private CalculatorFrame adaptee;
CalculatorFrame_btn6_actionAdapter(CalculatorFrame adaptee) {
this.adaptee = adaptee;
}

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

class CalculatorFrame_btn5_actionAdapter implements ActionListener {
private CalculatorFrame adaptee;
CalculatorFrame_btn5_actionAdapter(CalculatorFrame adaptee) {
this.adaptee = adaptee;
}

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

class CalculatorFrame_btn4_actionAdapter implements ActionListener {
private CalculatorFrame adaptee;
CalculatorFrame_btn4_actionAdapter(CalculatorFrame adaptee) {
this.adaptee = adaptee;
}

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

class CalculatorFrame_btn2_actionAdapter implements ActionListener {
private CalculatorFrame adaptee;
CalculatorFrame_btn2_actionAdapter(CalculatorFrame adaptee) {
this.adaptee = adaptee;
}

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

class CalculatorFrame_btn3_actionAdapter implements ActionListener {
private CalculatorFrame adaptee;
CalculatorFrame_btn3_actionAdapter(CalculatorFrame adaptee) {
this.adaptee = adaptee;
}

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

class CalculatorFrame_btnclear_actionAdapter implements ActionListener {
private CalculatorFrame adaptee;
CalculatorFrame_btnclear_actionAdapter(CalculatorFrame adaptee) {
this.adaptee = adaptee;
}

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

class CalculatorFrame_btndivide_actionAdapter implements ActionListener {
private CalculatorFrame adaptee;
CalculatorFrame_btndivide_actionAdapter(CalculatorFrame adaptee) {
this.adaptee = adaptee;
}

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

class CalculatorFrame_btnjultiply_actionAdapter implements ActionListener {
private CalculatorFrame adaptee;
CalculatorFrame_btnjultiply_actionAdapter(CalculatorFrame adaptee) {
this.adaptee = adaptee;
}

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

class CalculatorFrame_btnminus_actionAdapter implements ActionListener {
private CalculatorFrame adaptee;
CalculatorFrame_btnminus_actionAdapter(CalculatorFrame adaptee) {
this.adaptee = adaptee;
}

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

class CalculatorFrame_btnplus_actionAdapter implements ActionListener {
private CalculatorFrame adaptee;
CalculatorFrame_btnplus_actionAdapter(CalculatorFrame adaptee) {
this.adaptee = adaptee;
}

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

class CalculatorFrame_btn1_actionAdapter implements ActionListener {
private CalculatorFrame adaptee;
CalculatorFrame_btn1_actionAdapter(CalculatorFrame adaptee) {
this.adaptee = adaptee;
}

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

public class Calculator {

public static void main(String[] args) {

CalculatorFrame Objcal=new CalculatorFrame();

Objcal.setSize(410,320);
Objcal.setVisible(true);
}
}

‘肆’ 求一份JAVA计算器源代码,要有()+-*/和清除键及小数点的,不用科学计算器

importjava.awt.BorderLayout;
importjava.awt.Dimension;
importjava.awt.GridLayout;
importjava.awt.Toolkit;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;

importjavax.swing.JButton;
importjavax.swing.JFrame;
importjavax.swing.JPanel;
importjavax.swing.SwingUtilities;
importjavax.swing.UIManager;

{
privateJPaneljp;//中间的面板
privatedoubleresult;//计算结果
privatebooleanstart;//用于判断是否是首次输入,true首次,false不是首次
privatebooleanflag;//用于判断是否清空显示区域的值,true需要,false不需要
privateStringlastCommand;//用于保存运算符
JButtondisplay;//显示区域

publicCalculatorPanel(){
//初始化各项值
result=0;
start=true;
flag=false;
lastCommand="=";

//设置CalculatorPanel这个面板的布局为框架布局
setLayout(newBorderLayout());
//这个按钮用来显示值的区域
display=newJButton("0.0");
display.setEnabled(false);//按钮样式设置为禁用样式

//用来初始化,清除用的
JButtonclear=newJButton("clear");
clear.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
//作初始化的操作
result=0;
start=true;
flag=false;
lastCommand="=";
display.setText("0.0");
}
});

jp=newJPanel();
//将此面板布局设置为网格布局,4行4列
jp.setLayout(newGridLayout(4,4));

//实例化监听器对象
NumberActionna=newNumberAction();
CommandActionca=newCommandAction();

makeButton("7",na);
makeButton("8",na);
makeButton("9",na);
makeButton("/",ca);

makeButton("4",na);
makeButton("5",na);
makeButton("6",na);
makeButton("*",ca);

makeButton("1",na);
makeButton("2",na);
makeButton("3",na);
makeButton("-",ca);

makeButton("0",na);
makeButton(".",na);
makeButton("+",ca);
makeButton("=",ca);

//将面板,两个按键加入到我的面板,实现计算器的界面
add(display,BorderLayout.NORTH);
add(jp,BorderLayout.CENTER);
add(clear,BorderLayout.SOUTH);

}

privatevoidmakeButton(StringbuttonName,ActionListeneral){
JButtonjb=newJButton(buttonName);
jp.add(jb);
jb.addActionListener(al);
}

//数字监听器
{
@Override
publicvoidactionPerformed(ActionEvente){
JButtonjb=(JButton)e.getSource();//获取事件源对象
Stringinput=jb.getText();//获取事件源对象上的标签
if(start){//首次输入
//一上就".",就什么也不做
if(input.equals(".")){
return;
}
//如果是"0.0"的话,就清空
if(display.getText().equals("0.0")){
display.setText("");
}
start=false;//改变是否首次输入的标记值
}else{
//判断显示区域的值里面是否已经有".",如果有,输入的又是".",就什么都不做
if(display.getText().indexOf(".")!=-1){
if(input.equals(".")){
return;
}
}

//判断显示区域的值里面只有"-",输入的又是".",就什么都不做
if(display.getText().equals("-")){
if(input.equals(".")){
return;
}
}

//判断显示区域的值如果是"0",输入的不是".",就什么也不做
if(display.getText().equals("0")){
if(!input.equals(".")){
return;
}
}
}
//如果我点击了运算符以后,再输入数字的话,就要清空显示区域的值
if(flag){
display.setText("");
flag=false;//还原初始值,不需要清空
}
display.setText(display.getText()+input);//设置显示区域的值
}
}

//运算符监听器
{
@Override
publicvoidactionPerformed(ActionEvente){
JButtonjb=(JButton)e.getSource();//获取事件源对象
StringinputCommand=jb.getText();//获取事件源对象上的标签

if(start){//首次输入
//一上就是"-"
if(inputCommand.equals("-")){
display.setText("-");//显示区域的内容设置为"-"
start=false;//改变首次输入的标记
}
}else{

if(!flag){//如果flag=false不需要清空显示区的值,就调用方法计算
calculate(Double.parseDouble(display.getText()));//保存显示区域的值,并计算
}
//保存你点击的运算符
lastCommand=inputCommand;
flag=true;//因为我这里已经输入过运算符,
}
}
}

//计算用的方法
privatevoidcalculate(doublex){
if(lastCommand.equals("+")){
result+=x;
}elseif(lastCommand.equals("-")){
result-=x;
}elseif(lastCommand.equals("*")){
result*=x;
}elseif(lastCommand.equals("/")){
result/=x;
}elseif(lastCommand.equals("=")){
result=x;
}
display.setText(""+result);
}
}

classMyFrameUitl{
publicstaticvoidinit(JFramejFrame,DimensionframeSize,Stringtitle,
StringiconFileName,booleanresizable){
Toolkittk=Toolkit.getDefaultToolkit();//系统工具包,可以读取系统信息
DimensionscreenSize=tk.getScreenSize();//获取屏幕大小
//获取宽高
intscreenWidth=screenSize.width;
intscreenHeight=screenSize.height;

//找中心点
intcenterX=screenWidth/2;
intcenterY=screenHeight/2;

//解析窗体大小
intframeWidth=frameSize.width;
intframeHeight=frameSize.height;

//设置窗体位置
jFrame.setBounds(centerX-frameWidth/2,centerY-frameHeight/2,
frameWidth,frameHeight);
jFrame.setTitle(title);//设置标题
if(iconFileName!=null){
jFrame.setIconImage(tk.getImage(iconFileName));//设置图标
}

//设置皮肤com.sun.java.swing.plaf.windows.WindowsLookAndFeel
/**/
try{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
SwingUtilities.updateComponentTreeUI(jFrame);
}catch(Exceptione1){
e1.printStackTrace();
}

jFrame.setResizable(resizable);//设置窗体是否可以改变大小
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置程序关闭动作
jFrame.setVisible(true);//显示窗体

}
}

{
publicCalculatorFrame(){
add(newCalculatorPanel());
MyFrameUitl.init(this,newDimension(400,300),"简易计算器",null,
false);
this.pack();//让组件按原始大小显示,并且窗体的大小刚好能够容纳所有的组件
}
}

publicclassCalculatorTest{
publicstaticvoidmain(String[]args){
newCalculatorFrame();
}
}

运行结果:

‘伍’ android模拟器上的自带软件的代码都在哪里 例如计算器 它的软件源代码在哪里

自带的Apk软件源代码放在 源码的packages/app/下 自带的ApiDemo程序源码放在development/samples/ApiDemos/下

‘陆’ 易安卓怎么制作万能计算器,发源码,谢谢

如果计算结果是打印在屏幕上的 就用cls 如果是在文本框中输出的话就直接输出一个空串就行 这样 “”

热点内容
scratch少儿编程课程 发布:2025-04-16 17:11:44 浏览:640
荣耀x10从哪里设置密码 发布:2025-04-16 17:11:43 浏览:368
java从入门到精通视频 发布:2025-04-16 17:11:43 浏览:85
php微信接口教程 发布:2025-04-16 17:07:30 浏览:310
android实现阴影 发布:2025-04-16 16:50:08 浏览:793
粉笔直播课缓存 发布:2025-04-16 16:31:21 浏览:345
机顶盒都有什么配置 发布:2025-04-16 16:24:37 浏览:213
编写手游反编译都需要学习什么 发布:2025-04-16 16:19:36 浏览:813
proteus编译文件位置 发布:2025-04-16 16:18:44 浏览:366
土压缩的本质 发布:2025-04-16 16:13:21 浏览:593