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

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

計(jì)算器退格鍵java代碼,計(jì)算器退格鍵java代碼是多少

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

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

站在用戶的角度思考問題,與客戶深入溝通,找到盤山網(wǎng)站設(shè)計(jì)與盤山網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶體驗(yàn)好的作品,建站類型包括:成都網(wǎng)站制作、網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、域名注冊(cè)、網(wǎng)絡(luò)空間、企業(yè)郵箱。業(yè)務(wù)覆蓋盤山地區(qū)。

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

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

}

}

計(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 本來就有錯(cuò)誤,你可以用我的擴(kuò)展成科學(xué)型的。

幫忙用java補(bǔ)齊制作計(jì)算器加減乘除等于小數(shù)點(diǎn)這幾個(gè)運(yùn)算符的代碼

import?java.awt.*;

import?java.awt.event.*;

import?javax.swing.*;

import?javax.swing.border.*;

import?java.util.LinkedList;?//工具包

import?java.text.NumberFormat;?//文本包

/**

*?java?swing計(jì)算器

*?@author?young

*

*/

public?class?Calculator?extends?Frame?implements?ActionListener?//?計(jì)算器類

{

JTextField?result;

NumberButton?numberButton[];

OperatorButton?operatorButton[];

Button?radixpoint,?positiveminus,?backspace,?reciprocal,?equal,?clear;?//?聲明成員變量

//?小數(shù)點(diǎn)按鈕,正負(fù)號(hào)按鈕,退格按鈕,求倒數(shù)按鈕,等號(hào)按鈕,清零按鈕

Panel?panel;

String?operator[]?=?{?"+",?"-",?"*",?"/"?};

LinkedList?linklist;

boolean?pressequal?=?false;

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

{

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

linklist?=?new?LinkedList();

numberButton?=?new?NumberButton[10];

for?(int?i?=?0;?i?=?9;?i++)?{

numberButton[i]?=?new?NumberButton(i);

numberButton[i].addActionListener(this);

}

operatorButton?=?new?OperatorButton[4];

for?(int?i?=?0;?i??4;?i++)?{

operatorButton[i]?=?new?OperatorButton(operator[i]);

operatorButton[i].addActionListener(this);

}

radixpoint?=?new?Button(".");

positiveminus?=?new?Button("+/-");

backspace?=?new?Button("CE");

reciprocal?=?new?Button("1/x");

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

clear?=?new?Button("C");

radixpoint.setForeground(Color.red);

positiveminus.setForeground(Color.red);

backspace.setForeground(Color.red);

reciprocal.setForeground(Color.red);

equal.setForeground(Color.red);

clear.setForeground(Color.red);

radixpoint.addActionListener(this);

positiveminus.addActionListener(this);

backspace.addActionListener(this);

reciprocal.addActionListener(this);

equal.addActionListener(this);

clear.addActionListener(this);

result?=?new?JTextField(10);

result.setHorizontalAlignment(JTextField.RIGHT);

result.setForeground(Color.black);

result.setBackground(Color.white);

result.setFont(new?Font("TimesRoman",?Font.PLAIN,?14));

result.setBorder(new?SoftBevelBorder(BevelBorder.LOWERED));

result.setEditable(false);

panel?=?new?Panel();

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

panel.add(numberButton[1]);

panel.add(numberButton[2]);

panel.add(numberButton[3]);

panel.add(backspace);

panel.add(clear);

panel.add(numberButton[4]);

panel.add(numberButton[5]);

panel.add(numberButton[6]);

panel.add(operatorButton[0]);

panel.add(operatorButton[2]);

panel.add(numberButton[7]);

panel.add(numberButton[8]);

panel.add(numberButton[9]);

panel.add(operatorButton[1]);

panel.add(operatorButton[3]);

panel.add(numberButton[0]);

panel.add(positiveminus);

panel.add(reciprocal);

panel.add(radixpoint);

panel.add(equal);

add(result,?"North");

add(panel,?"Center");

addWindowListener(new?WindowAdapter()?{

public?void?windowClosing(WindowEvent?e)?{

System.exit(0);

}

});

setSize(270,?200);

setLocation(300,?230);

setVisible(true);

}

public?void?actionPerformed(ActionEvent?e)?//?按鈕的單擊事件處理方法

{

if?(e.getSource()?instanceof?NumberButton)?//?數(shù)字按鈕

{

NumberButton?b?=?(NumberButton)?e.getSource();

if?(linklist.size()?==?0)?{

int?number?=?b.getNumber();

linklist.add(""?+?number);

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

pressequal?=?false;

}

else?if?(linklist.size()?==?1??pressequal?==?false)?{

int?number?=?b.getNumber();

String?num?=?(String)?linklist.getFirst();

String?s?=?num.concat(""?+?number);

linklist.set(0,?s);

result.setText(s);

}?else?if?(linklist.size()?==?1??pressequal?==?true)?{

int?number?=?b.getNumber();

linklist.removeFirst();

linklist.add(""?+?number);

pressequal?=?false;

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

}?else?if?(linklist.size()?==?2)?{

int?number?=?b.getNumber();

linklist.add(""?+?number);

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

}?else?if?(linklist.size()?==?3)?{

int?number?=?b.getNumber();

String?num?=?(String)?linklist.getLast();

String?s?=?num.concat(""?+?number);

linklist.set(2,?s);

result.setText(s);

}

}?else?if?(e.getSource()?instanceof?OperatorButton)?//?操作按鈕

{

OperatorButton?b?=?(OperatorButton)?e.getSource();

if?(linklist.size()?==?1)?{

String?fuhao?=?b.getOperator();

linklist.add(fuhao);

}?else?if?(linklist.size()?==?2)?{

String?fuhao?=?b.getOperator();

linklist.set(1,?fuhao);

}?else?if?(linklist.size()?==?3)?{

String?fuhao?=?b.getOperator();

String?number1?=?(String)?linklist.getFirst();

String?number2?=?(String)?linklist.getLast();

String?operator?=?(String)?linklist.get(1);

try?{

double?n1?=?Double.parseDouble(number1);

double?n2?=?Double.parseDouble(number2);

double?n?=?0;

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

n?=?n1?+?n2;

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

n?=?n1?-?n2;

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

n?=?n1?*?n2;

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

n?=?n1?/?n2;

}

linklist.clear();

linklist.add(""?+?n);

linklist.add(fuhao);

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

}?catch?(Exception?ee)?{

}

}

}?else?if?(e.getSource()?==?equal)?//?等號(hào)按鈕

{

pressequal?=?true;

if?(linklist.size()?==?1?||?linklist.size()?==?2)?{

String?num?=?(String)?linklist.getFirst();

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

}?else?if?(linklist.size()?==?3)?{

String?number1?=?(String)?linklist.getFirst();

String?number2?=?(String)?linklist.getLast();

String?operator?=?(String)?linklist.get(1);

try?{

double?n1?=?Double.parseDouble(number1);

double?n2?=?Double.parseDouble(number2);

double?n?=?0;

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

n?=?n1?+?n2;

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

n?=?n1?-?n2;

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

n?=?n1?*?n2;

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

n?=?n1?/?n2;

}

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

linklist.set(0,?""?+?n);

linklist.removeLast();

linklist.removeLast();

}?catch?(Exception?ee)?{

}

}

}?else?if?(e.getSource()?==?radixpoint)?//?小數(shù)點(diǎn)按鈕

{

if?(linklist.size()?==?0)?{

pressequal?=?false;

}?else?if?(linklist.size()?==?1)?{

String?dot?=?radixpoint.getLabel();

String?num?=?(String)?linklist.getFirst();

String?s?=?null;

if?(num.indexOf(dot)?==?-1)?{

s?=?num.concat(dot);

linklist.set(0,?s);

}?else?{

s?=?num;

}

linklist.set(0,?s);

result.setText(s);

}

else?if?(linklist.size()?==?3)?{

String?dot?=?radixpoint.getLabel();

String?num?=?(String)?linklist.getLast();

String?s?=?null;

if?(num.indexOf(dot)?==?-1)?{

s?=?num.concat(dot);

linklist.set(2,?s);

}?else?{

s?=?num;

}

result.setText(s);

}

}?else?if?(e.getSource()?==?backspace)?//?退格按鈕

{

if?(linklist.size()?==?1)?{

String?num?=?(String)?linklist.getFirst();

if?(num.length()?=?1)?{

num?=?num.substring(0,?num.length()?-?1);

linklist.set(0,?num);

result.setText(num);

}?else?{

linklist.removeLast();

result.setText("0");

}

}?else?if?(linklist.size()?==?3)?{

String?num?=?(String)?linklist.getLast();

if?(num.length()?=?1)?{

num?=?num.substring(0,?num.length()?-?1);

linklist.set(2,?num);

result.setText(num);

}?else?{

linklist.removeLast();

result.setText("0");

}

}

}?else?if?(e.getSource()?==?positiveminus)?//?正負(fù)號(hào)按鈕

{

if?(linklist.size()?==?1)?{

String?number1?=?(String)?linklist.getFirst();

try?{

double?d?=?Double.parseDouble(number1);

d?=?-1?*?d;

String?str?=?String.valueOf(d);

linklist.set(0,?str);

result.setText(str);

}?catch?(Exception?ee)?{

}

}?else?if?(linklist.size()?==?3)?{

String?number2?=?(String)?linklist.getLast();

try?{

double?d?=?Double.parseDouble(number2);

d?=?-1?*?d;

String?str?=?String.valueOf(d);

linklist.set(2,?str);

result.setText(str);

}?catch?(Exception?ee)?{

}

}

}?else?if?(e.getSource()?==?reciprocal)?//?求倒數(shù)按鈕

{

if?(linklist.size()?==?1?||?linklist.size()?==?2)?{

String?number1?=?(String)?linklist.getFirst();

try?{

double?d?=?Double.parseDouble(number1);

d?=?1.0?/?d;

String?str?=?String.valueOf(d);

linklist.set(0,?str);

result.setText(str);

}?catch?(Exception?ee)?{

}

}?else?if?(linklist.size()?==?3)?{

String?number2?=?(String)?linklist.getLast();

try?{

double?d?=?Double.parseDouble(number2);

d?=?1.0?/?d;

String?str?=?String.valueOf(d);

linklist.set(0,?str);

result.setText(str);

}?catch?(Exception?ee)?{

}

}

}?else?if?(e.getSource()?==?clear)?//?清零按鈕

{

pressequal?=?false;

result.setText("0");

linklist.clear();

}

}

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

new?Calculator();

}

}

class?NumberButton?extends?Button?//?數(shù)字按鈕類

{

int?number;

public?NumberButton(int?number)?//?構(gòu)造方法

{

super(""?+?number);

this.number?=?number;

setForeground(Color.blue);

}

public?int?getNumber()?{

return?number;

}

}

class?OperatorButton?extends?Button?//?運(yùn)算符號(hào)按鈕類

{

String?operator;

public?OperatorButton(String?operator)?//?構(gòu)造方法

{

super(operator);

this.operator?=?operator;

setForeground(Color.red);

}

public?String?getOperator()?{

return?operator;

}

}

java 寫計(jì)算器的退格鍵怎么寫,如“123456”按下退格變“12345”

String str = "12345";

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

int a = Integer.parseInt(str);

誰能給一個(gè)Java的計(jì)算器程序代碼(加注釋 )

以前寫的,給你了

import java.awt.BorderLayout;

import java.awt.GridLayout;

import java.awt.event.MouseEvent;

import java.awt.event.MouseListener;

import java.text.DecimalFormat;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.JTextField;

public class Calculator {

//計(jì)算器面板

private JFrame f = new JFrame("Calculator");

//輸入面扳

private JPanel inputPanel = new JPanel();

//加減乘除面板

private JPanel operationPanel = new JPanel();

//數(shù)字面板

private JPanel buttonsPanel = new JPanel();

//輸入數(shù)據(jù)文本框

private JTextField input = new JTextField(20);

//退格鍵

private JButton backspace = new JButton("BackSpace");

//清空

private JButton CE = new JButton("CE ");

//刪除

private JButton C = new JButton("C ");

//四則運(yùn)算符號(hào)鍵

private JButton add = new JButton("+");

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

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

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

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

private JButton decimal = new JButton(".");

//等號(hào)

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

//數(shù)字鍵

private JButton zero = new JButton("0");

private JButton one = new JButton("1");

private JButton two = new JButton("2");

private JButton three = new JButton("3");

private JButton four = new JButton("4");

private JButton five = new JButton("5");

private JButton six = new JButton("6");

private JButton seven = new JButton("7");

private JButton eight = new JButton("8");

private JButton nine = new JButton("9");

private String num1 = "";//保存第一個(gè)運(yùn)算數(shù)字

private String operator = "";//保存運(yùn)算符號(hào)

public static void main(String[] args) {

new Calculator();//new計(jì)算器實(shí)例

}

public Calculator(){

//添加組件,布局

inputPanel.add(input);

f.add(inputPanel, BorderLayout.NORTH);

operationPanel.add(backspace);

operationPanel.add(CE);

operationPanel.add(C);

f.add(operationPanel, BorderLayout.CENTER);

buttonsPanel.add(add);

buttonsPanel.add(sub);

buttonsPanel.add(mul);

buttonsPanel.add(div);

buttonsPanel.add(one);

buttonsPanel.add(two);

buttonsPanel.add(three);

buttonsPanel.add(zero);

buttonsPanel.add(four);

buttonsPanel.add(five);

buttonsPanel.add(six);

buttonsPanel.add(decimal);

buttonsPanel.add(seven);

buttonsPanel.add(eight);

buttonsPanel.add(nine);

buttonsPanel.add(equal);

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

f.add(buttonsPanel, BorderLayout.SOUTH);

//注冊(cè)各個(gè)組件監(jiān)聽事件

backspace.addMouseListener(new OperationMouseListener());

CE.addMouseListener(new OperationMouseListener());

C.addMouseListener(new OperationMouseListener());

decimal.addMouseListener(new OperationMouseListener());

equal.addMouseListener(new OperationMouseListener());

//注冊(cè)四則運(yùn)算監(jiān)聽

add.addMouseListener(new CalcMouseListener());

sub.addMouseListener(new CalcMouseListener());

mul.addMouseListener(new CalcMouseListener());

div.addMouseListener(new CalcMouseListener());

//注冊(cè)數(shù)字監(jiān)聽事件

zero.addMouseListener(new NumberMouseListener());

one.addMouseListener(new NumberMouseListener());

two.addMouseListener(new NumberMouseListener());

three.addMouseListener(new NumberMouseListener());

four.addMouseListener(new NumberMouseListener());

five.addMouseListener(new NumberMouseListener());

six.addMouseListener(new NumberMouseListener());

seven.addMouseListener(new NumberMouseListener());

eight.addMouseListener(new NumberMouseListener());

nine.addMouseListener(new NumberMouseListener());

f.setVisible(true);

f.pack();

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

private class NumberMouseListener implements MouseListener{

public void mouseClicked(MouseEvent e) {

if(input.getText().trim().equals("0")){//如果文本框已經(jīng)是0,結(jié)果還是0

input.setText(((JButton)e.getSource()).getText().trim());

}else{//否則的話,把0添加到后面,譬如文本框是1,結(jié)果就為10

input.setText(input.getText().concat(((JButton)e.getSource()).getText().trim()));

}

}

public void mouseEntered(MouseEvent e) {}

public void mouseExited(MouseEvent e) {}

public void mousePressed(MouseEvent e) {}

public void mouseReleased(MouseEvent e) {}

}

private class CalcMouseListener implements MouseListener{

//如果輸入的是運(yùn)算符號(hào),保存第一個(gè)結(jié)果和運(yùn)算符號(hào)

public void mouseClicked(MouseEvent e) {

num1 = input.getText().trim();input.setText("");

operator = ((JButton)e.getSource()).getText().trim();

}

public void mouseEntered(MouseEvent e) {}

public void mouseExited(MouseEvent e) {}

public void mousePressed(MouseEvent e) {}

public void mouseReleased(MouseEvent e) {}

}

private class OperationMouseListener implements MouseListener{

public void mouseClicked(MouseEvent e) {

if(e.getSource() == backspace){//退格鍵,刪除一個(gè)直到?jīng)]有字符刪除

String inputText = input.getText();

if(inputText.length() 0){

input.setText(inputText.substring(0, inputText.length() - 1));

}

}else if(e.getSource() == C){

input.setText("0");//C,清空所有運(yùn)算數(shù)字和符號(hào)

num1 = "";

}else if(e.getSource() == CE){

input.setText("0");//CE--將文本框置為0

}else if(e.getSource() == decimal){

String text = input.getText().trim();

//如果按了小數(shù)點(diǎn),如果文本框已經(jīng)有小數(shù)點(diǎn),不做任何操作,否則在結(jié)果后面加上小數(shù)點(diǎn)

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

input.setText(text.concat("."));

}

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

//如果是等號(hào)

if(!operator.trim().equals("")){

if(!input.getText().trim().equals("")){

double result = 0D;

if(operator.equals("+")){//執(zhí)行加法運(yùn)算

result = Double.parseDouble(num1) + Double.parseDouble(input.getText().trim());

}else if(operator.equals("-")){//減法運(yùn)算

result = Double.parseDouble(num1) - Double.parseDouble(input.getText().trim());

}else if(operator.equals("*")){//乘法運(yùn)算

result = Double.parseDouble(num1) * Double.parseDouble(input.getText().trim());

}else if(operator.equals("/")){//除法運(yùn)算

result = Double.parseDouble(num1) / Double.parseDouble(input.getText().trim());

}

//格式化最終結(jié)果,保留兩位小數(shù)點(diǎn)

input.setText(new DecimalFormat("0.00").format(result));

}

}

}

}

public void mouseEntered(MouseEvent e) {}

public void mouseExited(MouseEvent e) {}

public void mousePressed(MouseEvent e) {}

public void mouseReleased(MouseEvent e) {}

}

}

有沒有完整的計(jì)算器Java代碼,不要有錯(cuò)誤的,急求

package?jisuanqi;

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.JButton;

import?javax.swing.JFrame;

import?javax.swing.JPanel;

import?javax.swing.JTextField;

public?class?Calculator?extends?JFrame?implements?ActionListener{?????//建一個(gè)Calculator類繼承JFrame實(shí)現(xiàn)ActionListener

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

Calculator?calculator=new?Calculator();?????//創(chuàng)建對(duì)象calculator

calculator.setVisible(true);???????????????//窗口可見

calculator.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);??//關(guān)閉圖標(biāo)后結(jié)束程序

}

String?keys?[]={"7","8","9","/","4","5","6","*","1","2","3","-","0",".","=","+"};//表示計(jì)算器的鍵的名字

String?command?[]={"backspace","C"};//功能鍵

JButton?KEYS?[]=new?JButton[keys.length];//創(chuàng)建鍵對(duì)象

JButton?COMMAND[]=new?JButton[command.length];//功能鍵對(duì)象

static?JTextField?resultSet=new?JTextField();//建一個(gè)文本框

double?resultNum;????????//結(jié)果

String?operator="";//運(yùn)算符??---------------------------

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

super();??????

init();//初始化計(jì)算器

this.setBackground(Color.WHITE);//this代表用當(dāng)前構(gòu)造方法創(chuàng)建的對(duì)象

this.setTitle("簡單計(jì)算器");????//窗口標(biāo)題

this.setLocation(500,?300);???//窗口大小

this.pack(); //使組件大小合適

}

void?init()?{??????//初始化計(jì)算器

resultSet.setHorizontalAlignment(JTextField.RIGHT);?????//設(shè)置文本框內(nèi)容向右對(duì)齊

resultSet.setBackground(Color.WHITE);???????????//設(shè)置文本框背景顏色

JPanel?keysPanel=new?JPanel();//創(chuàng)建一個(gè)面板

keysPanel.setLayout(new?GridLayout(4,5,3,3));?//設(shè)置面板的布局為網(wǎng)格型

for?(int?i=0;ikeys.length;i++)?{??????//把數(shù)字鍵和符號(hào)鍵設(shè)為按鈕

KEYS[i]=new?JButton(keys[i]);

keysPanel.add(KEYS[i]);????????????//把按鈕加到面板中

}

JPanel?comPanel=new?JPanel();???????//創(chuàng)建命令鍵面板

comPanel.setLayout(new?GridLayout(1,2,3,3));//設(shè)置面板布局為網(wǎng)格型?

for(int?i=0;icommand.length;i++)?{????//把命令鍵設(shè)置為按鈕

COMMAND[i]=new?JButton(command[i]);

comPanel.add(COMMAND[i]);??????????//把按鈕放到面板上

}

JPanel?pane=new?JPanel();??????//創(chuàng)建面板

pane.setLayout(new?BorderLayout());??//面板布局為區(qū)域型

pane.add(comPanel,BorderLayout.NORTH);?????//把命令面板加入到大面板上

pane.add(keysPanel,BorderLayout.CENTER); //把數(shù)字鍵面板加到大面板

getContentPane().setLayout(new?BorderLayout());?????//設(shè)置底層容器的布局為區(qū)域型

getContentPane().add(resultSet,BorderLayout.NORTH);???//把文本框放到北面

getContentPane().add(pane,BorderLayout.CENTER);????//把大面板放到中央

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

KEYS[i].addActionListener(this);//為按鈕添加監(jiān)聽器

}

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

COMMAND[i].addActionListener(this);???

}

}

//處理事件 ?

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

String?lable=e.getActionCommand();

if(lable.equals("backspace")){??????//如果命令為backspace

backspace();

}

else?if?(lable.equals("C")){?????//如果命令為C

Clear();

}

else?if?(Character.isDigit(lable.charAt(0)))?{???????//---------------

if(resultSet.getText().equals("0")){

resultSet.setText("");

}

resultSet.setText(resultSet.getText()+lable);

}

else?if?(lable.equals(".")){

point();

}

else?if?(lable.equals("=")){

equal();

}

else?{

doOperation();

resultSet.setText("");

operator=lable;

}

}

void?point()?{

String?s=resultSet.getText();

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

resultSet.setText(s+".");

}

}

void?equal()?{

doOperation();

resultSet.setText(""+resultNum);

operator="";

}

void?doOperation()?{

double?m=Double.parseDouble(resultSet.getText());

if(operator.equals("")){

resultNum=m;

}

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

resultNum+=m;

}

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

resultNum-=m;

}

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

resultNum*=m;

}

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

resultNum/=m;

}

}

void?Clear()?{?????//清零

resultSet.setText("0");

}

void?backspace()?{????//退格

String?text?=resultSet.getText();

if?(text.length()0){

resultSet.setText(text.substring(0,?text.length()-1));//將文本最后一個(gè)字符刪除

}

else?if?(text.length()==0){

resultSet.setText("0");

}

}

}


分享標(biāo)題:計(jì)算器退格鍵java代碼,計(jì)算器退格鍵java代碼是多少
轉(zhuǎn)載來源:http://weahome.cn/article/dsspoci.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部