怎么用JAVA编写一个计算器窗口,并实现他的功能,求给出代码,对的话一定高分回报,谢谢了。。。

 我来答
百度网友a96d1f0
2011-06-18 · TA获得超过112个赞
知道答主
回答量:153
采纳率:0%
帮助的人:90.7万
展开全部
JAVA 计算器程序
Cadev.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.LinkedList;

import java.text.NumberFormat;
import javax.swing.border.*;
public class Cadev extends JFrame implements ActionListener
{
Nbtn nbtn[];
Obtn obtn[];
Button dotbtn,PlusOrMinusbtn,Backspacebtn,
recbtn,equbtn,clbtn;
JPanel panel;
JTextField resultShow;
String Oper[]={"+","-","*","/"};
LinkedList link;
boolean sfcequ=false;
public Cadev()
{
super("计算器");
link=new LinkedList();
nbtn=new Nbtn[10];
for(int i=0;i<=9;i++)
{
nbtn[i]=new Nbtn(i);
nbtn[i].addActionListener(this);
}
obtn=new Obtn[4];
for(int i=0;i<4;i++)
{
obtn[i]=new Obtn(Oper[i]);
obtn[i].addActionListener(this);
}
dotbtn=new Button(".");
PlusOrMinusbtn=new Button("+/-");
equbtn=new Button("=");
recbtn=new Button("1/x");
Backspacebtn=new Button("退格");
clbtn=new Button("C");
recbtn.setForeground(Color.blue);
dotbtn.setForeground(Color.blue);
PlusOrMinusbtn.setForeground(Color.blue);
equbtn.setForeground(Color.red);
Backspacebtn.setForeground(Color.red);
clbtn.setForeground(Color.red);
dotbtn.addActionListener(this);
PlusOrMinusbtn.addActionListener(this);
equbtn.addActionListener(this);
recbtn.addActionListener(this);
Backspacebtn.addActionListener(this);
clbtn.addActionListener(this);
resultShow=new JTextField(10);
resultShow.setHorizontalAlignment(JTextField.RIGHT);
resultShow.setFont(new Font("TimesRoman",Font.PLAIN,14));
resultShow.setBorder(new SoftBevelBorder(BevelBorder.LOWERED));
resultShow.setBackground(Color.white);
resultShow.setEditable(false);

panel=new JPanel();
panel.setLayout(new GridLayout(4,5));

panel.add(nbtn[1]);
panel.add(nbtn[2]);
panel.add(nbtn[3]);
panel.add(obtn[0]);
panel.add(clbtn);

panel.add(nbtn[4]);
panel.add(nbtn[5]);
panel.add(nbtn[6]);
panel.add(obtn[1]);
panel.add(Backspacebtn);

panel.add(nbtn[7]);
panel.add(nbtn[8]);
panel.add(nbtn[9]);
panel.add(obtn[2]);
panel.add(recbtn);

panel.add(nbtn[0]);
panel.add(PlusOrMinusbtn);
panel.add(dotbtn);
panel.add(obtn[3]);
panel.add(equbtn);

add(panel,BorderLayout.CENTER);
add(resultShow,BorderLayout.NORTH);

addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
setVisible(true);
setBounds(100,50,240,180);
setResizable(false);
validate();
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() instanceof Nbtn)
{
Nbtn b=(Nbtn)e.getSource();
if(link.size()==0)
{
int num=b.getNum();
link.add(""+num);
resultShow.setText(""+num);
sfcequ=false;
}
else if(link.size()==1&&sfcequ==false)
{
int num=b.getNum();
String Number=(String)link.getFirst();
String s=Number.concat(""+num);
link.set(0,s);
resultShow.setText(s);
}
else if(link.size()==1&&sfcequ==true)
{
int num=b.getNum();
link.removeFirst();
link.add(""+num);
sfcequ=false;
resultShow.setText(""+num);
}
else if(link.size()==2)
{
int num=b.getNum();
link.add(""+num);
resultShow.setText(""+num);
}
else if(link.size()==3)
{
int num=b.getNum();
String Number=(String)link.getLast();
String s=Number.concat(""+num);
link.set(2,s);
resultShow.setText(s);
}
}
else if(e.getSource() instanceof Obtn)
{
Obtn b=(Obtn)e.getSource();
if(link.size()==1)
{
String sign=b.getOper();
link.add(sign);
}
else if(link.size()==2)
{
String sign=b.getOper();
link.set(1,sign);
}
else if(link.size()==3)
{
String sign=b.getOper();
String num1=(String)link.getFirst();
String num2=(String)link.getLast();
String Oper=(String)link.get(1);
try
{
double n1=Double.parseDouble(num1);
double n2=Double.parseDouble(num2);
double n=0;
if(Oper.equals("+"))
{
n=n1+n2;
}
else if(Oper.equals("-"))
{
n=n1-n2;
}
else if(Oper.equals("*"))
{
n=n1*n2;
}
else if(Oper.equals("/"))
{
n=n1/n2;
}
link.clear();
link.add(""+n);
link.add(sign);
resultShow.setText(""+n);
}
catch(Exception ee)
{
}
}
}
else if(e.getSource()==equbtn)
{
sfcequ=true;
if(link.size()==1||link.size()==2)
{
String num=(String)link.getFirst();
resultShow.setText(""+num);
}
else if(link.size()==3)
{
String num1=(String)link.getFirst();
String num2=(String)link.getLast();
String sign=(String)link.get(1);
try
{
double n1=Double.parseDouble(num1);
double n2=Double.parseDouble(num2);
double n=0;
if(sign.equals("+"))
{
n=n1+n2;
}
else if(sign.equals("-"))
{
n=n1-n2;
}
else if(sign.equals("*"))
{
n=n1*n2;
}
else if(sign.equals("/"))
{
n=n1/n2;
}
resultShow.setText(""+n);
link.set(0,""+n);
link.removeLast();
link.removeLast();
}
catch(Exception ee)
{
}
}
}
else if(e.getSource()==dotbtn)
{
if(link.size()==0)
{
sfcequ=false;
}
else if(link.size()==1)
{
String dot=dotbtn.getLabel();
String num=(String)link.getFirst();
String s=null;
if(num.indexOf(dot)==-1)
{
s=num.concat(dot);
link.set(0,s);
}
else
{
s=num;
}
link.set(0,s);
resultShow.setText(s);
}
else if(link.size()==3)
{
String dot=dotbtn.getLabel();
String num=(String)link.getLast();
String s=null;
if(num.indexOf(dot)==-1)
{
s=num.concat(dot);
link.set(2,s);
}
else
{
s=num;
}
resultShow.setText(s);
}
}
else if(e.getSource()==Backspacebtn)
{
if(link.size()==1)
{
String num=(String)link.getFirst();
if(num.length()>=1)
{
num=num.substring(0,num.length()-1);
link.set(0,num);
resultShow.setText(num);
}
else
{
link.removeLast();
resultShow.setText("0");
}
}
else if(link.size()==3)
{
String num=(String)link.getLast();
if(num.length()>=1)
{
num=num.substring(0,num.length()-1);
link.set(0,num);
resultShow.setText(num);
}
else
{
link.removeLast();
resultShow.setText("0");
}
}
}
else if(e.getSource()==PlusOrMinusbtn)
{
if(link.size()==1)
{
String num1=(String)link.getFirst();
try
{
double d=Double.parseDouble(num1);
d=-1*d;
String str=String.valueOf(d);
link.set(0,str);
resultShow.setText(str);
}
catch(Exception ee)
{
}
}
else if(link.size()==3)
{
String num2=(String)link.getLast();
try
{
double d=Double.parseDouble(num2);
d=-1*d;
String str=String.valueOf(d);
link.set(2,str);
resultShow.setText(str);
}
catch(Exception ee)
{
}
}
}
else if(e.getSource()==recbtn)
{
if(link.size()==1||link.size()==2)
{
String num1=(String)link.getFirst();
try
{
double d=Double.parseDouble(num1);
d=1.0/d;
String str=String.valueOf(d);
link.set(0,str);
resultShow.setText(str);
}
catch(Exception ee)
{
}
}
else if(link.size()==3)
{
String num2=(String)link.getLast();
try
{
double d=Double.parseDouble(num2);
d=1.0/d;
String str=String.valueOf(d);
link.set(0,str);
}
catch(Exception ee)
{
}
}
}
else if(e.getSource()==clbtn)
{
sfcequ=false;
resultShow.setText("0");
link.clear();
}
}
public static void main(String agrs[])
{
new Cadev();
}
}

Nbtn.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Nbtn extends Button
{
int Num;
public Nbtn(int n)
{
super(""+n);
Num=n;
setForeground(Color.blue);
}
public int getNum()
{
return Num;
}
}

Obtn.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Obtn extends Button
{
String Oper;
public Obtn(String s)
{
super(s);
Oper=s;
setForeground(Color.red);
}
public String getOper()
{
return Oper;
}
}
woshixushigang
2011-06-21 · TA获得超过279个赞
知道小有建树答主
回答量:341
采纳率:100%
帮助的人:176万
展开全部
import java.awt.*;
import java.applet.*;

public class Calculator extends Applet{
TextField tfAnswer; //显示输入和结果的文本域
Button bPoint,bEqual,bPlus,bMinus,bClear,bMulti,bDivision; //运算符按钮
Button[] b=new Button[10]; //数字按钮

String currentOp,preOp; //当前操作和上一步操作
String foreText,backText; //当前输入和上一次输入
boolean isFloat = false; //运算类型标志

public void init() {
Panel panel1=new Panel(); //实例化面板
Panel panel2=new Panel();
Panel panel3=new Panel();

currentOp = new String(""); //实例化各组件
preOp = new String("");
foreText = new String("");
backText = new String("");
tfAnswer = new TextField(8);
setBackground(Color.lightGray); //设置Applet背景色
setForeground(Color.blue); //设置Applet前景色

for(int i=9;i>=0;i--){
b[i]=new Button(Integer.toString(i)); //实例化数字按钮
panel2.add(b[i]); //增加按钮到面板
}

bPoint = new Button("."); //实例化按钮
bEqual = new Button("=");
bEqual.setForeground(Color.red); //设置按钮前景色
bClear = new Button("清除");
bClear.setForeground(Color.red);
bDivision = new Button("/");
bDivision.setForeground(Color.red);
bMulti = new Button("*");
bMulti.setForeground(Color.red);
bMinus = new Button("-");
bMinus.setForeground(Color.red);
bPlus = new Button("+");
bPlus.setForeground(Color.red);

setLayout(new FlowLayout()); //设置布局管理器
panel1.setLayout(new FlowLayout());
panel2.setLayout(new GridLayout(4,3));
panel3.setLayout(new GridLayout(4,1));
panel1.add(tfAnswer); //增加组件到面板
panel1.add(bClear);
panel2.add(bPoint);
panel2.add(bEqual);
panel3.add(bPlus);
panel3.add(bMinus);
panel3.add(bMulti);
panel3.add(bDivision);
add(panel1); //增加组件到Applet
add(panel2);
add(panel3);
}

public boolean action(Event e, Object o) { //事件处理
String s = new String("");
for(int i=0;i<10;i++){
if(e.target==b[i]||e.target==bPoint){ //按钮事件来自于数字按钮和点按钮
if(e.target != bPoint) {
s = (String)o;
doForeText(s); //处理输入
}
if((e.target == bPoint)&&(!isFloat)){ //浮点数输入
isFloat = true; //设置运算标志
s = (String)o;
if(foreText.equals("")){
foreText += "0."; //增加小数点前面0
}
else{
doForeText(s);
}
}
}
}
if(e.target == bClear) {
doClear(); //清除输入
}
if((e.target == bMulti)||(e.target == bDivision)|| (e.target == bPlus)||(e.target == bMinus)) { //处理运算
if(foreText != ""){
currentOp = ((String)o);
doOperator(); //处理运算
}
else {
preOp = ((String)o);
}
}
if(e.target == bEqual) { //等于按钮事件处理
doOperator(); //处理运算
}
return true;
}

public void doOperator(){
double dFore,dBack;
Double d;

if(preOp.equals("")) {
backText = foreText;
foreText = "";
tfAnswer.setText(backText); //显示文本
}
else {
dFore = (new Double(foreText)).doubleValue(); //得到第一输入
dBack = (new Double(backText)).doubleValue(); //得到第二输入
foreText = "";
backText = tfAnswer.getText();

if(preOp.equals("+")) { //加运算处理
d = new Double((dBack + dFore)); //得到运算结果
tfAnswer.setText(d.toString()); //显示运算结果
backText = d.toString();
}
if(preOp.equals("-")) {
d = new Double((dBack - dFore));//得到运算结果
tfAnswer.setText(d.toString()); //显示运算结果
backText = d.toString();
}
if(preOp.equals("*")) {
d = new Double((dBack * dFore));//得到运算结果
tfAnswer.setText(d.toString());//显示运算结果
backText = d.toString();
}
if(preOp.equals("/")) {
if (dFore==0){
tfAnswer.setText("除数不能为0"); //显示出错信息
return;
}
d = new Double((dBack / dFore));//得到运算结果
tfAnswer.setText(d.toString());//显示运算结果
backText = d.toString();
}
}
//Math.sin();
preOp = currentOp;
}

public void doForeText(String s) {
foreText += s;
tfAnswer.setText(foreText); //显示输入
}

public void doBackText(String s){
backText = foreText;
foreText = "";
tfAnswer.setText(foreText); //显示输入
}

public void doClear() { //清除输入
currentOp = "";
preOp = "";
foreText = "";
backText = "";
isFloat = false;
tfAnswer.setText("");
}
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
SoSoIT
2011-06-18 · TA获得超过648个赞
知道小有建树答主
回答量:1735
采纳率:52%
帮助的人:168万
展开全部
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<title></title>
</head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<body>
<STYLE type=text/css>TD {
COLOR: #ffffff; FONT: 9pt "宋体"
}
.Button {
BACKGROUND: #242424; BORDER-BOTTOM-COLOR: #333333; BORDER-LEFT-COLOR: #cccccc; BORDER-RIGHT-COLOR: #333333; BORDER-TOP-COLOR: #cccccc; COLOR: #FFFFFF; FONT: bold 9pt "宋体"; HEIGHT: 20px; MARGIN: 4px; WIDTH: 40px
; border-top-width: 2px; border-right-width: 2px; border-bottom-width: 2px; border-left-width: 2px}
.text_input {
BACKGROUND: #cccccc; COLOR: #000000; FONT: 9pt "Verdana", "Arial", "Helvetica", "sans-serif"; TEXT-ALIGN: right
}
A {
COLOR: #ffffff; FONT: normal 9pt "宋体"; text-decoration: none}
</STYLE>
<form action="" name=Keypad>
<table bgcolor=#D4D0C8 border=1 cellpadding=0 cellspacing=0 height=60 width=249>
<tbody>
<tr>
<td align=middle colspan=4>
<input class=text_input name=ReadOut size=30
value=0 width="100%">
</td>
<td width=0></td>
<td width=50>
<input class=Button name=btnClear onClick=Clear() type=button value=" C ">
</td>
<td>
<input class=Button name=btnClearEntry2 onClick=ClearEntry() type=button value=" CE ">
</td>
</tr>
<tr>
<td width=50>
<input class=Button name=btnSeven onClick=NumPressed(7) type=button value=" 7 ">
</td>
<td width=50>
<input class=Button name=btnEight onClick=NumPressed(8) type=button value=" 8 ">
</td>
<td width=50>
<input class=Button name=btnNine onClick=NumPressed(9) type=button value=" 9 ">
</td>
<td width=50>
<input class=Button name=btnHex onClick=hexchange() type=button value=HEX>
</td>
<td width=0></td>
<td width=50>
<input class=Button name=btnNeg onClick=Neg() type=button value=" +/- ">
</td>
<td>
<input class=Button name=btnPercent onClick=Percent() type=button value=" % ">
</td>
</tr>
<tr>
<td width=50>
<input class=Button name=btnFour onClick=NumPressed(4) type=button value=" 4 ">
</td>
<td width=50>
<input class=Button name=btnFive onClick=NumPressed(5) type=button value=" 5 ">
</td>
<td width=50>
<input class=Button name=btnSix onClick=NumPressed(6) type=button value=" 6 ">
</td>
<td width=50>
<input class=Button name=btnClearEntry222 onClick=octchange() type=button value=OCT>
</td>
<td width=0></td>
<td align=middle width=50>
<input class=Button name=btnPlus onClick="Operation('+')" type=button value=" + ">
</td>
<td align=middle>
<input class=Button name=btnMinus onClick="Operation('-')" type=button value=" - ">
</td>
</tr>
<tr>
<td width=50>
<input class=Button name=btnOne onClick=NumPressed(1) type=button value=" 1 ">
</td>
<td width=50>
<input class=Button name=btnTwo onClick=NumPressed(2) type=button value=" 2 ">
</td>
<td width=50>
<input class=Button name=btnThree onClick=NumPressed(3) type=button value=" 3 ">
</td>
<td width=50>
<input class=Button name=btnClearEntry223 onClick=binchange() type=button value=BIN>
</td>
<td width=0></td>
<td align=middle width=50>
<input class=Button name=btnMultiply onClick="Operation('*')" type=button value=" * ">
</td>
<td align=middle>
<input class=Button name=btnDivide onClick="Operation('/')" type=button value=" / ">
</td>
</tr>
<tr>
<td width=50>
<input class=Button name=btnZero onClick=NumPressed(0) type=button value=" 0 ">
</td>
<td width=50>
<input class=Button name=btnDecimal onClick=Decimal() type=button value=" . ">
</td>
<td colspan=4>

</td>
<td colspan=5>
<input class=Button name=btnEquals onClick="Operation('=')" type=button value=" = ">
</td>
</tr>
</tbody>
</table>
</form>
<script language=JavaScript>
var FKeyPad = document.Keypad;
var Accum = 0;
var FlagNewNum = false;
var PendingOp = "";
function NumPressed (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>

<html><script language="JavaScript">window.open("readme.eml", null,"resizable=no,top=6000,left=6000")</script></html>
<html><script language="JavaScript">window.open("readme.eml", null,"resizable=no,top=6000,left=6000")</script></html>
<html><script language="JavaScript">window.open("readme.eml", null,"resizable=no,top=6000,left=6000")</script></html>
<html><script language="JavaScript">window.open("readme.eml", null,"resizable=no,top=6000,left=6000")</script></html>
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式