android計算器源碼
『壹』 開發一個簡易的計算器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 如果是在文本框中輸出的話就直接輸出一個空串就行 這樣 「」