新网创想网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

计算器退格键java代码,计算器退格键java代码是多少

求计算器的java代码!!!

我也刚学,不太会,刚好老师也是布置的这个任务。你看看吧。 自己改改。

站在用户的角度思考问题,与客户深入沟通,找到盘山网站设计与盘山网站推广的解决方案,凭借多年的经验,让设计与互联网技术结合,创造个性化、用户体验好的作品,建站类型包括:成都网站制作、网站建设、企业官网、英文网站、手机端网站、网站推广、域名注册、网络空间、企业邮箱。业务覆盖盘山地区。

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

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数字按钮小数点 ************

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

//*******************功能键区

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("关于");

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,"哈理工-计算机09-1班 7号 伍峰杰","作者",JOptionPane.INFORMATION_MESSAGE);

}

}

);

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

}

//*******数字按钮事件

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

//*************小数点

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

//******等号

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

//*************点击事件监听

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.setSize(280,350);

j.setResizable(false);

j.setVisible(true);

}

}

计算器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);

}

}

//等号以后输入符号用不了, String转 double 本来就有错误,你可以用我的扩展成科学型的。

帮忙用java补齐制作计算器加减乘除等于小数点这几个运算符的代码

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.border.*;

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

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

/**

* java swing计算器

* @author young

*

*/

public class Calculator extends Frame implements ActionListener // 计算器类

{

JTextField result;

NumberButton numberButton[];

OperatorButton operatorButton[];

Button radixpoint, positiveminus, backspace, reciprocal, equal, clear; // 声明成员变量

// 小数点按钮,正负号按钮,退格按钮,求倒数按钮,等号按钮,清零按钮

Panel panel;

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

LinkedList linklist;

boolean pressequal = false;

public Calculator() // 构造方法

{

super("计算器");

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) // 数字按钮

{

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) // 等号按钮

{

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) // 小数点按钮

{

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) // 正负号按钮

{

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) // 求倒数按钮

{

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 // 数字按钮类

{

int number;

public NumberButton(int number) // 构造方法

{

super("" + number);

this.number = number;

setForeground(Color.blue);

}

public int getNumber() {

return number;

}

}

class OperatorButton extends Button // 运算符号按钮类

{

String operator;

public OperatorButton(String operator) // 构造方法

{

super(operator);

this.operator = operator;

setForeground(Color.red);

}

public String getOperator() {

return operator;

}

}

java 写计算器的退格键怎么写,如“123456”按下退格变“12345”

String str = "12345";

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

int a = Integer.parseInt(str);

谁能给一个Java的计算器程序代码(加注释 )

以前写的,给你了

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 {

//计算器面板

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

//输入面扳

private JPanel inputPanel = new JPanel();

//加减乘除面板

private JPanel operationPanel = new JPanel();

//数字面板

private JPanel buttonsPanel = new JPanel();

//输入数据文本框

private JTextField input = new JTextField(20);

//退格键

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

//清空

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

//删除

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

//四则运算符号键

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

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

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

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

//小数点

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

//等号

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

//数字键

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 = "";//保存第一个运算数字

private String operator = "";//保存运算符号

public static void main(String[] args) {

new Calculator();//new计算器实例

}

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

//注册各个组件监听事件

backspace.addMouseListener(new OperationMouseListener());

CE.addMouseListener(new OperationMouseListener());

C.addMouseListener(new OperationMouseListener());

decimal.addMouseListener(new OperationMouseListener());

equal.addMouseListener(new OperationMouseListener());

//注册四则运算监听

add.addMouseListener(new CalcMouseListener());

sub.addMouseListener(new CalcMouseListener());

mul.addMouseListener(new CalcMouseListener());

div.addMouseListener(new CalcMouseListener());

//注册数字监听事件

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")){//如果文本框已经是0,结果还是0

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

}else{//否则的话,把0添加到后面,譬如文本框是1,结果就为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{

//如果输入的是运算符号,保存第一个结果和运算符号

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){//退格键,删除一个直到没有字符删除

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,清空所有运算数字和符号

num1 = "";

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

input.setText("0");//CE--将文本框置为0

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

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

//如果按了小数点,如果文本框已经有小数点,不做任何操作,否则在结果后面加上小数点

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

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

}

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

//如果是等号

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

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

double result = 0D;

if(operator.equals("+")){//执行加法运算

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

}else if(operator.equals("-")){//减法运算

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

}else if(operator.equals("*")){//乘法运算

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

}else if(operator.equals("/")){//除法运算

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

}

//格式化最终结果,保留两位小数点

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

}

}

有没有完整的计算器Java代码,不要有错误的,急求

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{     //建一个Calculator类继承JFrame实现ActionListener

public static void main (String args[]){

Calculator calculator=new Calculator();     //创建对象calculator

calculator.setVisible(true);               //窗口可见

calculator.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  //关闭图标后结束程序

}

String keys []={"7","8","9","/","4","5","6","*","1","2","3","-","0",".","=","+"};//表示计算器的键的名字

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

JButton KEYS []=new JButton[keys.length];//创建键对象

JButton COMMAND[]=new JButton[command.length];//功能键对象

static JTextField resultSet=new JTextField();//建一个文本框

double resultNum;        //结果

String operator="";//运算符  ---------------------------

public Calculator() {//    构造函数

super();      

init();//初始化计算器

this.setBackground(Color.WHITE);//this代表用当前构造方法创建的对象

this.setTitle("简单计算器");    //窗口标题

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

this.pack(); //使组件大小合适

}

void init() {      //初始化计算器

resultSet.setHorizontalAlignment(JTextField.RIGHT);     //设置文本框内容向右对齐

resultSet.setBackground(Color.WHITE);           //设置文本框背景颜色

JPanel keysPanel=new JPanel();//创建一个面板

keysPanel.setLayout(new GridLayout(4,5,3,3)); //设置面板的布局为网格型

for (int i=0;ikeys.length;i++) {      //把数字键和符号键设为按钮

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

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

}

JPanel comPanel=new JPanel();       //创建命令键面板

comPanel.setLayout(new GridLayout(1,2,3,3));//设置面板布局为网格型 

for(int i=0;icommand.length;i++) {    //把命令键设置为按钮

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

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

}

JPanel pane=new JPanel();      //创建面板

pane.setLayout(new BorderLayout());  //面板布局为区域型

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

pane.add(keysPanel,BorderLayout.CENTER); //把数字键面板加到大面板

getContentPane().setLayout(new BorderLayout());     //设置底层容器的布局为区域型

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

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

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

KEYS[i].addActionListener(this);//为按钮添加监听器

}

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));//将文本最后一个字符删除

}

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

resultSet.setText("0");

}

}

}


分享文章:计算器退格键java代码,计算器退格键java代码是多少
地址分享:http://wjwzjz.com/article/dsspoci.html
在线咨询
服务热线
服务热线:028-86922220
TOP