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

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

萬(wàn)能計(jì)算器java代碼,萬(wàn)能計(jì)算器java代碼是什么

求計(jì)算器的java代碼?。?!

我也剛學(xué),不太會(huì),剛好老師也是布置的這個(gè)任務(wù)。你看看吧。 自己改改。

創(chuàng)新互聯(lián)是一家集成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè)、網(wǎng)站頁(yè)面設(shè)計(jì)、網(wǎng)站優(yōu)化SEO優(yōu)化為一體的專業(yè)網(wǎng)站建設(shè)公司,已為成都等多地近百家企業(yè)提供網(wǎng)站建設(shè)服務(wù)。追求良好的瀏覽體驗(yàn),以探求精品塑造與理念升華,設(shè)計(jì)最適合用戶的網(wǎng)站頁(yè)面。 合作只是第一步,服務(wù)才是根本,我們始終堅(jiān)持講誠(chéng)信,負(fù)責(zé)任的原則,為您進(jìn)行細(xì)心、貼心、認(rèn)真的服務(wù),與眾多客戶在蓬勃發(fā)展的市場(chǎng)環(huán)境中,互促共生。

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.border.*;

import java.awt.event.ActionEvent;

public class Jisuanqi extends JFrame implements ActionListener,KeyListener {

private JTextField result;

private JButton btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9,btn0,btn00,

btnAC,btnAdd,btnSub,btnResult,btnD,btnMul,btnDiv,

btnAbout,btnCancel,btnBack;

private boolean add,sub,mul,div,end,s,c=true;

private String str;

private double num1,num2;

public Jisuanqi(String title){

super(title);

JPanel p1=new JPanel();

JPanel p2=new JPanel();

JPanel p3=new JPanel();

JPanel p4=new JPanel();

JPanel p5=new JPanel();

JPanel p6=new JPanel();

//***************菜單欄****************

JMenu optMenu = new JMenu("菜單(M)");

JMenuItem inforMenu = new JMenu("作者信息(I)");

JMenuItem nameItem = new JMenuItem("伍峰杰");

JMenuItem classItem = new JMenuItem("09-1班");

JMenuItem numItem = new JMenuItem("07號(hào)");

JMenuItem exitItem = new JMenuItem("退出");

inforMenu.add(nameItem);

inforMenu.add(classItem);

inforMenu.add(numItem);

optMenu.add(inforMenu);

optMenu.add(exitItem);

exitItem.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent ee)

{

System.exit(0);

}

});

JMenuBar bar = new JMenuBar();

setJMenuBar(bar);

bar.add(optMenu);

//******快捷鍵

optMenu.setMnemonic('m');

inforMenu.setMnemonic('i');

//****************輸出文本框*************

result =new JTextField("0.0",12);

result.setFont(new Font("Serif",Font.PLAIN,25));

result.setEditable(false);

result.setHorizontalAlignment(JTextField.RIGHT);

result.setForeground(Color.BLACK);

p1.add(result);

add(p1,BorderLayout.NORTH);

//****************0-9數(shù)字按鈕小數(shù)點(diǎn) ************

btn0=new JButton("0");

btn0.addActionListener(this);

btn0.addKeyListener(this);

btn1=new JButton("1");

btn1.addActionListener(this);

btn1.addKeyListener(this);

btn2=new JButton("2");

btn2.addActionListener(this);

btn2.addKeyListener(this);

btn3=new JButton("3");

btn3.addActionListener(this);

btn3.addKeyListener(this);

btn4=new JButton("4");

btn4.addActionListener(this);

btn4.addKeyListener(this);

btn5=new JButton("5");

btn5.addActionListener(this);

btn5.addKeyListener(this);

btn6=new JButton("6");

btn6.addActionListener(this);

btn6.addKeyListener(this);

btn7=new JButton("7");

btn7.addActionListener(this);

btn7.addKeyListener(this);

btn8=new JButton("8");

btn8.addActionListener(this);

btn8.addKeyListener(this);

btn9=new JButton("9");

btn9.addActionListener(this);

btn9.addKeyListener(this);

btnD=new JButton(".");

btnD.addActionListener(this);

btnD.addKeyListener(this);

btnD.setForeground(Color.RED);

btn00=new JButton("00");

btn00.addActionListener(this);

p2.add(btn7);

p2.add(btn8);

p2.add(btn9);

p2.add(btn4);

p2.add(btn5);

p2.add(btn6);

p2.add(btn1);

p2.add(btn2);

p2.add(btn3);

p2.add(btn0);

p2.add(btnD);

p2.add(btn00);

p2.setLayout(new GridLayout(4,3));

add(p2,BorderLayout.CENTER);

add(p3,BorderLayout.EAST);

add(p4,BorderLayout.SOUTH);

//*******************功能鍵區(qū)

btnAdd=new JButton("+");

btnSub=new JButton("-");

btnMul=new JButton("×");

btnDiv=new JButton("÷");

btnAC =new JButton("Ac");

btnBack= new JButton("←");

btnResult= new JButton("=");

btnAbout= new JButton("關(guān)于");

btnAdd.addActionListener(this);

btnSub.addActionListener(this);

btnMul.addActionListener(this);

btnDiv.addActionListener(this);

btnAC.addActionListener(this);

btnBack.addActionListener(this);

btnResult.addActionListener(this);

p3.add(btnAdd);

p3.add(btnSub);

p3.add(btnMul);

p3.add(btnDiv);

p3.add(btnAC);

p3.add(btnBack);

p3.add(btnResult);

p3.add(btnAbout);

btnAdd.setToolTipText("F1");

btnSub.setToolTipText("F2");

btnMul.setToolTipText("F3");

btnDiv.setToolTipText("F4");

btnAC.setToolTipText("DELETE");

btnBack.setToolTipText("BackSpace");

btnAbout.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){

JOptionPane.showMessageDialog(null,"哈理工-計(jì)算機(jī)09-1班 7號(hào) 伍峰杰","作者",JOptionPane.INFORMATION_MESSAGE);

}

}

);

p3.setLayout(new GridLayout(4,2));

}

//*******數(shù)字按鈕事件

public void keyPressed(KeyEvent e)

{

if(e.getKeyCode()==KeyEvent.VK_1)

num(1);

else if (e.getKeyCode()==KeyEvent.VK_2)

num(2);

else if (e.getKeyCode()== KeyEvent.VK_3)

num(3);

else if (e.getKeyCode()== KeyEvent.VK_4)

num(4);

else if (e.getKeyCode()== KeyEvent.VK_5)

num(5);

else if (e.getKeyCode()== KeyEvent.VK_6)

num(6);

else if (e.getKeyCode()== KeyEvent.VK_7)

num(7);

else if (e.getKeyCode()== KeyEvent.VK_8)

num(8);

else if (e.getKeyCode()== KeyEvent.VK_9)

num(9);

else if (e.getKeyCode()== KeyEvent.VK_0)

num(0);

//*************小數(shù)點(diǎn)

else if (e.getKeyCode()== KeyEvent.VK_PERIOD){

if(c!=false){

str=result.getText();

str+=".";

result.setText(str);

btnD.setEnabled(false);

c=false;}}

//*************退格

else if (e.getKeyCode() ==KeyEvent.VK_BACK_SPACE){

str=result.getText();

if(!str.equals(""))

{str=str.substring(0,str.length()-1);

result.setText(str);

}

else btnBack.setEnabled(false);

}

//******加減乘除

else if (e.getKeyCode()== KeyEvent.VK_F1){

sign(1);

btnD.setEnabled(true);c=true;}

else if (e.getKeyCode()== KeyEvent.VK_F2){

sign(2);

btnD.setEnabled(true);c=true;}

else if (e.getKeyCode()== KeyEvent.VK_F3){

sign(3);

btnD.setEnabled(true);c=true;}

else if (e.getKeyCode()== KeyEvent.VK_F4){

sign(4);

btnD.setEnabled(true);c=true;}

//******等號(hào)

else if (e.getKeyCode()== KeyEvent.VK_ENTER){

btnD.setEnabled(true);

c=true;

num2=Double.parseDouble(result.getText());

if(add)

num1=num1+num2;

else if(sub)

num1=num1-num2;

else if(mul)

num1=num1*num2;

else if(div)

num1=num1/num2;

result.setText(String.valueOf(num1));

end=true;

}

//****** 空格全刪除

else if (e.getKeyCode()== KeyEvent.VK_DELETE){

btnD.setEnabled(true);c=true;

result.setText("0.0");

num2=0;

}

}

public void keyTyped(KeyEvent e){}

public void keyReleased(KeyEvent e){}

//*************點(diǎn)擊事件監(jiān)聽(tīng)

public void actionPerformed(ActionEvent e){

if(e.getSource()==btn1)

num(1);

else if (e.getSource()==btn2)

num(2);

else if (e.getSource()== btn3)

num(3);

else if (e.getSource()== btn4)

num(4);

else if (e.getSource()== btn5)

num(5);

else if (e.getSource()== btn6)

num(6);

else if (e.getSource()== btn7)

num(7);

else if (e.getSource()== btn8)

num(8);

else if (e.getSource()== btn9)

num(9);

else if (e.getSource()== btn0)

num(0);

else if (e.getSource()== btn00)

num(100);

else if (e.getSource()== btnD){

str=result.getText();

str+=".";

result.setText(str);

btnD.setEnabled(false);

c=false;}

//******+-*%

else if (e.getSource()== btnAdd){

sign(1);

btnD.setEnabled(true);c=true;}

else if (e.getSource()== btnSub){

sign(2);

btnD.setEnabled(true);c=true;}

else if (e.getSource()== btnMul){

sign(3);

btnD.setEnabled(true);c=true;}

else if (e.getSource()== btnDiv){

sign(4);

btnD.setEnabled(true);c=true;}

else if (e.getSource()== btnAC){

btnD.setEnabled(true);c=true;

result.setText("0.0");

num2=0;

}

//***********=

else if (e.getSource()== btnResult){

btnD.setEnabled(true);

c=true;

num2=Double.parseDouble(result.getText());

if(add)

num1=num1+num2;

else if(sub)

num1=num1-num2;

else if(mul)

num1=num1*num2;

else if(div)

num1=num1/num2;

result.setText(String.valueOf(num1));

end=true;

//btnResult.setEnabled(false);

}

//***********Back鍵

else if (e.getSource() ==btnBack){

str=result.getText();

if(!str.equals(""))

{str=str.substring(0,str.length()-1);

result.setText(str);

}

else btnBack.setEnabled(false);

}

}

public void num(int i){

String s = null;

if(i!=100)

s=String.valueOf(i);

else s="00";

if(end){

result.setText("0.0");

end=false;

btnBack.setEnabled(true);

}

if((result.getText()).equals("0.0")){

result.setText(s);

btnBack.setEnabled(true);

}

else {

str=result.getText()+s;

result.setText(str);

btnBack.setEnabled(true);

}

}

public void sign(int s){

num1=Double.parseDouble(result.getText());

switch(s){

case 1:add=true;sub=false;mul=false;div=false;break;

case 2:add=false;sub=true;mul=false;div=false;break;

case 3:add=false;sub=false;mul=true;div=false;break;

case 4:

if(num1==0)result.setText("error");

else{add=false;sub=false;mul=false;div=true;break;}

}

end=true;

}

public static void main(String[] args){

Jisuanqi j=new Jisuanqi("計(jì)算器");

j.setSize(280,350);

j.setResizable(false);

j.setVisible(true);

}

}

用JAVA編寫(xiě)一個(gè)計(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)擊按鈕+、-、*、/時(shí),com?=?true

boolean?com?=?false;

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

int?i?=?0;

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

String?text?=?"";

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

double?defbutton?=?0;

//?+、-、*、/的代號(hào)分別為1,2,3,4

int?symbol?=?0;

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

Jisuanqi()?{

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

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

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

pane.setLayout(new?BorderLayout());

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

//?替換圖標(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);?//?對(duì)1按鈕添加監(jiān)聽(tīng)事件

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

setContentPane(pane);

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

}

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

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

/*

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

?*?false;或者是否點(diǎn)擊數(shù)字鍵,如果是i?=?1,如果沒(méi)有?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");

}

}

/*

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

?*?如果text中的數(shù)值=0就要判斷在這個(gè)數(shù)值中是否有.存在?如果有那么就在原來(lái)數(shù)值基礎(chǔ)之上添?加0;否則保持原來(lái)的數(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;

}

/*

?*?本程序不會(huì)讓一個(gè)數(shù)值中出現(xiàn)2個(gè)以上的小數(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ì)算器代碼

import javax.swing.*;

import javax.swing.border.Border;

import java.awt.*;

import java.awt.event.ActionListener;

import java.awt.event.ActionEvent;

import java.math.BigDecimal;

import java.math.RoundingMode;

import java.util.HashMap;

/**

* 我的計(jì)算器。Cheshi 繼承于 JFrame,是計(jì)算器的界面

c*/

public class Cheshi extends JFrame {

private Border border = BorderFactory.createEmptyBorder(5, 5, 5, 5);

private JTextField textbox = new JTextField("0");

private CalculatorCore core = new CalculatorCore();

private ActionListener listener = new ActionListener() {

public void actionPerformed(ActionEvent e) {

JButton b = (JButton) e.getSource();

String label = b.getText();

String result = core.process(label);

textbox.setText(result);

}

};

public Cheshi(String title) throws HeadlessException {

super(title); // 調(diào)用父類構(gòu)造方法

setupFrame(); // 調(diào)整窗體屬性

setupControls(); // 創(chuàng)建控件

}

private void setupControls() {

setupDisplayPanel(); // 創(chuàng)建文本面板

setupButtonsPanel(); // 創(chuàng)建按鈕面板

}

// 創(chuàng)建按鈕面板并添加按鈕

private void setupButtonsPanel() {

JPanel panel = new JPanel();

panel.setBorder(border);

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

createButtons(panel, new String[]{

"7", "8", "9", "+", "C",

"4", "5", "6", "-", "CE",

"1", "2", "3", "*", "", // 空字符串表示這個(gè)位置沒(méi)有按鈕

"0", ".", "=", "/", ""

});

this.add(panel, BorderLayout.CENTER);

}

/**

* 在指定的面板上創(chuàng)建按鈕

*

* @param panel 要?jiǎng)?chuàng)建按鈕的面板

* @param labels 按鈕文字

*/

private void createButtons(JPanel panel, String[] labels) {

for (String label : labels) {

// 如果 label 為空,則表示創(chuàng)建一個(gè)空面板。否則創(chuàng)建一個(gè)按鈕。

if (label.equals("")) {

panel.add(new JPanel());

} else {

JButton b = new JButton(label);

b.addActionListener(listener); // 為按鈕添加偵聽(tīng)器

panel.add(b);

}

}

}

// 設(shè)置顯示面板,用一個(gè)文本框來(lái)作為計(jì)算器的顯示部分。

private void setupDisplayPanel() {

JPanel panel = new JPanel();

panel.setLayout(new BorderLayout());

panel.setBorder(border);

setupTextbox();

panel.add(textbox, BorderLayout.CENTER);

this.add(panel, BorderLayout.NORTH);

}

// 調(diào)整文本框

private void setupTextbox() {

textbox.setHorizontalAlignment(JTextField.RIGHT); // 文本右對(duì)齊

textbox.setEditable(false); // 文本框只讀

textbox.setBackground(Color.white); // 文本框背景色為白色

}

// 調(diào)整窗體

private void setupFrame() {

this.setDefaultCloseOperation(EXIT_ON_CLOSE); // 當(dāng)窗體關(guān)閉時(shí)程序結(jié)束

this.setLocation(100, 50); // 設(shè)置窗體顯示在桌面上的位置

this.setSize(300, 200); // 設(shè)置窗體大小

this.setResizable(false); // 窗體大小固定

}

// 程序入口

public static void main(String[] args) throws Exception {

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

Cheshi frame = new Cheshi("我的計(jì)算器");

frame.setVisible(true); // 在桌面上顯示窗體

}

}

/**

* 計(jì)算器核心邏輯。這個(gè)邏輯只能處理 1~2 個(gè)數(shù)的運(yùn)算。

*/

class CalculatorCore {

private String displayText = "0"; // 要顯示的文本

boolean reset = true;

private BigDecimal number1, number2;

private String operator;

private HashMapString, Operator operators = new HashMapString, Operator();

private HashMapString, Processor processors = new HashMapString, Processor();

CalculatorCore() {

setupOperators();

setupProcessors();

}

// 為每種命令添加處理方式

private void setupProcessors() {

processors.put("[0-9]", new Processor() {

public void calculate(String command) {

numberClicked(command);

}

});

processors.put("\\.", new Processor() {

public void calculate(String command) {

dotClicked();

}

});

processors.put("=", new Processor() {

public void calculate(String command) {

equalsClicked();

}

});

processors.put("[+\\-*/]", new Processor() {

public void calculate(String command) {

operatorClicked(command);

}

});

processors.put("C", new Processor() {

public void calculate(String command) {

clearClicked();

}

});

processors.put("CE", new Processor() {

public void calculate(String command) {

clearErrorClicked();

}

});

}

// 為每種 operator 添加處理方式

private void setupOperators() {

operators.put("+", new Operator() {

public BigDecimal process(BigDecimal number1, BigDecimal number2) {

return number1.add(number2);

}

});

operators.put("-", new Operator() {

public BigDecimal process(BigDecimal number1, BigDecimal number2) {

return number1.subtract(number2);

}

});

operators.put("*", new Operator() {

public BigDecimal process(BigDecimal number1, BigDecimal number2) {

return number1.multiply(number2);

}

});

operators.put("/", new Operator() {

public BigDecimal process(BigDecimal number1, BigDecimal number2) {

return number1.divide(number2, 30, RoundingMode.HALF_UP);

}

});

}

// 根據(jù)命令處理。這里的命令實(shí)際上就是按鈕文本。

public String process(String command) {

for (String pattern : processors.keySet()) {

if (command.matches(pattern)) {

processors.get(pattern).calculate(command);

break;

}

}

return displayText;

}

// 當(dāng)按下 CE 時(shí)

private void clearErrorClicked() {

if (operator == null) {

number1 = null;

} else {

number2 = null;

}

displayText = "0";

reset = true;

}

// 當(dāng)按下 C 時(shí),將計(jì)算器置為初始狀態(tài)。

private void clearClicked() {

number1 = null;

number2 = null;

operator = null;

displayText = "0";

reset = true;

}

// 當(dāng)按下 = 時(shí)

private void equalsClicked() {

calculateResult();

number1 = null;

number2 = null;

operator = null;

reset = true;

}

// 計(jì)算結(jié)果

private void calculateResult() {

number2 = new BigDecimal(displayText);

Operator oper = operators.get(operator);

if (oper != null) {

BigDecimal result = oper.process(number1, number2);

displayText = result.toString();

}

}

// 當(dāng)按下 +-*/ 時(shí)(這里也可以擴(kuò)展成其他中間操作符)

private void operatorClicked(String command) {

if (operator != null) {

calculateResult();

}

number1 = new BigDecimal(displayText);

operator = command;

reset = true;

}

// 當(dāng)按下 . 時(shí)

private void dotClicked() {

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

displayText += ".";

} else if (reset) {

displayText = "0.";

}

reset = false;

}

// 當(dāng)按下 0-9 時(shí)

private void numberClicked(String command) {

if (reset) {

displayText = command;

} else {

displayText += command;

}

reset = false;

}

// 運(yùn)算符處理接口

interface Operator {

BigDecimal process(BigDecimal number1, BigDecimal number2);

}

// 按鈕處理接口

interface Processor {

void calculate(String command);

}

}

計(jì)算器java代碼

import java.awt.Color;

import java.awt.Font;

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JTextField;

import javax.swing.WindowConstants;

import javax.swing.border.LineBorder;

class Normal{

double i,j;

public Normal(double num1,double num2){

i=num1;

j=num2;

}

public double puls(){

return i+j;

}

public double subtract(){

return i-j;

}

public double multiply(){

return i*j;

}

public double divide(){

return i/j;

}

public double surpuls(){

return i%j;

}

}

class scientific extends Normal{

public scientific(int num1, int num2) {

super(num1, num2);

}

}

public class calc extends JFrame{

public static void main(String[] args) {

viewNormal VN= new viewNormal("normal");

}

}

class viewNormal extends JFrame implements ActionListener{

JPanel jp1 = new JPanel(new GridLayout(4,3,5,5));

JPanel jp2 = new JPanel(new GridLayout(5,1,5,5));

JLabel jl;

JButton[] jb;

JButton jbs,jbo,jba,jbb,jbc,jby;

StringBuffer sb = new StringBuffer();

Normal normal;

int dot=0;

double fnum=0;

double lnum=0;

double result;

String sign=null;

public viewNormal(String title){

setTitle(title);

setLayout(null);

setVisible(true);

setBounds(200,200,305,350);

setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

jb= new JButton[12];

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

jb[i]=new JButton(""+(i+1));

jp1.add(jb[i]);

jb[i].addActionListener(this);

}

jb[9]=new JButton(".");

jb[10]=new JButton("0");

jb[11]=new JButton("=");

jb[9].addActionListener(this);

jb[10].addActionListener(this);

jb[11].addActionListener(this);

jp1.add(jb[9]);

jp1.add(jb[10]);

jp1.add(jb[11]);

jp1.setBounds(10, 100, 200, 200);

jbs= new JButton("+");jbo= new JButton("-");jba= new JButton("*");

jbb= new JButton("/");jby= new JButton("%");jbc= new JButton("C");

jbs.addActionListener(this);jbo.addActionListener(this);jba.addActionListener(this);

jbb.addActionListener(this);jby.addActionListener(this);jbc.addActionListener(this);

//jp2.add(jby);

jp2.add(jbs);jp2.add(jbo);jp2.add(jba);jp2.add(jbb);jp2.add(jbc);

jp2.setBounds(215, 100, 70, 200);

jl= new JLabel("0",JLabel.RIGHT);

jl.setFont(new Font("Batang",Font.BOLD, 20));

jl.setBorder(new LineBorder(Color.black,2));

jl.setBackground(Color.white);

jl.setBounds(10, 40, 275, 50);

jl.setOpaque(true);

add(jl);

add(jp1);

add(jp2);

}

//+

public void sum(){

lnum=Double.parseDouble(sb.toString());

normal=new Normal(fnum,lnum);

fnum=normal.puls();

result=fnum;

}

//-

private void sub() {

System.out.println(sb.toString());

lnum=Double.parseDouble(sb.toString());

normal=new Normal(fnum,lnum);

fnum=normal.subtract();

result=fnum;

}

//*

private void mul() {

lnum=Double.parseDouble(sb.toString());

normal=new Normal(fnum,lnum);

fnum=normal.multiply();

result=fnum;

}

// /

private void div() {

lnum=Double.parseDouble(sb.toString());

normal=new Normal(fnum,lnum);

fnum=normal.divide();

result=fnum;

}

//%

private void sur() {

lnum=Double.parseDouble(sb.toString());

normal=new Normal(fnum,lnum);

fnum=normal.surpuls();

result=fnum;

}

// =

private void same(){

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

sum();

}

if(sign.equals("-")){

sub();

}

if(sign.equals("*")){

mul();

}

if(sign.equals("/")){

div();

}

if(sign.equals("%")){

sur();

}

}

//result

public void Result(){

if(result%1!=0)

jl.setText(""+result);

else

{

int i=(int)result;

jl.setText(""+i);

}

}

@Override

public void actionPerformed(ActionEvent e) {

//System.out.println(sb.toString());

// 1~9

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

if(e.getSource()==jb[i]!sb.toString().equals("0")){

sb.append(jb[i].getText());

jl.setText(sb.toString());

}

else if(e.getSource()==jb[i]sb.toString().equals("0")){

int d=sb.length();

sb.delete(0, d);

sb.append(jb[i].getText());

jl.setText(sb.toString());

}

}

// 0

if(e.getSource()==jb[10]!sb.toString().equals("0")){

sb.append(jb[10].getText());

jl.setText(sb.toString());

}

// .

if(e.getSource()==jb[9]dot==0!sb.toString().equals("")){

dot++;

sb.append(jb[9].getText());

jl.setText(sb.toString());

}

// =

if(e.getSource()==jb[11]!sb.toString().equals("")){

same();

Result();

int d=sb.length();

sb.delete(0, d);

dot=0;

}

// +

if(e.getSource()==jbs!sb.toString().equals("")){

if(sign!="+"sign!=null)

same();

else

sum();

sign ="+";

Result();

int d=sb.length();

sb.delete(0, d);

dot=0;

}

//-

if(e.getSource()==jbo!sb.toString().equals("")){

if(fnum==0)

fnum=2*Double.parseDouble(sb.toString());

if(sign!="-"sign!=null)

same();

else

sub();

sign ="-";

Result();

int d=sb.length();

sb.delete(0, d);

dot=0;

}

//*

if(e.getSource()==jba!sb.toString().equals("")){

if(fnum==0)

fnum=1;

if(sign!="*"sign!=null)

same();

else

mul();

sign ="*";

Result();

int d=sb.length();

sb.delete(0, d);

dot=0;

}

// /

if(e.getSource()==jbb!sb.toString().equals("")){

if(fnum==0)

fnum=Math.pow(Double.parseDouble(sb.toString()),2);

if(sign!="/"sign!=null)

same();

else

div();

sign ="/";

Result();

int d=sb.length();

sb.delete(0, d);

dot=0;

}

//%

// if(e.getSource()==jby!sb.toString().equals("")){

// if(fnum==0){

// fnum=Double.parseDouble(sb.toString());

// result=fnum;

// }

// else {

// if(sign!="%"sign!=null)

// same();

// else{

// lnum=Double.parseDouble(sb.toString());

// normal=new Normal(fnum,lnum);

// fnum=normal.surpuls();

// result=fnum;

// }

// }

// sign ="%";

// Result();

// int d=sb.length();

// sb.delete(0, d);

// dot=0;

// }

//clear

if(e.getSource()==jbc){

int d=sb.length();

sb.delete(0, d);

jl.setText("0");

dot=0;

fnum=0;

lnum=0;

sign=null;

}

}

}

class viewScientific extends viewNormal{

public viewScientific(String title){

super(title);

setBounds(200,200,800,500);

}

}

//等號(hào)以后輸入符號(hào)用不了, String轉(zhuǎn) double 本來(lái)就有錯(cuò)誤,你可以用我的擴(kuò)展成科學(xué)型的。

JAVA計(jì)算器代碼

import java.awt.*;

import java.applet.*;

public class CalculatorDemo extends Applet

{

TextField answerText;

Button pointButton,equalButton,plusButton;

Button minusButton,clearButton,multiButton,divisionButton;

Button[] b=new Button[10];

String currentOp,preOp;

String foreText,backText;

boolean isFloat=false;

public void init()

{

Panel p1=new Panel();

Panel p2=new Panel();

Panel p3=new Panel();

currentOp=new String("");

preOp=new String("");

foreText=new String("");

backText=new String("");

answerText=new TextField(8);

setBackground(Color.lightGray);

setForeground(Color.blue);

for(int i=9;i=0;i--)

{

b[i]=new Button(Integer.toString(i));

p2.add(b[i]);

}

pointButton=new Button(".");

equalButton=new Button("=");

equalButton.setForeground(Color.red);

clearButton=new Button("清除");

clearButton.setForeground(Color.red);

divisionButton=new Button("/");

divisionButton.setForeground(Color.red);

multiButton=new Button("*");

multiButton.setForeground(Color.red);

minusButton=new Button("-");

minusButton.setForeground(Color.red);

plusButton=new Button("+");

plusButton.setForeground(Color.red);

setLayout(new FlowLayout());

p1.setLayout(new FlowLayout());

p2.setLayout(new GridLayout(4,3));

p3.setLayout(new GridLayout(4,1));

p1.add(answerText);

p1.add(clearButton);

p2.add(pointButton);

p2.add(equalButton);

p3.add(plusButton);

p3.add(minusButton);

p3.add(multiButton);

p3.add(divisionButton);

add(p1);

add(p2);

add(p3);

}

public boolean action(Event e,Object o)

{

String s=new String("");

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

{

if(e.target==b[i]||e.target==pointButton)

{

if(e.target !=pointButton)

{

s=(String)o;

doForeText(s);

}

if((e.target==pointButton)(!isFloat))

{

isFloat=true;

s=(String)o;

if(foreText.equals(""))

{

foreText +="0.";

}

else

{

doForeText(s);

}

}

}

}

if(e.target==clearButton)

{

doClear();

}

if((e.target==clearButton)||(e.target==divisionButton)

||(e.target==plusButton)||(e.target==minusButton))

{

if(foreText !="")

{

currentOp=((String)o);

doOperator();

}

else

{

preOp=((String)o);

}

}

if(e.target==equalButton)

{

doOperator();

}

return true;

}

public void doOperator()

{

double dFore,dBack;

Double d;

if(preOp.equals(""))

{

backText=foreText;

foreText="";

answerText.setText(backText);

}

else

{

dFore=(new Double(foreText)).doubleValue();

dBack=(new Double(backText)).doubleValue();

foreText="";

backText=answerText.getText();

if(preOp.equals("+"))

{

d=new Double((dBack+dFore));

answerText.setText(d.toString());

backText=d.toString();

}

if(preOp.equals("-"))

{

d=new Double((dBack-dFore));

answerText.setText(d.toString());

backText=d.toString();

}

if(preOp.equals("*"))

{

d=new Double((dBack*dFore));

answerText.setText(d.toString());

backText=d.toString();

}

if(preOp.equals("/"))

{

if(dFore==0)

{

answerText.setText("除數(shù)不能為0");

return;

}

d=new Double((dBack/dFore));

answerText.setText(d.toString());

backText=d.toString();

}

}

preOp=currentOp;

}

public void doForeText(String s)

{

foreText +=s;

answerText.setText(foreText);

}

public void doBackText(String s)

{

backText=foreText;

foreText="";

answerText.setText(foreText);

}

public void doClear()

{

currentOp="";

preOp="";

foreText="";

backText="";

isFloat=false;

answerText.setText("");

}

}


當(dāng)前題目:萬(wàn)能計(jì)算器java代碼,萬(wàn)能計(jì)算器java代碼是什么
分享路徑:http://weahome.cn/article/hdippe.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部