importjava.awt.*importjava.awt.event.*classShopFrameextendsFrameimplementsActionListener{Labellabel1,label2,label3,label4Buttonbutton1,button2,button3,button4,button5TextAreatextPanelpanel1,panel2staticfloatsum=0.0fShopFrame(Strings){super(s)setLayout(newBorderLayout())label1=newLabel("面纸:3元",Label.LEFT)label2=newLabel("钢笔:5元",Label.LEFT)label3=newLabel("书:10元",Label.LEFT)label4=newLabel("袜子:8元",Label.LEFT)button1=newButton("加入购物车")button2=newButton("加入购物车")button3=newButton("加入购物车")button4=newButton("加入购物车")button5=newButton("查看购物车")text=newTextArea("商品有:"+"\n",5,10)text.setEditable(false)addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){System.exit(0)}})button1.addActionListener(this)button2.addActionListener(this)button3.addActionListener(this)button4.addActionListener(this)button5.addActionListener(this)panel1=newPanel()panel2=newPanel()panel1.add(label1)panel1.add(button1)panel1.add(label2)panel1.add(button2)panel1.add(label3)panel1.add(button3)panel1.add(label4)panel1.add(button4)panel2.setLayout(newBorderLayout())panel2.add(button5,BorderLayout.NORTH)panel2.add(text,BorderLayout.SOUTH)this.add(panel1,BorderLayout.CENTER)this.add(panel2,BorderLayout.SOUTH)setBounds(100,100,350,250)setVisible(true)validate()}publicvoidactionPerformed(ActionEvente){if(e.getSource()==button1){text.append("一个面纸、")sum=sum+3}elseif(e.getSource()==button2){text.append("一只钢笔、")sum=sum+5}elseif(e.getSource()==button3){text.append("一本书、")sum=sum+10}elseif(e.getSource()==button4){text.append("一双袜子、")sum=sum+8}elseif(e.getSource()==button5){text.append("\n"+"总价为:"+"\n"+sum)}}}publicclassShopping{publicstaticvoidmain(String[]args){newShopFrame("购物车")}}我没用Swing可能显示不出来你的效果。不满意得话我在给你编一个。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)