真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網站制作重慶分公司

計算器java代碼編寫 簡單計算器編程代碼java

如何用JAVA語言編寫計算器小程序?

具體代碼如下:

創(chuàng)新互聯(lián)企業(yè)建站,10余年網站建設經驗,專注于網站建設技術,精于網頁設計,有多年建站和網站代運營經驗,設計師為客戶打造網絡企業(yè)風格,提供周到的建站售前咨詢和貼心的售后服務。對于成都做網站、成都網站設計中不同領域進行深入了解和探索,創(chuàng)新互聯(lián)在網站建設中充分了解客戶行業(yè)的需求,以靈動的思維在網頁中充分展現(xiàn),通過對客戶行業(yè)精準市場調研,為客戶提供的解決方案。

import javax.swing.*;

import java.awt.event.*;

import java.awt.*;

public class Calculator ?extends JFrame implements ActionListener ?{

private JFrame jf;

private JButton[] allButtons;

private JButton clearButton;

private JTextField jtf;

public Calculator() {

//對圖形組件實例化

jf=new JFrame("任靜的計算器1.0:JAVA版");

jf.addWindowListener(new WindowAdapter(){

public void windowClosing(){

System.exit(0);

}

});

allButtons=new JButton[16];

clearButton=new JButton("清除");

jtf=new JTextField(25);

jtf.setEditable(false);

String str="123+456-789*0.=/";

for(int i=0;iallButtons.length;i++){

allButtons[i]=new JButton(str.substring(i,i+1));

}

}

public void init(){

//完成布局

jf.setLayout(new BorderLayout());

JPanel northPanel=new JPanel();

JPanel centerPanel=new JPanel();

JPanel southPanel=new JPanel();

northPanel.setLayout(new FlowLayout());

centerPanel.setLayout(new GridLayout(4,4));

southPanel.setLayout(new FlowLayout());

northPanel.add(jtf);

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

centerPanel.add(allButtons[i]);

}

southPanel.add(clearButton);

jf.add(northPanel,BorderLayout.NORTH);

jf.add(centerPanel,BorderLayout.CENTER);

jf.add(southPanel,BorderLayout.SOUTH);

addEventHandler();

}

//添加事件監(jiān)聽

public void addEventHandler(){

jtf.addActionListener(this);

for(int i=0;iallButtons.length;i++){

allButtons[i].addActionListener(this);

}

clearButton.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e) {

// TODO Auto-generated method stub

Calculator.this.jtf.setText("");

}

});

}

//事件處理

public void actionPerformed(ActionEvent e) {

//在這里完成事件處理 ?使計算器可以運行

String action=e.getActionCommand();

if(action=="+"||action=="-"||action=="*"||action=="/"){

}

}

public void setFontAndColor(){

Font f=new Font("宋體",Font.BOLD,24);

jtf.setFont(f);

jtf.setBackground(new Color(0x8f,0xa0,0xfb));

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

allButtons[i].setFont(f);

allButtons[i].setForeground(Color.RED);

}

}

public void showMe(){

init();

setFontAndColor();

jf.pack();

jf.setVisible(true);

jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

public static void main(String[] args){

new Calculator().showMe();

}

}

用JAVA編寫一個計算器

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();?//?定義按鈕面板

//?當點擊按鈕+、-、*、/時,com?=?true

boolean?com?=?false;

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

int?i?=?0;

//?存放text的內容

String?text?=?"";

//?存放點擊按鈕+、-、*、/之前的數值

double?defbutton?=?0;

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

int?symbol?=?0;

//?構造函數

Jisuanqi()?{

super("WangJiao");?//?設定標題

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

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

pane.setLayout(new?BorderLayout());

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

//?替換圖標

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)?{

/*

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

?*?false;或者是否點擊數字鍵,如果是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中的數值=0就要判斷在這個數值中是否有.存在?如果有那么就在原來數值基礎之上添?加0;否則保持原來的數值不變

?*/

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

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)就是類型轉換了,下面都是一樣

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;

}

/*

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

?*/

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);

}

}

}?//?獲得點擊+之前的數值

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

com?=?true;

i?=?0;

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

symbol?=?1;

}?//?獲得點擊-之前的數值

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

com?=?true;

i?=?0;

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

symbol?=?2;

}?//?獲得點擊*之前的數值

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

com?=?true;

i?=?0;

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

System.out.println(defbutton);

symbol?=?3;

}?//?獲得點擊/之前的數值

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:?{?//?計算加法

double?ad?=?defbutton

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

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

i?=?0;

text?=?"";

break;

}

case?2:?{?//?計算減法

double?ad?=?defbutton

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

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

i?=?0;

text?=?"";

break;

}

case?3:?{?//?計算乘法

double?ad?=?defbutton

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

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

i?=?0;

text?=?"";

break;

}

case?4:?{?//?計算除法

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();

}

}

//?計算器數字按鈕定義面板

class?Number_Key?extends?JPanel?{

/**

?*

?*/

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

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

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

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

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

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

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

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

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

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

JButton?nine?=?new?JButton("9");?//?數字鍵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));?//?定義布局管理器為網格布局

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

//?添加按鈕

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);

}

}

//?計算器顯示結果的窗體

class?Result?extends?JPanel?{

/**

?*

?*/

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

//?text先是輸入和結果

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

@SuppressWarnings("deprecation")

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

text.setHorizontalAlignment(SwingConstants.RIGHT);

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

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

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

}

}

直接復制?保存成Jisuanqi?.java可以直接運行了

怎么用JAVA編程編寫一個計算器?

打開IED:打開自己java編程的軟件,采用的是eclipse軟件。

建立java工程。

編寫類。

編寫類的詳細步驟

1.類的基本結構:

由于這里用到了界面,所以要進行窗口界面的編程,按鈕事件的處理,和計算處理界面;

package MyCaculator;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class MyCaculator extends JFrame {

private int add=1,sub=2,mul=3,div=4;

private int op=0;

boolean ifOp;

private String output="0";

private Button[] jba=new Button[]{new Button("7"),new Button("8"),

new Button("9"),new Button("+"),

new Button("4"),new Button("5"),new Button("6"),new Button("-"),

new Button("1"),new Button("2"),new Button("3"),new Button("*"),

new Button("0"),new Button("."),new Button("="),new Button("/")};

private JPanel jpt=new JPanel();

private JPanel jpb=new JPanel();

private JTextField jtf=new JTextField("");

private MyCaculator(){

}

private void operate(String x){

}

public String add(String x){

return output;

}

public String subtract(String x){

return output;

}

public String multiply(String x){

return output;

}

public String divide(String x){

return output;

}

public String Equals(String x){

return output;

}

public void opClean(){

}

class setOperate_Act implements ActionListener{

public void actionPerformed(ActionEvent e) {

}

}

class setLabel_Act implements ActionListener{

public void actionPerformed(ActionEvent e) {

}

}

public static void main(String[] args) {

}

}

2.建立構造方法:

所謂構造方法就是,對自己的主類進行初始化,代碼如下:

private MyCaculator(){

jpt.setLayout(new BorderLayout());

jpt.add(jtf);

this.add(jpt,BorderLayout.NORTH);

jpb.setLayout(new GridLayout(4,4));

for(int i=0;ijba.length;i++){

jpb.add(jba[i]);

if(i==3||i==7||i==11||i==15||i==14)

jba[i].addActionListener(new setOperate_Act());

else

jba[i].addActionListener(new setLabel_Act());

}

this.add(jpb,BorderLayout.CENTER);

this.setSize(250, 200);

this.setResizable(false);

this.setVisible(true);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

3.建立數據計算方法

這里的數據計算方法有6個,一個是主方法其他幾個是加減乘除的處理方法,代碼如下:

private void operate(String x){

double x1=Double.valueOf(x);

double y=Double.valueOf(output);

switch(op){

case 0:output=x;break;

case 1:output=String.valueOf(y+x1);break;

case 2:output =String.valueOf(y-x1);break;

case 3:output =String.valueOf(y*x1);break;

case 4:

if(x1!=0) output=String.valueOf(y/x1);

else output="不能為0";

break;

}

}

public String add(String x){

operate(x);

op=add;

return output;

}

public String subtract(String x){

operate(x);

op=sub;

return output;

}

public String multiply(String x){

operate(x);

op=mul;

return output;

}

public String divide(String x){

operate(x);

op=div;

return output;

}

public String Equals(String x){

operate(x);

op=0;

return output;

}

public void opClean(){

op=0;

output ="0";

}

4.事件處理方法

這里的時間處理方法,沒有建立一個整體的方法,二是在為了便于處理的方法,將按鈕事件分成兩個部分,并采用兩個子類來實現(xiàn),這兩個類時內部類要寫在主類內部的,代碼如下:

class setOperate_Act implements ActionListener{

public void actionPerformed(ActionEvent e) {

if(e.getSource()==jba[3]){

jtf.setText(add(jtf.getText()));

ifOp=true;

}

else if(e.getSource()==jba[7]){

jtf.setText(subtract(jtf.getText()));

ifOp=true;

}

else if(e.getSource()==jba[11]){

jtf.setText(multiply(jtf.getText()));

ifOp=true;

}

else if(e.getSource()==jba[15]){

jtf.setText(divide(jtf.getText()));

ifOp=true;

}

else if(e.getSource()==jba[14]){

jtf.setText(Equals(jtf.getText()));

ifOp=true;

}

}

}

class setLabel_Act implements ActionListener{

public void actionPerformed(ActionEvent e) {

Button tempb=(Button)e.getSource();

if(ifOp){

jtf.setText(tempb.getLabel());

ifOp=false;

}else {

jtf.setText(jtf.getText()+tempb.getLabel());

}

}

}

5.建立main方法:

要想實現(xiàn)我們的代碼,我們需在main方法中,實例化我們的對象。

public static void main(String[] args) {

new MyCaculator();

}

編寫java程序簡單計算器

主要涉及的知識點: 類的寫法, 以及方法的調用 .建議多做練習. 如果有看不懂的地方. 可以繼續(xù)追問,一起討論.

參考代碼如下

//Number類

class?Number?{

private?int?n1;//私有的整型數據成員n1

private?int?n2;//私有的整型數據成員n2

//?通過構造函數給n1和n2賦值

public?Number(int?n1,?int?n2)?{

this.n1?=?n1;

this.n2?=?n2;

}

//?加法

public?int?addition()?{

return?n1?+?n2;

}

//?減法

public?int?subtration()?{

return?n1?-?n2;

}

//?乘法

public?int?multiplication()?{

return?n1?*?n2;

}

//?除法?(可能除不盡,所以使用double作為返回類型)

public?double?division()?{

return?n1?*?1.0?/?n2;?//?通過n1*1.0?把計算結果轉換成double類型.

}

}

//Exam4?類

public?class?Exam4{

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

Number?number=new?Number(15,?6);//創(chuàng)建Number類的對象

//下面的是調用方法得到返回值進行輸出顯示

System.out.println("加法"+number.addition());

System.out.println("減法"+number.subtration());

System.out.println("乘法"+number.multiplication());

System.out.println("除法"+number.division());

}

}

java編一個計算器的代碼

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

代碼如下:

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("計算器");

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("計算器");

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);

}

}

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

才子_輝祝您愉快!


當前標題:計算器java代碼編寫 簡單計算器編程代碼java
分享鏈接:http://weahome.cn/article/hihepd.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部