苹果手机游戏排名:(Java)谁有计算器的完整代码?

来源:百度文库 编辑:查人人中国名人网 时间:2024/05/08 20:48:26
有的发给我好么,谢谢

真是爽阿,两个要这个代码的
看看我写得把,还不错,楼主给我追加点分吧
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Calculator extends JFrame {

private Container container;
private GridBagLayout layout;
private GridBagConstraints constraints;
private JTextField displayField;//计算结果显示区
private String lastCommand;//保存+,-,*,/,=命令
private double result;//保存计算结果
private boolean start;//判断是否为数字的开始

public Calculator() {

super("Calculator");
container=getContentPane();
layout=new GridBagLayout();
container.setLayout(layout);
constraints=new GridBagConstraints();
start=true;
result=0;
lastCommand = "=";
displayField=new JTextField(20);
displayField.setHorizontalAlignment(JTextField.RIGHT);
constraints.gridx=0;
constraints.gridy=0;
constraints.gridwidth=4;
constraints.gridheight=1;
constraints.fill=GridBagConstraints.BOTH;
constraints.weightx=100;
constraints.weighty=100;
layout.setConstraints(displayField,constraints);
container.add(displayField);
ActionListener insert = new InsertAction();
ActionListener command = new CommandAction();
addButton("Backspace",0,1,2,1,insert);
addButton("CE",2,1,1,1,insert);
addButton("C",3,1,1,1,insert);
addButton("7",0,2,1,1,insert);
addButton("8",1,2,1,1,insert);
addButton("9",2,2,1,1,insert);
addButton("/",3,2,1,1,command);
addButton("4",0,3,1,1,insert);
addButton("5",1,3,1,1,insert);
addButton("6",2,3,1,1,insert);
addButton("*",3,3,1,1,command);
addButton("1",0,4,1,1,insert);
addButton("2",1,4,1,1,insert);
addButton("3",2,4,1,1,insert);
addButton("-",3,4,1,1,command);
addButton("0",0,5,1,1,insert);
addButton("+/-",1,5,1,1,insert);//只显示"-"号,"+"没有实用价值
addButton(".",2,5,1,1,insert);
addButton("+",3,5,1,1,command);
addButton("=",0,6,4,1,command);
setSize(300,300);
setVisible(true);

}

private void addButton(String label,int row,int column,int with,int height,ActionListener listener) {

JButton button=new JButton(label);
constraints.gridx=row;
constraints.gridy=column;
constraints.gridwidth=with;
constraints.gridheight=height;
constraints.fill=GridBagConstraints.BOTH;
button.addActionListener(listener);
layout.setConstraints(button,constraints);
container.add(button);
}

private class InsertAction implements ActionListener {

public void actionPerformed(ActionEvent event) {

String input=event.getActionCommand();
if (start) {
displayField.setText("");
start=false;
if(input.equals("+/-"))
displayField.setText(displayField.getText()+"-");
}

if(!input.equals("+/-")) {
if(input.equals("Backspace")) {
String str=displayField.getText();
if(str.length()>0)
displayField.setText(str.substring(0,str.length()-1));
}else if(input.equals("CE")||input.equals("C")) {
displayField.setText("0");
start=true;
}else
displayField.setText(displayField.getText()+input);
}
}
}

private class CommandAction implements ActionListener {

public void actionPerformed(ActionEvent evt) {
String command=evt.getActionCommand();
if(start) {
lastCommand=command;
}else {
calculate(Double.parseDouble(displayField.getText()));
lastCommand=command;
start=true;
}
}
}

public void calculate(double x) {
if (lastCommand.equals("+")) result+= x;
else if (lastCommand.equals("-")) result-=x;
else if (lastCommand.equals("*")) result*=x;
else if (lastCommand.equals("/")) result/=x;
else if (lastCommand.equals("=")) result=x;
displayField.setText(""+ result);
}

public static void main(String []args) {
Calculator calculator=new Calculator();
calculator.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

}

参考: http://gzdzw.51.net/htmjsq.htm

计算器代码:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>计算器</title>
<style type="text/css"></style>
</head>

<body bgcolor="#63D8D8" topmargin="0" leftmargin="0">
<form action name="Calculator" class="font">
<table border="1" cellpadding="2" bordercolorlight="#808080" bordercolordark="#FFFFFF" class="font">
<tr>
<td colspan="5" align="center" bgcolor="#A5C0D8"><input name="ReadOut" size="22" value="0" width="100%" class="font"></td>
</tr>
<tr>
<td align="center"><input name="btnSeven" onclick="Num(7)" type="button" value=" 7 " class="font"></td>
<td align="center"><input name="btnEight" onclick="Num(8)" type="button" value=" 8 " class="font"></td>
<td align="center"><input name="btnNine" onclick="Num(9)" type="button" value=" 9 " class="font"></td>
<td align="center"><input name="btnPlus" onclick="Operation('+')" type="button" value=" +(加) " class="font" style="color: #FF0000"></td>
<td align="center"><input name="btnClear" onclick="Clear()" type="button" value=" C " class="font" style="color: #FF0000"></td>
</tr>
<tr>
<td align="center"><input name="btnFour" onclick="Num(4)" type="button" value=" 4 " class="font"></td>
<td align="center"><input name="btnFive" onclick="Num(5)" type="button" value=" 5 " class="font"></td>
<td align="center"><input name="btnSix" onclick="Num(6)" type="button" value=" 6 " class="font"></td>
<td align="center"><input name="btnMinus" onclick="Operation('-')" type="button" value=" -(减) " class="font" style="color: #FF0000"></td>
<td align="center"><input name="btnClearEntry" onclick="ClearEntry()" type="button" value=" CE " class="font" style="color: #FF0000"></td>
</tr>
<tr>
<td align="center"><input name="btnOne" onclick="Num(1)" type="button" value=" 1 " class="font"></td>
<td align="center"><input name="btnTwo" onclick="Num(2)" type="button" value=" 2 " class="font"></td>
<td align="center"><input name="btnThree" onclick="Num(3)" type="button" value=" 3 " class="font"></td>
<td align="center"><input name="btnMultiply" onclick="Operation('*')" type="button" value=" *(乘) " class="font" style="color: #FF0000"></td>
<td align="center"><input name="btnPercent" onclick="Percent()" type="button" value=" % " class="font"></td>
</tr>
<tr>
<td align="center"><input name="btnZero" onclick="Num(0)" type="button" value=" 0 " class="font"></td>
<td align="center"><input name="btnNeg" onclick="Neg()" type="button" value=" +/- " class="font"></td>
<td align="center"><input name="btnDecimal" onclick="Decimal()" type="button" value=" . " class="font"></td>
<td align="center"><input name="btnDivide" onclick="Operation('/')" type="button" value=" /(除) " class="font" style="color: #FF0000"></td>
<td align="center"><input name="btnEquals" onclick="Operation('=')" type="button" value=" = " class="font" style="color: #FF0000"></td>
</tr>
</table>
</form>
<SCRIPT language=JavaScript>
var FKeyPad = document.Calculator;
var Accum = 0;
var FlagNewNum = false;
var PendingOp = "";
function Num (Num) {
if (FlagNewNum) {
FKeyPad.ReadOut.value = Num;
FlagNewNum = false;
}
else {
if (FKeyPad.ReadOut.value == "0")
FKeyPad.ReadOut.value = Num;
else
FKeyPad.ReadOut.value += Num;
}
}
function Operation (Op) {
var Readout = FKeyPad.ReadOut.value;
if (FlagNewNum && PendingOp != "=");
else
{
FlagNewNum = true;
if ( '+' == PendingOp )
Accum += parseFloat(Readout);
else if ( '-' == PendingOp )
Accum -= parseFloat(Readout);
else if ( '/' == PendingOp )
Accum /= parseFloat(Readout);
else if ( '*' == PendingOp )
Accum *= parseFloat(Readout);
else
Accum = parseFloat(Readout);
FKeyPad.ReadOut.value = Accum;
PendingOp = Op;
}
}
function Decimal () {
var curReadOut = FKeyPad.ReadOut.value;
if (FlagNewNum) {
curReadOut = "0.";
FlagNewNum = false;
}
else
{
if (curReadOut.indexOf(".") == -1)
curReadOut += ".";
}
FKeyPad.ReadOut.value = curReadOut;
}
function ClearEntry () {
FKeyPad.ReadOut.value = "0";
FlagNewNum = true;
}
function Clear () {
Accum = 0;
PendingOp = "";
ClearEntry();
}
function Neg () {
FKeyPad.ReadOut.value = parseFloat(FKeyPad.ReadOut.value) * -1;
}
function Percent () {
FKeyPad.ReadOut.value = (parseFloat(FKeyPad.ReadOut.value) / 100) * parseFloat(Accum);
}
function hexchange()
{
var hex,dec;
dec = parseFloat(FKeyPad.ReadOut.value);
hex = dec.toString(16);
window.alert("该十进制数转成十六进制数为:["+ hex +"]");
}
function octchange()
{
var oct,dec;
dec = parseFloat(FKeyPad.ReadOut.value);
oct = dec.toString(8);
window.alert("该十进制数转成八进制数为:["+ oct +"]");
}
function binchange()
{
var bin,dec;
dec = parseFloat(FKeyPad.ReadOut.value);
bin = dec.toString(2);
window.alert("该十进制数转成二进制数为:["+ bin +"]");
}
</SCRIPT>
</body>
</html>

-----------------------------
//主类

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
public class Counter{
Process process=new Process();
JTextField t1=new JTextField();
public Counter(){
JFrame f=new JFrame();

t1.setEditable(false);
t1.setBackground(Color.white);
JButton b1=new JButton(" 1 ");b1.setForeground(Color.blue);
JButton b2=new JButton(" 2 ");b2.setForeground(Color.blue);
JButton b3=new JButton(" 3 ");b3.setForeground(Color.blue);
JButton b4=new JButton(" + ");b4.setForeground(Color.red);
JButton b5=new JButton(" C ");b5.setForeground(Color.red);
JButton b6=new JButton(" 4 ");b6.setForeground(Color.blue);
JButton b7=new JButton(" 5 ");b7.setForeground(Color.blue);
JButton b8=new JButton(" 6 ");b8.setForeground(Color.blue);
JButton b9=new JButton(" - ");b9.setForeground(Color.red);
JButton b10=new JButton(" B ");b10.setForeground(Color.red);
JButton b11=new JButton(" 7 ");b11.setForeground(Color.blue);
JButton b12=new JButton(" 8 ");b12.setForeground(Color.blue);
JButton b13=new JButton(" 9 ");b13.setForeground(Color.blue);
JButton b14=new JButton(" * ");b14.setForeground(Color.red);
JButton b15=new JButton("1/x");b15.setForeground(Color.red);
JButton b16=new JButton(" 0 ");b16.setForeground(Color.blue);
JButton b17=new JButton("+/-");b17.setForeground(Color.red);
JButton b18=new JButton(" . ");b18.setForeground(Color.red);
JButton b19=new JButton(" / ");b19.setForeground(Color.red);
JButton b20=new JButton(" = ");b20.setForeground(Color.red);

Container c=f.getContentPane();
c.setLayout(new BorderLayout());

JPanel p=new JPanel(new GridLayout(4,1));
JPanel p1=new JPanel(new FlowLayout());
JPanel p2=new JPanel(new FlowLayout());
JPanel p3=new JPanel(new FlowLayout());
JPanel p4=new JPanel(new FlowLayout());

c.add(t1,BorderLayout.NORTH);
p1.add(b1);p1.add(b2);p1.add(b3);p1.add(b4);p1.add(b5);
p2.add(b6);p2.add(b7);p2.add(b8);p2.add(b9);p2.add(b10);
p3.add(b11);p3.add(b12);p3.add(b13);p3.add(b14);p3.add(b15);
p4.add(b16);p4.add(b17);p4.add(b18);p4.add(b19);p4.add(b20);
p.add(p1);
p.add(p2);
p.add(p3);
p.add(p4);
c.add(p,BorderLayout.CENTER);

f.setTitle("计算器");
f.setBounds(300,160,284,210);
f.setVisible(true);
f.setResizable(false);

//b1事件
b1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
process.input("1");
t1.setText(process.out());
}
});
//b2事件
b2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
process.input("2");
t1.setText(process.out());
}
});
//b3事件
b3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
process.input("3");
t1.setText(process.out());
}
});
//b6事件
b6.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
process.input("4");
t1.setText(process.out());
}
});
//b7事件
b7.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
process.input("5");
t1.setText(process.out());
}
});
//b8事件
b8.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
process.input("6");
t1.setText(process.out());
}
});
//b11事件
b11.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
process.input("7");
t1.setText(process.out());
}
});
//b12事件
b12.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
process.input("8");
t1.setText(process.out());
}
});
//b13事件
b13.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
process.input("9");
t1.setText(process.out());
}
});
//b16事件
b16.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
process.input("0");
t1.setText(process.out());
}
});
//b18事件
b18.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
process.input(".");
t1.setText(process.out());
}
});
//b5事件
b5.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
process.clear();
t1.setText("0");
}
});
//b10事件
b10.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
process.backSpace();
t1.setText(process.out());
}
});
//加号事件
b4.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
process.sign(1);
}
});
//减号事件
b9.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
process.sign(2);
}
});
//乘号事件
b14.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
process.sign(3);
}
});
//除号事件
b19.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
process.sign(4);
}
});
//+/-号事件
b17.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
process.setSign();
t1.setText(process.out());
}
});
//1/x号事件
b15.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
process.sign(5);
}
});
//等号事件
b20.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
t1.setText(process.sum());
}
});
}

public static void main(String args[]){
Counter cou=new Counter();
}
}

//Process类

class Process {
double temp1, temp2;
String tfsum1, tfsum2;
int mark, mark1;
//构造函数

Process() {
clear();
}

void clear() {
temp1 = temp2 = 0;
mark = mark1 = 0;
tfsum1 = tfsum2 = "0";
}

//接收数据

void input(String s) {
if (mark1 == -1) {
clear();
if (!s.equalsIgnoreCase(".")) {
tfsum1 = s;
}
else {
tfsum1 += s;
}
}
else if (mark != 0) {
if (tfsum2.equalsIgnoreCase("0") && !s.equalsIgnoreCase(".")) {
tfsum2 = s;
}
else if (!s.equalsIgnoreCase(".") ||
(tfsum2.indexOf(".") < 0 && s.equalsIgnoreCase("."))) {
tfsum2 += s;
}
mark1 = 2;
}
else {
if (tfsum1.equalsIgnoreCase("0") && !s.equalsIgnoreCase(".")) {
tfsum1 = s;
}
else if (!s.equalsIgnoreCase(".") ||
(tfsum1.indexOf(".") < 0 && s.equalsIgnoreCase("."))) {
tfsum1 += s;
}
mark1 = 1;
}
temp1 = Double.parseDouble(tfsum1);
temp2 = Double.parseDouble(tfsum2);
}
//////////////符号//////////////
void sign(int sign) {
if (mark1 != 0) {
mark = sign;
}
}
//输出计算结果
String sum() {
mark1 = -1;
switch (mark) {
default:
break;
case 1:
temp1 += temp2;
break;
case 2:
temp1 -= temp2;
break;
case 3:
temp1 *= temp2;
break;
case 4:
if (temp2 != 0) {
temp1 /= temp2;
}
else {
return "除数不能为零";
}
break;
case 5:
if(temp1!=0)
temp1=1/temp1;
else return "零不能取倒数";
break;
}
tfsum1 = Double.toString(temp1);
tfsum2 = Double.toString(temp2);
if (Double.isInfinite(temp1)) {
return "CE";
}
else {
return String.valueOf(temp1);
}
}

//输出当前操作数的字符串
String out() {
if (mark != 0 && mark1 >= 0) {
return tfsum2;
}
else {
return tfsum1;
}
}

//实现退格的功能

void backSpace() {
if (mark1 == 1) {
if (tfsum1.length() <= 1) {
tfsum1 = "0";
}
else {
tfsum1 = tfsum1.substring(0, tfsum1.length() - 1);
}
temp1 = Double.parseDouble(tfsum1);
}
if (mark1 == 2) {
if (tfsum2.length() <= 1) {
tfsum2 = "0";
}
else {
tfsum2 = tfsum2.substring(0, tfsum2.length() - 1);
}
temp2 = Double.parseDouble(tfsum2);
}
}

//取反

void setSign() {
switch (mark1) {
case -1:
case 1:
if (tfsum1.startsWith("-")) {
tfsum1 = tfsum1.substring(1, tfsum1.length());
}
else {
tfsum1 = "-" + tfsum1;
}
break;
case 2:
if (tfsum2.startsWith("-")) {
tfsum2 = tfsum2.substring(1, tfsum2.length());
}
else {
tfsum2 = "-" + tfsum1;
}
break;
default:
break;
}
temp1 = Double.parseDouble(tfsum1);
temp2 = Double.parseDouble(tfsum2);
}
}