java設(shè)計(jì)計(jì)算器代碼 java編寫計(jì)算器代碼

急求?。?!如何用java程序代碼實(shí)現(xiàn)計(jì)算器界面

package?jisuanqi_new;

為增城等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計(jì)制作服務(wù),及增城網(wǎng)站建設(shè)行業(yè)解決方案。主營業(yè)務(wù)為成都網(wǎng)站建設(shè)、成都網(wǎng)站設(shè)計(jì)、增城網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會得到認(rèn)可,從而選擇與我們長期合作。這樣,我們也可以走得更遠(yuǎn)!

import?java.awt.*;

import?java.awt.event.*;

public?class?JiSuanQi_new?implements?ActionListener

{

Panel?p1;//聲明面板p1

TextField?t1;//聲明文本行t1

String[]?label?=?{"7","8","9","/","4","5","6","*","1","2","3","-","0",".","=","+"};//聲明標(biāo)簽數(shù)組label1存放按鈕上的標(biāo)簽

Button[]?b;//聲明按鈕數(shù)組存放16個按鈕

private?int?i;//聲明i以備后用

private?String?op1?=?"0";//運(yùn)算數(shù)備用

private?String?operator?=?"+";//運(yùn)算符備用

private?boolean?append?=?false;//備用

public?JiSuanQi_new()//構(gòu)造方法

{

t1=new?TextField();//初始化文本行t1

b?=?new?Button[label.length];//初始化按鈕數(shù)組b

p1=new?Panel();//初始化面板p1

p1.setLayout(new?GridLayout(4,4,4,4));//使面板選擇網(wǎng)格布局管理器以備儲存16個按鈕(4行4列)

for(int?i=0;ib.length;i++)//利用for循環(huán)把標(biāo)簽放在按鈕上,使每個按鈕添加事件監(jiān)聽器,在面板p1上添加上16個按鈕

{

b[i]?=?new?Button(label[i]);//把標(biāo)簽依次放在16個按鈕上

b[i].addActionListener(this);//使每個按鈕添加動作事件監(jiān)聽器

p1.add(b[i]);?//分別將按鈕添加到面板p1上

}

Frame?f=new?Frame("計(jì)算器1.0");//初始化窗口f,起名字計(jì)算器1.0

f.setLayout(new?BorderLayout());//為窗口選擇邊界布局管理器

f.add(BorderLayout.NORTH,t1);//把文本行他添加到窗口的北部

f.add(BorderLayout.CENTER,p1);//把面吧p1添加到窗口的中間

f.addWindowListener(new?WindowAdapter(){//給窗口f添加窗口事件監(jiān)聽器

public?void?windowClosing(WindowEvent?eve){//運(yùn)行窗口關(guān)閉方法

System.exit(0);//退出程序

}

});

f.setSize(250,?250);//設(shè)置窗口大小

f.setLocation(200,200);

f.setVisible(true);//顯示窗口

}

public?static?void?main(String?args[])

{

new?JiSuanQi_new();?//調(diào)用構(gòu)造方法

}

public?void?actionPerformed(ActionEvent?ae)

{//按鈕被操作發(fā)生

String?comm?=?ae.getActionCommand();//返回與此動作相關(guān)的命令字符串,即:使用者第一次點(diǎn)擊的按鈕是什么。

if("0123456789".indexOf(comm)!=-1)//如果相關(guān)命令字符串為0~9之間的數(shù)字則執(zhí)行

{

if(append){

String?temp?=?t1.getText();//新數(shù)字

t1.setText(temp+comm);

}else{?????????????????????????//因?yàn)榇藭rappend為false執(zhí)行這個

t1.setText(comm);?//將文本行t1設(shè)置為相關(guān)命令字符串(你按中的按鈕代碼)

append?=??true;//此時append=true若繼續(xù)按按鈕若繼續(xù)按數(shù)字的話1.第一次的按話不會改變2.非第一次按得話會覆蓋之前按得數(shù)字(即缺點(diǎn):只能進(jìn)行單個數(shù)字的計(jì)算)

}

}

else?if(("+-*/".indexOf(comm)!=-1))//如果相關(guān)命令字符串為+-*/之間的數(shù)字則執(zhí)行

{

//保存

//t1.setText(comm);

op1?=?t1.getText();//把第一個數(shù)賦值給op1

operator?=?comm;//把命令字符串賦值給operator

append?=?false;//此時append為false即若繼續(xù)按按鈕若按數(shù)字的話將重復(fù)上面的動作,按符號的話將覆蓋之前的符號

}

else?if("=".equals(comm))//如果按的是=號,則按條件進(jìn)行下面的運(yùn)算

{

String?op2?=?t1.getText();//op2第二個數(shù)

double?d1?=?Double.parseDouble(op1);

double?d2?=?Double.parseDouble(op2);

if(operator.equals("+")){

d1?=?d1?+?d2?;

}else?if(operator.equals("-")){

d1?=?d1?-?d2;

}else?if(operator.equals("*")){

d1?=?d1?*?d2;

}else?{

d1?=?d1?/?d2;

}

t1.setText(d1+"");//顯示計(jì)算結(jié)果

append?=?false;

}

else?if(".".equals(comm))//若是.號繼續(xù)按

{

String?temp?=?t1.getText();

if(temp.indexOf(".")==-1){

t1.setText(temp+".");

append?=?true;

}

}

}

}

用JAVA編寫一個計(jì)算器

import?java.awt.BorderLayout;

import?java.awt.Color;

import?java.awt.GridLayout;

import?java.awt.event.ActionEvent;

import?java.awt.event.ActionListener;

import?javax.swing.ImageIcon;

import?javax.swing.JButton;

import?javax.swing.JFrame;

import?javax.swing.JPanel;

import?javax.swing.JTextField;

import?javax.swing.SwingConstants;

public?class?Jisuanqi?extends?JFrame?implements?ActionListener?{

/**

?*

?*/

private?static?final?long?serialVersionUID?=?1L;

Result?result?=?new?Result();?//?定義text的面板

Number_Key?number_key?=?new?Number_Key();?//?定義按鈕面板

//?當(dāng)點(diǎn)擊按鈕+、-、*、/時,com?=?true

boolean?com?=?false;

//?當(dāng)i=0時說明是我們第一次輸入,字符串text不會累加

int?i?=?0;

//?存放text的內(nèi)容

String?text?=?"";

//?存放點(diǎn)擊按鈕+、-、*、/之前的數(shù)值

double?defbutton?=?0;

//?+、-、*、/的代號分別為1,2,3,4

int?symbol?=?0;

//?構(gòu)造函數(shù)

Jisuanqi()?{

super("WangJiao");?//?設(shè)定標(biāo)題

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);?//?設(shè)定關(guān)閉窗體時退出程序

JPanel?pane?=?new?JPanel();?//?定義主面板

pane.setLayout(new?BorderLayout());

setBounds(380,?220,?30,?80);?//?前兩個參數(shù)是在屏幕上顯示的坐標(biāo),后兩個是大小

//?替換圖標(biāo)

ImageIcon?icon?=?new?ImageIcon("F:1.GIF");

//?Jisuanqi.class.getResource("APPLE.GIF")

//?);

setIconImage(icon.getImage());

pane.add(result,?BorderLayout.NORTH);

pane.add(number_key,?BorderLayout.CENTER);

pane.add(number_key.equal,?BorderLayout.SOUTH);

number_key.one.addActionListener(this);?//?對1按鈕添加監(jiān)聽事件

number_key.two.addActionListener(this);?//?對2按鈕添加監(jiān)聽事件

number_key.three.addActionListener(this);?//?對3按鈕添加監(jiān)聽事件

number_key.four.addActionListener(this);?//?對4按鈕添加監(jiān)聽事件

number_key.five.addActionListener(this);?//?對5按鈕添加監(jiān)聽事件

number_key.six.addActionListener(this);?//?對6按鈕添加監(jiān)聽事件

number_key.seven.addActionListener(this);?//?對7按鈕添加監(jiān)聽事件

number_key.eight.addActionListener(this);?//?對8按鈕添加監(jiān)聽事件

number_key.nine.addActionListener(this);?//?對9按鈕添加監(jiān)聽事件

number_key.zero.addActionListener(this);?//?對0按鈕添加監(jiān)聽事件

number_key.ce.addActionListener(this);?//?對置零按鈕添加監(jiān)聽事件

number_key.plus.addActionListener(this);?//?對+按鈕添加監(jiān)聽事件

number_key.equal.addActionListener(this);?//?對=按鈕添加監(jiān)聽事件

number_key.sub.addActionListener(this);?//?對-按鈕添加監(jiān)聽事件

number_key.mul.addActionListener(this);?//?對*按鈕添加監(jiān)聽事件

number_key.div.addActionListener(this);?//?對/按鈕添加監(jiān)聽事件

number_key.point.addActionListener(this);?//?對.按鈕添加監(jiān)聽事件

setContentPane(pane);

pack();?//?初始化窗體大小為正好盛放所有按鈕

}

//?各個按鈕觸發(fā)的事件

public?void?actionPerformed(ActionEvent?e)?{

/*

?*?如果是點(diǎn)擊數(shù)字按鈕那么先要判斷是否在此之前點(diǎn)擊了+、-、*、/、=,如果是那么com=true?如果沒有com=

?*?false;或者是否點(diǎn)擊數(shù)字鍵,如果是i?=?1,如果沒有?i?=?0;

?*/

if?(e.getSource()?==?number_key.one)?{

if?(com?||?i?==?0)?{

result.text.setText("1");

com?=?false;

i?=?1;

}?else?{

text?=?result.text.getText();

result.text.setText(text?+?"1");

}

}?else?if?(e.getSource()?==?number_key.two)?{

if?(com?||?i?==?0)?{

result.text.setText("2");

com?=?false;

i?=?1;

}?else?{

text?=?result.text.getText();

result.text.setText(text?+?"2");

}

}?else?if?(e.getSource()?==?number_key.three)?{

if?(com?||?i?==?0)?{

result.text.setText("3");

com?=?false;

i?=?1;

}?else?{

text?=?result.text.getText();

result.text.setText(text?+?"3");

}

}?else?if?(e.getSource()?==?number_key.four)?{

if?(com?||?i?==?0)?{

result.text.setText("4");

com?=?false;

i?=?1;

}?else?{

text?=?result.text.getText();

result.text.setText(text?+?"4");

}

}?else?if?(e.getSource()?==?number_key.five)?{

if?(com?||?i?==?0)?{

result.text.setText("5");

com?=?false;

i?=?1;

}?else?{

text?=?result.text.getText();

result.text.setText(text?+?"5");

}

}?else?if?(e.getSource()?==?number_key.six)?{

if?(com?||?i?==?0)?{

result.text.setText("6");

com?=?false;

i?=?1;

}?else?{

text?=?result.text.getText();

result.text.setText(text?+?"6");

}

}?else?if?(e.getSource()?==?number_key.seven)?{

if?(com?||?i?==?0)?{

result.text.setText("7");

com?=?false;

i?=?1;

}?else?{

text?=?result.text.getText();

result.text.setText(text?+?"7");

}

}?else?if?(e.getSource()?==?number_key.eight)?{

if?(com?||?i?==?0)?{

result.text.setText("8");

com?=?false;

i?=?1;

}?else?{

text?=?result.text.getText();

result.text.setText(text?+?"8");

}

}?else?if?(e.getSource()?==?number_key.nine)?{

if?(com?||?i?==?0)?{

result.text.setText("9");

com?=?false;

i?=?1;

}?else?{

text?=?result.text.getText();

result.text.setText(text?+?"9");

}

}

/*

?*?對于0這個按鈕有一定的說法,在我的程序里不會出現(xiàn)如00000這樣的情況,我加了判斷條件就是

?*?如果text中的數(shù)值=0就要判斷在這個數(shù)值中是否有.存在?如果有那么就在原來數(shù)值基礎(chǔ)之上添?加0;否則保持原來的數(shù)值不變

?*/

else?if?(e.getSource()?==?number_key.zero)?{?//?result.text.getText()是得到text里內(nèi)容的意思

if?(com?||?i?==?0)?{

result.text.setText("0");

com?=?false;

i?=?1;

}?else?{

text?=?result.text.getText();

if?(Float.parseFloat(text)??0?||?Float.parseFloat(text)??0)?{?//?Float.parseFloat(text)就是類型轉(zhuǎn)換了,下面都是一樣

result.text.setText(text?+?"0");

}?else?{

if?(text.trim().indexOf(".")?==?-1)?{

result.text.setText(text);

}?else?{

result.text.setText(text?+?"0");

}

}

}

}?else?if?(e.getSource()?==?number_key.ce)?{

result.text.setText("0");

i?=?0;

com?=?true;

//?text?=?"";

defbutton?=?0;

}

/*

?*?本程序不會讓一個數(shù)值中出現(xiàn)2個以上的小數(shù)點(diǎn).具體做法是:判斷是否已經(jīng)存在.存在就不添加,?不存在就添加.

?*/

else?if?(e.getSource()?==?number_key.point)?{

if?(com?||?i?==?0)?{

result.text.setText("0.");

com?=?false;

i?=?1;

}?else?{

text?=?result.text.getText();

if?(text.trim().indexOf(".")?==?-1)?{

result.text.setText(text?+?".");

}?else?{

result.text.setText(text);

}

}

}?//?獲得點(diǎn)擊+之前的數(shù)值

else?if?(e.getSource()?==?number_key.plus)?{

com?=?true;

i?=?0;

defbutton?=?Double.parseDouble(result.text.getText());

symbol?=?1;

}?//?獲得點(diǎn)擊-之前的數(shù)值

else?if?(e.getSource()?==?number_key.sub)?{

com?=?true;

i?=?0;

defbutton?=?Double.parseDouble(result.text.getText());

symbol?=?2;

}?//?獲得點(diǎn)擊*之前的數(shù)值

else?if?(e.getSource()?==?number_key.mul)?{

com?=?true;

i?=?0;

defbutton?=?Double.parseDouble(result.text.getText());

System.out.println(defbutton);

symbol?=?3;

}?//?獲得點(diǎn)擊/之前的數(shù)值

else?if?(e.getSource()?==?number_key.div)?{

com?=?true;

i?=?0;

defbutton?=?Double.parseDouble(result.text.getText());

symbol?=?4;

}?else?if?(e.getSource()?==?number_key.equal)?{

switch?(symbol)?{

case?1:?{?//?計(jì)算加法

double?ad?=?defbutton

+?Double.parseDouble(result.text.getText());

result.text.setText(ad?+?"");

i?=?0;

text?=?"";

break;

}

case?2:?{?//?計(jì)算減法

double?ad?=?defbutton

-?Double.parseDouble(result.text.getText());

result.text.setText(String.valueOf(ad));

i?=?0;

text?=?"";

break;

}

case?3:?{?//?計(jì)算乘法

double?ad?=?defbutton

*?Double.parseDouble(result.text.getText());

result.text.setText(ad?+?"");

i?=?0;

text?=?"";

break;

}

case?4:?{?//?計(jì)算除法

double?ad?=?defbutton

/?Double.parseDouble(result.text.getText());

result.text.setText(ad?+?"");

i?=?0;

text?=?"";

break;

}

}

System.out.println(com);

}

System.out.println(result.text.getText());

}

@SuppressWarnings("deprecation")

public?static?void?main(String[]?args)?{

Jisuanqi?loveyou?=?new?Jisuanqi();

loveyou.show();

}

}

//?計(jì)算器數(shù)字按鈕定義面板

class?Number_Key?extends?JPanel?{

/**

?*

?*/

private?static?final?long?serialVersionUID?=?1L;

JButton?zero?=?new?JButton("0");?//?數(shù)字鍵0

JButton?one?=?new?JButton("1");?//?數(shù)字鍵1

JButton?two?=?new?JButton("2");?//?數(shù)字鍵2

JButton?three?=?new?JButton("3");?//?數(shù)字鍵3

JButton?four?=?new?JButton("4");?//?數(shù)字鍵4

JButton?five?=?new?JButton("5");?//?數(shù)字鍵5

JButton?six?=?new?JButton("6");?//?數(shù)字鍵6

JButton?seven?=?new?JButton("7");?//?數(shù)字鍵7

JButton?eight?=?new?JButton("8");?//?數(shù)字鍵8

JButton?nine?=?new?JButton("9");?//?數(shù)字鍵9

JButton?plus?=?new?JButton("+");

JButton?sub?=?new?JButton("-");

JButton?mul?=?new?JButton("*");

JButton?div?=?new?JButton("/");

JButton?equal?=?new?JButton("=");

JButton?ce?=?new?JButton("清零");?//?置零鍵

JButton?point?=?new?JButton(".");

Number_Key()?{

setLayout(new?GridLayout(4,?4,?1,?1));?//?定義布局管理器為網(wǎng)格布局

setBackground(Color.blue);?//?設(shè)置背景顏色

//?添加按鈕

add(one);

add(two);

add(three);

add(four);

add(five);

add(six);

add(seven);

add(eight);

add(nine);

add(zero);

add(plus);

add(sub);

add(mul);

add(div);

add(point);

add(equal);

add(ce);

}

}

//?計(jì)算器顯示結(jié)果的窗體

class?Result?extends?JPanel?{

/**

?*

?*/

private?static?final?long?serialVersionUID?=?1L;

//?text先是輸入和結(jié)果

JTextField?text?=?new?JTextField("0");

@SuppressWarnings("deprecation")

Result()?{?//?講輸入的數(shù)字或得到的結(jié)果在text的右邊顯示

text.setHorizontalAlignment(SwingConstants.RIGHT);

text.enable(false);?//?文本框不能編輯

setLayout(new?BorderLayout());?//?設(shè)定布局管理器邊框布局

add(text,?BorderLayout.CENTER);?//?text放置在窗體的中間

}

}

直接復(fù)制?保存成Jisuanqi?.java可以直接運(yùn)行了

java編一個計(jì)算器的代碼

界面漂亮堪比系統(tǒng)自帶計(jì)算器,功能完美加減乘除開平方等等全部具備,還有清零按鈕,小數(shù)點(diǎn)的使用,連加連乘功能完全參考系統(tǒng)官方計(jì)算器經(jīng)過長期調(diào)試改進(jìn)而成,馬上拷貝代碼拿去試試看吧,絕不后悔!

代碼如下:

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.util.*;

public class Counter {

public static void main(String[] args) {

CounterFrame frame = new CounterFrame();

frame.show();

}

}

class CounterFrame extends JFrame {

public CounterFrame() {

JMenuBar menuBar = new JMenuBar();

JMenu menuFile = new JMenu();

JMenu menuFile1 = new JMenu();

JMenu menuFile2 = new JMenu();

JMenu menuFile3 = new JMenu();

JMenuItem menuFileExit = new JMenuItem();

menuFile.setText("文件");

menuFile1.setText("編輯");

menuFile2.setText("查看");

menuFile3.setText("幫助");

menuFileExit.setText("退出");

menuFileExit.addActionListener

(

new ActionListener() {

public void actionPerformed(ActionEvent e) {

CounterFrame.this.windowClosed();

}

}

);

menuFile.add(menuFileExit);

menuBar.add(menuFile);

menuBar.add(menuFile1);

menuBar.add(menuFile2);

menuBar.add(menuFile3);

setTitle("計(jì)算器");

setJMenuBar(menuBar);

setSize(new Dimension(400, 280));

this.getContentPane().add(new Allpanel());

this.addWindowListener

(

new WindowAdapter() {

public void windowClosing(WindowEvent e) {

CounterFrame.this.windowClosed();

}

}

);

}

protected void windowClosed() {

System.exit(0);

}

}

class Tool {

public static Tool instance;

private JTextField field;

private Tool() {

this.field=new JTextField(30);

this.field.setHorizontalAlignment(JTextField.RIGHT);

}

public static Tool getinstance()

{

if(instance==null)

{

instance=new Tool();

}

return instance;

}

public JTextField getfield()

{

return (this.field);

}

}

class Allpanel extends JPanel {

public Allpanel() {

this.setLayout(new BorderLayout(0,7));

Northpanel np=new Northpanel();

Centerpanel cp=new Centerpanel();

this.add(np,BorderLayout.NORTH);

this.add(cp,BorderLayout.CENTER);

}

}

class Centercenter extends JPanel {

static Vector Vec=new Vector();

static Vector vc=new Vector();

static Vector vc1=new Vector();

static Vector vc2=new Vector();

static Vector vc3=new Vector();

static String begin="yes";

static double add;

static double jq;

static double cs;

static double cq;

static double dy;

static String jg;

static String what;

static double tool=0;

static String to="yes";

/**

* Method Centercenter

*

*

*/

public Centercenter() {

// TODO: Add your code here

final JTextField text=Tool.getinstance().getfield();

this.setLayout(new GridLayout(4,5,3,3));

String arg[] ={"7","8","9","/","sqrt","4","5","6","*","%","1","2","3","-","1/x","0","+/-",".","+","="};

for(int i=0;i20;i++)

{

final JButton b=new JButton(arg[i]);

//this.add(new JButton(arg[i]));

this.add(b);

if(i==0||i==1||i==2||i==5||i==6||i==7||i==10||i==11||i==12||i==15)

{

b.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

String mark=b.getText();

String ma=text.getText();

if(vc3.contains("v3"))

{

text.setText("0."+mark);

vc3.clear();

}

else if(vc.contains("a"))

{

if(vc2.contains("v2"))

{

text.setText("0."+mark);

vc.clear();

vc2.clear();

}

else

{

text.setText(mark);

vc.clear();

Vec.clear();

Vec.add(mark);

}

}

else

{

text.setText(ma.trim()+mark);

Vec.add(mark);

}

begin="no";

to="yes";

}

});

}

if(i==17)

{

b.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

String mar=b.getText();

String m=text.getText();

if("yes".equals(begin))

{

vc3.add("v3");

}

if(vc1.contains("v1"))

{

vc2.add("v2");

vc1.clear();

}

if(!Vec.contains(".")!vc.contains("a"))

{

text.setText(m.trim()+mar);

Vec.add(".");

}

}

});

}

if(i==18)

{

b.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

String ma=text.getText();

add=Double.parseDouble(ma);

if(what==null)

{

tool=add;

what="add";

}

else

{

tool=tool+add;

text.setText(String.valueOf((tool)));

}

vc.add("a");

vc1.add("v1");

to="+";

}

});

}

if(i==13)

{

b.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

String ma=text.getText();

jq=Double.parseDouble(ma);

if(what==null)

{

tool=jq;

what="jq";

}

else

{

tool=tool-jq;

text.setText(String.valueOf((tool)));

}

vc.add("a");

vc1.add("v1");

to="-";

}

});

}

if(i==3)

{

b.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

String ma=text.getText();

cq=Double.parseDouble(ma);

if(what==null)

{

tool=cq;

what="cq";

}

else

{

tool=tool/cq;

text.setText(String.valueOf((tool)));

}

vc.add("a");

vc1.add("v1");

to="/";

}

});

}

if(i==4)

{

b.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

String ma=text.getText();

cq=Double.parseDouble(ma);

text.setText(String.valueOf(Math.sqrt(cq)));

}

});

}

if(i==8)

{

b.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

String ma=text.getText();

cs=Double.parseDouble(ma);

if(what==null)

{

tool=cs;

what="cs";

}

else

{

tool=tool*cs;

text.setText(String.valueOf((tool)));

}

vc.add("a");

vc1.add("v1");

to="*";

}

});

}

if(i==19)

{

b.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

String ma=text.getText();

dy=Double.parseDouble(ma);

if(what=="add")

{

jg=String.valueOf((tool+dy));

}

if(what=="jq")

{

jg=String.valueOf((tool-dy));

}

if(what=="cs")

{

jg=String.valueOf((tool*dy));

}

if(what=="cq")

{

jg=String.valueOf((tool/dy));

}

if(what==null)

{

if(to=="+")

{

tool=add;

jg=String.valueOf(tool+dy);

}

else if(to=="-")

{

tool=jq;

jg=String.valueOf(dy-tool);

}

else if(to=="*")

{

tool=cs;

jg=String.valueOf(dy*tool);

}

else if(to=="/")

{

tool=cq;

jg=String.valueOf(dy/tool);

}

else

{

jg=String.valueOf(dy);

}

}

text.setText(jg);

Vec.clear();

Vec.add(".");

vc.add("a");

vc1.add("v1");

what=null;

tool=0;

}

});

}

}

}

}

class Centernorth extends JPanel {

public Centernorth() {

final JTextField text=Tool.getinstance().getfield();

JButton jb1=new JButton("Backspace");

JButton jb2=new JButton(" CE ");

JButton jb3=new JButton(" C ");

this.add(jb1);

this.add(jb2);

this.add(jb3);

jb1.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e)

{

String back=Tool.getinstance().getfield().getText();

text.setText(backmethod(back));

Centercenter.Vec.remove(Centercenter.Vec.size()-1);

}

});

jb3.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e)

{

text.setText("0.");

Centercenter.Vec.clear();

Centercenter.Vec.add(".");

Centercenter.vc.add("a");

Centercenter.begin="yes";

Centercenter.vc1.clear();

Centercenter.what=null;

Centercenter.tool=0;

}

});

}

public String backmethod(String str)

{

return str.substring(0,str.length()-1);

}

}

class Centerpanel extends JPanel {

public Centerpanel() {

this.setLayout(new BorderLayout(8,7));

Centernorth cn=new Centernorth();

Centercenter cc=new Centercenter();

Centerwest cw=new Centerwest();

this.add(cn,BorderLayout.NORTH);

this.add(cc,BorderLayout.CENTER);

this.add(cw,BorderLayout.WEST);

}

}

class Centerwest extends JPanel {

public Centerwest() {

this.setLayout(new GridLayout(4,1,3,3));

this.add(new JButton("MC"));

this.add(new JButton("MR"));

this.add(new JButton("MS"));

this.add(new JButton("M+"));

}

}

class Northpanel extends JPanel {

private JTextField tf;

public Northpanel() {

tf=Tool.getinstance().getfield();

this.add(tf);

}

}

---------------------------------------------------------------------------

=============《按你要求特意后改過的最簡單功能的代碼如下》========================

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.util.*;

public class Counter2 {

public static void main(String[] args) {

CounterFrame frame = new CounterFrame();

frame.show();

}

}

class CounterFrame extends JFrame {

public CounterFrame() {

setTitle("計(jì)算器");

setSize(new Dimension(400, 280));

this.getContentPane().add(new Allpanel());

this.addWindowListener

(

new WindowAdapter() {

public void windowClosing(WindowEvent e) {

CounterFrame.this.windowClosed();

}

}

);

}

protected void windowClosed() {

System.exit(0);

}

}

class Tool {

public static Tool instance;

private JTextField field;

private Tool() {

this.field=new JTextField(30);

this.field.setHorizontalAlignment(JTextField.RIGHT);

}

public static Tool getinstance()

{

if(instance==null)

{

instance=new Tool();

}

return instance;

}

public JTextField getfield()

{

return (this.field);

}

}

class Allpanel extends JPanel {

public Allpanel() {

this.setLayout(new BorderLayout(0,7));

Northpanel np=new Northpanel();

Centerpanel cp=new Centerpanel();

this.add(np,BorderLayout.NORTH);

this.add(cp,BorderLayout.CENTER);

}

}

class Centercenter extends JPanel {

static Vector Vec=new Vector();

static Vector vc=new Vector();

static Vector vc1=new Vector();

static Vector vc2=new Vector();

static Vector vc3=new Vector();

static String begin="yes";

static double add;

static double jq;

static double cs;

static double cq;

static double dy;

static String jg;

static String what;

static double tool=0;

static String to="yes";

/**

* Method Centercenter

*

*

*/

public Centercenter() {

// TODO: Add your code here

final JTextField text=Tool.getinstance().getfield();

this.setLayout(new GridLayout(4,5,3,3));

String arg[] ={"7","8","9","/","4","5","6","*","1","2","3","-","0","=",".","+"};

for(int i=0;i16;i++)

{

final JButton b=new JButton(arg[i]);

//this.add(new JButton(arg[i]));

this.add(b);

if(i==0||i==1||i==2||i==4||i==5||i==6||i==8||i==9||i==10||i==12)

{

b.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

String mark=b.getText();

String ma=text.getText();

if(vc3.contains("v3"))

{

text.setText("0."+mark);

vc3.clear();

}

else if(vc.contains("a"))

{

if(vc2.contains("v2"))

{

text.setText("0."+mark);

vc.clear();

vc2.clear();

}

else

{

text.setText(mark);

vc.clear();

Vec.clear();

Vec.add(mark);

}

}

else

{

text.setText(ma.trim()+mark);

Vec.add(mark);

}

begin="no";

to="yes";

}

});

}

if(i==14)

{

b.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

String mar=b.getText();

String m=text.getText();

if("yes".equals(begin))

{

vc3.add("v3");

}

if(vc1.contains("v1"))

{

vc2.add("v2");

vc1.clear();

}

if(!Vec.contains(".")!vc.contains("a"))

{

text.setText(m.trim()+mar);

Vec.add(".");

}

}

});

}

if(i==15)

{

b.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

String ma=text.getText();

add=Double.parseDouble(ma);

if(what==null)

{

tool=add;

what="add";

}

else

{

tool=tool+add;

text.setText(String.valueOf((tool)));

}

vc.add("a");

vc1.add("v1");

to="+";

}

});

}

if(i==11)

{

b.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

String ma=text.getText();

jq=Double.parseDouble(ma);

if(what==null)

{

tool=jq;

what="jq";

}

else

{

tool=tool-jq;

text.setText(String.valueOf((tool)));

}

vc.add("a");

vc1.add("v1");

to="-";

}

});

}

if(i==3)

{

b.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

String ma=text.getText();

cq=Double.parseDouble(ma);

if(what==null)

{

tool=cq;

what="cq";

}

else

{

tool=tool/cq;

text.setText(String.valueOf((tool)));

}

vc.add("a");

vc1.add("v1");

to="/";

}

});

}

if(i==7)

{

b.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

String ma=text.getText();

cs=Double.parseDouble(ma);

if(what==null)

{

tool=cs;

what="cs";

}

else

{

tool=tool*cs;

text.setText(String.valueOf((tool)));

}

vc.add("a");

vc1.add("v1");

to="*";

}

});

}

if(i==13)

{

b.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

String ma=text.getText();

dy=Double.parseDouble(ma);

if(what=="add")

{

jg=String.valueOf((tool+dy));

}

if(what=="jq")

{

jg=String.valueOf((tool-dy));

}

if(what=="cs")

{

jg=String.valueOf((tool*dy));

}

if(what=="cq")

{

jg=String.valueOf((tool/dy));

}

if(what==null)

{

if(to=="+")

{

tool=add;

jg=String.valueOf(tool+dy);

}

else if(to=="-")

{

tool=jq;

jg=String.valueOf(dy-tool);

}

else if(to=="*")

{

tool=cs;

jg=String.valueOf(dy*tool);

}

else if(to=="/")

{

tool=cq;

jg=String.valueOf(dy/tool);

}

else

{

jg=String.valueOf(dy);

}

}

text.setText(jg);

Vec.clear();

Vec.add(".");

vc.add("a");

vc1.add("v1");

what=null;

tool=0;

}

});

}

}

}

}

class Centernorth extends JPanel {

public Centernorth() {

final JTextField text=Tool.getinstance().getfield();

}

}

class Centerpanel extends JPanel {

public Centerpanel() {

this.setLayout(new BorderLayout(8,7));

Centernorth cn=new Centernorth();

Centercenter cc=new Centercenter();

Centerwest cw=new Centerwest();

this.add(cn,BorderLayout.NORTH);

this.add(cc,BorderLayout.CENTER);

this.add(cw,BorderLayout.WEST);

}

}

class Centerwest extends JPanel {

public Centerwest() {

}

}

class Northpanel extends JPanel {

private JTextField tf;

public Northpanel() {

tf=Tool.getinstance().getfield();

this.add(tf);

}

}

------------------------------------------------------------

才子_輝祝您愉快!

網(wǎng)站題目:java設(shè)計(jì)計(jì)算器代碼 java編寫計(jì)算器代碼
網(wǎng)站網(wǎng)址:http://muchs.cn/article42/ddihohc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內(nèi)鏈、定制網(wǎng)站、面包屑導(dǎo)航、網(wǎng)站導(dǎo)航關(guān)鍵詞優(yōu)化

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

微信小程序開發(fā)