当前位置:首页 > 知识百科 > 正文内容

java购物车代码_java编写购物车

fpodo2个月前 (08-14)知识百科3

下面文件名要为:

JiSuanQi.java

import java.awt.*;

import java.awt.event.*;

public class JiSuanQi

{

String s="",s1=null,s2=null;

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

TextField tf=new TextField(30);

Panel p1=new Panel();

Panel p2=new Panel();

Panel p3=new Panel();

Button bt1=new Button("=");

Button bt2=new Button("删除");

Button[] bt=new Button[16];

int id=0;

public static void main(String[] args)

{

new JiSuanQi().init();

}

public void init()

{

f.setBackground(new Color(85,247,253));

f.setLayout(new BorderLayout(4,4));

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

p3.setLayout(new BorderLayout(4,4));

f.setResizable(false);

f.add(p1,BorderLayout.NORTH);

f.add(p2);

p3.add(bt2,BorderLayout.NORTH);

p3.add(bt1);

p1.add(tf);

f.add(p3,BorderLayout.EAST);

String[] b={"1","2","3","+","4","5","6","-","7","8","9","*","0",".","复位","/"};

for(int i=0;i<16;i++)

{

bt[i]=new Button(b[i]);

p2.add(bt[i]);

}

bt[0].setForeground(Color.blue);

bt[1].setForeground(Color.blue);

bt[2].setForeground(Color.blue);

bt[4].setForeground(Color.blue);

bt[5].setForeground(Color.blue);

bt[6].setForeground(Color.blue);

bt[8].setForeground(Color.blue);

bt[9].setForeground(Color.blue);

bt[10].setForeground(Color.blue);

bt[12].setForeground(Color.blue);

bt[13].setForeground(Color.blue);

bt[3].setForeground(Color.red);

bt[7].setForeground(Color.red);

bt[11].setForeground(Color.red);

bt[15].setForeground(Color.red);

bt[14].setForeground(Color.red);

bt1.setForeground(Color.red);

bt2.setForeground(Color.red);

f.pack();

f.setVisible(true);

f.addWindowListener(new WindowAdapter()

{

public void windowClosing(WindowEvent e)

{

System.exit(0);

}

}

);

bt[0].addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

s+=1;

s2+=1;

tf.setText(s);

}

}

);

bt[1].addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

s+=2;

s2+=2;

tf.setText(s);

}

}

);

bt[2].addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

s+=3;

s2+=3;

tf.setText(s);

}

}

);

bt[4].addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

s+=4;

s2+=4;

tf.setText(s);

}

}

);

bt[5].addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

s+=5;

s2+=5;

tf.setText(s);

}

}

);

bt[6].addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

s+=6;

s2+=6;

tf.setText(s);

}

}

);

bt[8].addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

s+=7;

s2+=7;

tf.setText(s);

}

}

);

bt[9].addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

s+=8;

s2+=8;

tf.setText(s);

}

}

);

bt[10].addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

s+=9;

s2+=9;

tf.setText(s);

}

}

);

bt[12].addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

s+=0;

s2+=0;

tf.setText(s);

}

}

);

bt[13].addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

s+='.';

s2+='.';

tf.setText(s);

}

}

);

bt[3].addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

s1=s;

s+='+';

id=1;

s2="";

tf.setText(s);

}

}

);

bt[7].addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

s1=s;

s+='-';

id=2;

s2="";

tf.setText(s);

}

}

);

bt[11].addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

s1=s;

s+='*';

id=3;

s2="";

tf.setText(s);

}

}

);

bt[14].addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

s="";

s2="";

tf.setText(s);

}

}

);

bt[15].addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

s1=s;

s+='/';

id=4;

s2="";

tf.setText(s);

}

}

);

bt1.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

if(id<1) ;

else{

s+='=';

double a=Double.parseDouble(s1);

double b=Double.parseDouble(s2);

double c=0;

switch(id)

{

case 1:c=a+b; break;

case 2:c=a-b; break;

case 3:c=a*b; break;

case 4:c=a/b; break;

}

s+=c;

tf.setText(s);

}

s="";s1="";s2="";id=0;

}

}

);

bt2.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

char[] c1;

char[] c2=new char[s.length()-1];

c1=s.toCharArray();

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

c2[i]=c1[i];

s=s.valueOf(c2);

if(id<1)

{

s1=s;

}

if(s2.length()>=1)

{

char[] c3;

char[] c4=new char[s2.length()-1];

c3=s2.toCharArray();

for(int i=0;i<s2.length()-1;i++)

c4[i]=c3[i];

s2=s2.valueOf(c4);

}

tf.setText(s);

}

}

);

}

}

相关文章

怎样给文档加密设置密码(怎样给文档加密设置密码保护)

1.选择自己要加密的文件夹,点击右键,选择添加到压缩文件,会弹出一个压缩文件名和参数的窗口 2.点击高级选项,然后选择保存文件安全数据,然后点击设置密码。 3.输入密码后选择确定,注意密码不要太长,以...

win8 我的电脑桌面不见了(windows8桌面我的电脑不见了)

1.恢复电脑桌面图标:先在电脑桌面空白处单击右键,进入个性化。然后在个性化页面,选择主题,下拉找到桌面图标设置。在弹出的桌面图标对话框中,勾选常用的图标,点击确定,即可显示桌面图标。2.先打开任务管理...

微信官网入口(2025微信官网入口)

企业微信后台通过腾讯企业微信官网进入就可以:https://work.weixin.qq.com/,登录时需要该账号的管理员扫码才可以哦。至于企业微信的管理,对于很多管理员来说,还是觉得挺疑惑的,比如...

开机蓝屏0xc000007b无法正常启动

通常是由引导设备问题、系统文件损坏或软件冲突引起的。以下是一些建议来解决这个问题:1. 重启电脑:有时候,重启电脑可以解决临时的问题。如果蓝屏问题只是偶尔出现,重启电脑可能足以解决问题。2. 检查硬盘...

个人怎么申请伤残鉴定(个人如何申请伤残鉴定)

对于道路伤残鉴定,一般是当事人委托律师事务所进行,律师事务所再委托有鉴定资质的司法鉴定机构做伤残鉴定,如果案件进入诉讼程序,伤残鉴定则在法院的主持下,由当事人双方共同指定有鉴定资质的法医中心进行鉴定。...

怎样将win7升级到win10(win7怎么样升级到win10)

Windows7系统如何升级为win10。嗯,这个是需要重新安装操作系统的。请在安装前先预备好备份好windows7系统下面你自己的个人数据备份到自己的移动硬盘上面。然后通过USB导的方式进行安装,...