大橙子网站建设,新征程启航

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

java计算器退格代码 java计算器中退格删除

高分求 用JAVA编写计算器界面(只要界面)代码!

package importantpg;

在铜仁等地区,都构建了全面的区域性战略布局,加强发展的系统性、市场前瞻性、产品创新能力,以专注、极致的服务理念,为客户提供成都网站建设、做网站 网站设计制作定制网站设计,公司网站建设,企业网站建设,品牌网站设计,营销型网站,成都外贸网站建设,铜仁网站建设费用合理。

import java.awt.*;

import java.awt.event.*;

public class jisuanqi

{

TextArea tf=new TextArea(3,50 );

Button lb=new Button("clear");

Panel p1=new Panel();

Panel p2=new Panel();

public jisuanqi()

{ Frame f=new Frame("计算器");

Panel p=new Panel();

f.setBackground(Color.white);

f.setSize(500, 1500);

f.setVisible(true);

f.addWindowListener(new WindowAdapter()

{

public void windowClosing(WindowEvent e) {System.exit(1);}

});

Button[] b=new Button[18]; b[10]=new Button("-"); b[15]=new Button("(");

b[11]=new Button("+"); b[12]=new Button("*"); b[16]=new Button(")");

b[13]=new Button("/"); b[14]=new Button("="); b[17]=new Button("退格");

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

{

b[i]=new Button(""+i);

}

p.setLayout(new GridLayout(6,3));

avent aa=new avent();

lb.addActionListener(aa);

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

{

p.add(b[i]);b[i].addActionListener(aa);

}

p1.setLayout(new BorderLayout());

p1.add(lb,"North");p1.add(tf,"South");f.add(p1, "South");

f.add(p,"Center"); f.pack();

} //构造方法

public static void main(String args[])

{

new jisuanqi();

}

class avent implements ActionListener //事件体

{

public void actionPerformed(ActionEvent e)

{

String s=null; String in="";

s=e.getActionCommand();

if (s!="="s!=nulls!="clear"s!="退格")

{

tf.append(s);

in+=s; s=null;

}

else if(s=="clear")

{

tf.replaceRange("",0,1000);

}else if(s=="=")

{

tf.append("="+jisuan(tf.getText()));

}

else if(s=="退格") {}

}///////////////////////////////////////////////事件处理方法

}

///////////////////////////////////////计算过程//////////////

static String jisuan(String s)

{

String hs= "";

char[] ch = new char[s.length()];

int top = -1;

for (int i = 0; i s.length(); i++)

{

char rsc = s.charAt(i);

switch(rsc)

{

case ' ' : break;

case '+' : //操作符

case '-' :

while (top = 0) //栈不为空时

{

char pc = ch[top--]; //pop Operator

if (pc == '(')

{

ch[++top] = pc; //push Operator

break;

}

else {hs = hs + pc;}

}

ch[++top] = rsc; //push Operator

hs += " ";break;

case '*' : //操作符

case '/' :

while (top = 0) //栈不为空时

{

char c = ch[top--]; //pop Operator

if (c == '(')

{

ch[++top] = c; //push Operator

break;

}

else

{

if (c == '+' || c == '-')

{

ch[++top] = c; //push Operator

break;

}

else

{

hs = hs + c;

}

}

}

ch[++top] = rsc; //push Operator

hs += " ";

break;

case '(' : //操作符

ch[++top] = rsc;

hs+= " ";

break;

case ')' : //操作符

while (top = 0) //栈不为空时

{

char c = ch[top--]; //pop Operator

if (c == '(')

{

break;

}

else

{

hs= hs + c;

}

}

hs += " ";

break;

default : //操作数

hs= hs + rsc;

break;

}//switch

}/////////////////////////////for

while (top = 0)

{

hs= hs + ch[top--]; //pop Operator

}

//System.out.println("hs="+hs); //后缀

//////////////////////////////////////////////后缀////////////////////////////////////

//后缀表达式计算

double[] result = new double[hs.length()];

double x, y;

top = - 1;

String st = "";

for (int i = 0; i hs.length(); i++)

{

char c = hs.charAt(i);

if ((c = '0' c = '9') || c == '.')

{

st += c;

}

if ((c == ' ' st != "") || i == hs.length() - 1)

{

result[++top] = Double.parseDouble(st) ; //push Operands

st = "";

}

if (c == '+' || c == '-' || c == '*' || c == '/')

{

if ((st != ""))

{

result[++top] =Double.parseDouble(st) ; //push Operands

st = "";

}

y = result[top--];

x = result[top--];

switch (c)

{

case '+' :

result[++top] = x+y;break;//push 中间结果再次入栈

case '-' :

result[++top] = x-y;break;

case '*' :

result[++top] = x*y;break;

case '/' :

result[++top] = x/y;break;

default :break;

}

}////////////////////if

} ///////////////////////for

return String.valueOf(result[top]); //pop 最终结果

} /////////////////////////jisuan

}

怎么用JAVA编写一个计算器窗口,并实现他的功能,求给出代码,对的话一定高分回报,谢谢了。。。

JAVA 计算器程序

Cadev.java

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.util.LinkedList;

import java.text.NumberFormat;

import javax.swing.border.*;

public class Cadev extends JFrame implements ActionListener

{

Nbtn nbtn[];

Obtn obtn[];

Button dotbtn,PlusOrMinusbtn,Backspacebtn,

recbtn,equbtn,clbtn;

JPanel panel;

JTextField resultShow;

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

LinkedList link;

boolean sfcequ=false;

public Cadev()

{

super("计算器");

link=new LinkedList();

nbtn=new Nbtn[10];

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

{

nbtn[i]=new Nbtn(i);

nbtn[i].addActionListener(this);

}

obtn=new Obtn[4];

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

{

obtn[i]=new Obtn(Oper[i]);

obtn[i].addActionListener(this);

}

dotbtn=new Button(".");

PlusOrMinusbtn=new Button("+/-");

equbtn=new Button("=");

recbtn=new Button("1/x");

Backspacebtn=new Button("退格");

clbtn=new Button("C");

recbtn.setForeground(Color.blue);

dotbtn.setForeground(Color.blue);

PlusOrMinusbtn.setForeground(Color.blue);

equbtn.setForeground(Color.red);

Backspacebtn.setForeground(Color.red);

clbtn.setForeground(Color.red);

dotbtn.addActionListener(this);

PlusOrMinusbtn.addActionListener(this);

equbtn.addActionListener(this);

recbtn.addActionListener(this);

Backspacebtn.addActionListener(this);

clbtn.addActionListener(this);

resultShow=new JTextField(10);

resultShow.setHorizontalAlignment(JTextField.RIGHT);

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

resultShow.setBorder(new SoftBevelBorder(BevelBorder.LOWERED));

resultShow.setBackground(Color.white);

resultShow.setEditable(false);

panel=new JPanel();

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

panel.add(nbtn[1]);

panel.add(nbtn[2]);

panel.add(nbtn[3]);

panel.add(obtn[0]);

panel.add(clbtn);

panel.add(nbtn[4]);

panel.add(nbtn[5]);

panel.add(nbtn[6]);

panel.add(obtn[1]);

panel.add(Backspacebtn);

panel.add(nbtn[7]);

panel.add(nbtn[8]);

panel.add(nbtn[9]);

panel.add(obtn[2]);

panel.add(recbtn);

panel.add(nbtn[0]);

panel.add(PlusOrMinusbtn);

panel.add(dotbtn);

panel.add(obtn[3]);

panel.add(equbtn);

add(panel,BorderLayout.CENTER);

add(resultShow,BorderLayout.NORTH);

addWindowListener(new WindowAdapter()

{ public void windowClosing(WindowEvent e)

{

System.exit(0);

}

});

setVisible(true);

setBounds(100,50,240,180);

setResizable(false);

validate();

}

public void actionPerformed(ActionEvent e)

{

if(e.getSource() instanceof Nbtn)

{

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

if(link.size()==0)

{

int num=b.getNum();

link.add(""+num);

resultShow.setText(""+num);

sfcequ=false;

}

else if(link.size()==1sfcequ==false)

{

int num=b.getNum();

String Number=(String)link.getFirst();

String s=Number.concat(""+num);

link.set(0,s);

resultShow.setText(s);

}

else if(link.size()==1sfcequ==true)

{

int num=b.getNum();

link.removeFirst();

link.add(""+num);

sfcequ=false;

resultShow.setText(""+num);

}

else if(link.size()==2)

{

int num=b.getNum();

link.add(""+num);

resultShow.setText(""+num);

}

else if(link.size()==3)

{

int num=b.getNum();

String Number=(String)link.getLast();

String s=Number.concat(""+num);

link.set(2,s);

resultShow.setText(s);

}

}

else if(e.getSource() instanceof Obtn)

{

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

if(link.size()==1)

{

String sign=b.getOper();

link.add(sign);

}

else if(link.size()==2)

{

String sign=b.getOper();

link.set(1,sign);

}

else if(link.size()==3)

{

String sign=b.getOper();

String num1=(String)link.getFirst();

String num2=(String)link.getLast();

String Oper=(String)link.get(1);

try

{

double n1=Double.parseDouble(num1);

double n2=Double.parseDouble(num2);

double n=0;

if(Oper.equals("+"))

{

n=n1+n2;

}

else if(Oper.equals("-"))

{

n=n1-n2;

}

else if(Oper.equals("*"))

{

n=n1*n2;

}

else if(Oper.equals("/"))

{

n=n1/n2;

}

link.clear();

link.add(""+n);

link.add(sign);

resultShow.setText(""+n);

}

catch(Exception ee)

{

}

}

}

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

{

sfcequ=true;

if(link.size()==1||link.size()==2)

{

String num=(String)link.getFirst();

resultShow.setText(""+num);

}

else if(link.size()==3)

{

String num1=(String)link.getFirst();

String num2=(String)link.getLast();

String sign=(String)link.get(1);

try

{

double n1=Double.parseDouble(num1);

double n2=Double.parseDouble(num2);

double n=0;

if(sign.equals("+"))

{

n=n1+n2;

}

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

{

n=n1-n2;

}

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

{

n=n1*n2;

}

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

{

n=n1/n2;

}

resultShow.setText(""+n);

link.set(0,""+n);

link.removeLast();

link.removeLast();

}

catch(Exception ee)

{

}

}

}

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

{

if(link.size()==0)

{

sfcequ=false;

}

else if(link.size()==1)

{

String dot=dotbtn.getLabel();

String num=(String)link.getFirst();

String s=null;

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

{

s=num.concat(dot);

link.set(0,s);

}

else

{

s=num;

}

link.set(0,s);

resultShow.setText(s);

}

else if(link.size()==3)

{

String dot=dotbtn.getLabel();

String num=(String)link.getLast();

String s=null;

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

{

s=num.concat(dot);

link.set(2,s);

}

else

{

s=num;

}

resultShow.setText(s);

}

}

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

{

if(link.size()==1)

{

String num=(String)link.getFirst();

if(num.length()=1)

{

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

link.set(0,num);

resultShow.setText(num);

}

else

{

link.removeLast();

resultShow.setText("0");

}

}

else if(link.size()==3)

{

String num=(String)link.getLast();

if(num.length()=1)

{

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

link.set(0,num);

resultShow.setText(num);

}

else

{

link.removeLast();

resultShow.setText("0");

}

}

}

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

{

if(link.size()==1)

{

String num1=(String)link.getFirst();

try

{

double d=Double.parseDouble(num1);

d=-1*d;

String str=String.valueOf(d);

link.set(0,str);

resultShow.setText(str);

}

catch(Exception ee)

{

}

}

else if(link.size()==3)

{

String num2=(String)link.getLast();

try

{

double d=Double.parseDouble(num2);

d=-1*d;

String str=String.valueOf(d);

link.set(2,str);

resultShow.setText(str);

}

catch(Exception ee)

{

}

}

}

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

{

if(link.size()==1||link.size()==2)

{

String num1=(String)link.getFirst();

try

{

double d=Double.parseDouble(num1);

d=1.0/d;

String str=String.valueOf(d);

link.set(0,str);

resultShow.setText(str);

}

catch(Exception ee)

{

}

}

else if(link.size()==3)

{

String num2=(String)link.getLast();

try

{

double d=Double.parseDouble(num2);

d=1.0/d;

String str=String.valueOf(d);

link.set(0,str);

}

catch(Exception ee)

{

}

}

}

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

{

sfcequ=false;

resultShow.setText("0");

link.clear();

}

}

public static void main(String agrs[])

{

new Cadev();

}

}

Nbtn.java

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class Nbtn extends Button

{

int Num;

public Nbtn(int n)

{

super(""+n);

Num=n;

setForeground(Color.blue);

}

public int getNum()

{

return Num;

}

}

Obtn.java

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class Obtn extends Button

{

String Oper;

public Obtn(String s)

{

super(s);

Oper=s;

setForeground(Color.red);

}

public String getOper()

{

return Oper;

}

}

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

String str = "12345";

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

int a = Integer.parseInt(str);

java编写计算器实现退格,倒数,小数点,正负号功能代码

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

class General extends JFrame implements ActionListener

{

String args[]={"退格","总清","清零","/","1","2","3","+","4","5","6","-","7","8","9","*","0","+/-",".","="};

JButton jb[]=new JButton[20];

JPanel p1=new JPanel();

JTextField jtf=new JTextField(15);

double num1,num2,jieguo;

char c,ch;

public General()

{

Container ct=getContentPane();

jtf.addKeyListener(new KA());

jtf.setHorizontalAlignment(JTextField.RIGHT);

ct.add(jtf,"North");

p1.setLayout(new GridLayout(5,4));

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

{

jb[i]=new JButton(args[i]);

jb[i].addActionListener(this);

p1.add(jb[i]);

}

ct.add(p1);

pack();

setVisible(true);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

public static void main(String args[])

{

new General();

}

public void actionPerformed(ActionEvent e)// 监听按键

{

String str=e.getActionCommand().trim();

if("0123456789.".indexOf(str)!=-1)

{

if(jtf.getText().equals("0"))jtf.setText(null);

jtf.setText(jtf.getText()+str);

return;

}

if("+-*/".indexOf(str)!=-1)

{

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

c=str.charAt(0);

jtf.setText(null);

return;

}

else if(str.equals("="))

{

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

switch(c)

{

case '+': jieguo=num1+num2; break;

case '-': jieguo=num1-num2; break;

case '*': jieguo=num1*num2; break;

case '/': jieguo=num1/num2; break;

}

jtf.setText(Double.toString(jieguo));

}

else if(str.equals("+/-"))

{

if(jtf.getText().isEmpty()==false)

{

Double lnum=-(Double.parseDouble(jtf.getText()));

jtf.setText(Double.toString(lnum));

}

else

{

jtf.setText("-");

}

}

else if(str.equals("清空"))

{

jtf.setText(null);

}

else if(str.equals("清零"))

{

jtf.setText("0");

}

else if(str.equals("退格"))

{

jtf.setText(jtf.getText().substring(0,jtf.getText().length()-1));

}

}

class KA extends KeyAdapter // 监听键盘

{

public void keyPressed(KeyEvent e)

{

char ch=e.getKeyChar();

if("+-*/".indexOf(ch)!=-1)

{

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

c=ch;

}

if(ch=='='|ch==e.VK_ENTER)

{

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

switch(c)

{

case '+': jieguo=num1+num2; break;

case '-': jieguo=num1-num2; break;

case '*': jieguo=num1*num2; break;

case '/': jieguo=num1/num2; break;

}

}

}

public void keyReleased(KeyEvent e)

{

char ch=e.getKeyChar();

if("+-*/".indexOf(ch)!=-1)

{

jtf.setText(null);

}

if(ch=='='|e.getKeyCode()==e.VK_ENTER)

{

jtf.setText(Double.toString(jieguo));

}

}

}

}


网页题目:java计算器退格代码 java计算器中退格删除
网站网址:http://dzwzjz.com/article/dosjosd.html
在线咨询
服务热线
服务热线:028-86922220
TOP