您好,欢迎访问三七文档
importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;importjava.awt.Toolkit;publicclassVendingMachineimplementsActionListener{/**********************************************容器变量的定义********************************************/JFrameframe;JPanelpanelMain;//自动售货机的最外面的容器BoxboxControl1;JPanelpanelChooseProduct,panelControl2;JPanelpanelIn[]=newJPanel[9];JPanelpanelInSouth[]=newJPanel[9];JPanelpanelAdvertisement,panelProductout,panelTakeButton,panelTips,panelNull;JPanelpanelInserCoin=newJPanel();/************************************************Label的定义*********************************************/JLabellabelPrice[]=newJLabel[9];JLabellabelAdvertisement,labelProductOut,labelCoinout,labelNull;/***********************************************按钮变量的定义*******************************************/JButtonbuttonProduct[]=newJButton[9];//buttonProduct[i]用于显示商品图片JButtonbuttonTakeProduct,buttonHelp,buttonCopyright,buttonInsertCoin,buttonMoneyBack;JButtonbuttonQuit;/********************************************************************************************************/JTextFieldtextMoneyDisplay;//金额显示intstorage[]={4,4,4,4,4,4,4,4,4};//库存量定义,每个商品的初始库存量为4intprice[]={1,4,3,2,3,4,2,2,2};//商品价格定义intamount,errorcount,occupy;ImageIconproductImage[]=newImageIcon[9];//productImage[]为装商品图片对象的数组ImageIconimageBegin,imageCoinin,imageCoinout;ImageIconimageNull;//imageNull为售罄后显示的图片ImageIconadImage;//adImage[]为装广告图片对象的数组Timertimer;//限时操作计时器intptr=0;intptr2=0;/********************************************************************************************************初始化函数构建图形用户界面********************************************************************************************************/publicVendingMachine(){/***************************************************************************商品选购区界面设计panelChooseProduct为内置商品子容器panelIn[]的中间容器panelIn[]内含商品图片,价格****************************************************************************/panelChooseProduct=newJPanel(newGridLayout(3,3,5,5));productImage[0]=newImageIcon(image/11.jpg);productImage[1]=newImageIcon(image/12.jpg);productImage[2]=newImageIcon(image/13.jpg);productImage[3]=newImageIcon(image/14.jpg);productImage[4]=newImageIcon(image/15.jpg);productImage[5]=newImageIcon(image/16.jpg);productImage[6]=newImageIcon(image/17.jpg);productImage[7]=newImageIcon(image/18.jpg);productImage[8]=newImageIcon(image/19.jpg);for(inti=0;i9;i++){labelPrice[i]=newJLabel(¥+String.valueOf(price[i])+.0);//labelPrice[i]用与标记商品价格buttonProduct[i]=newJButton(productImage[i]);//buttonProduct[i]用于显示商品图片buttonProduct[i].addActionListener(this);buttonProduct[i].setBorder(BorderFactory.createLoweredBevelBorder());panelInSouth[i]=newJPanel();panelInSouth[i].add(labelPrice[i]);panelInSouth[i].setBorder(BorderFactory.createRaisedBevelBorder());panelIn[i]=newJPanel(newBorderLayout());//panelIn[]为panelChooseProduct内部子panelpanelIn[i].add(buttonProduct[i],BorderLayout.CENTER);panelIn[i].add(panelInSouth[i],BorderLayout.SOUTH);panelChooseProduct.add(panelIn[i]);panelInSouth[i].setBackground(Color.GRAY);}panelChooseProduct.setBorder(BorderFactory.createRaisedBevelBorder());imageNull=newImageIcon(image/null.JPG);//imageNull为售罄后显示的图片/*************************************************************************控制区一界面设计boxControl1内含图像区labelAdvertisement,金额显示框textMoneyDisplay,取货口panelProductout以及声明帮助和退出按钮**************************************************************************/buttonHelp=newJButton(帮助);buttonHelp.addActionListener(this);buttonCopyright=newJButton(声明);buttonCopyright.addActionListener(this);buttonQuit=newJButton(退出);buttonQuit.addActionListener(this);panelTips=newJPanel();panelTips.add(buttonHelp);panelTips.add(buttonCopyright);panelTips.add(buttonQuit);imageBegin=newImageIcon(image/begin.gif);//imageBegin为初始图片labelProductOut=newJLabel(imageBegin);//labelProductOut为取货口显示购买商品图片labelProductOut.setBorder(BorderFactory.createLoweredBevelBorder());buttonTakeProduct=newJButton(取货);//buttonTakeProduct为取货按钮buttonTakeProduct.addActionListener(this);panelProductout=newJPanel();panelProductout.add(labelProductOut);panelTakeButton=newJPanel();panelTakeButton.add(buttonTakeProduct);textMoneyDisplay=newJTextField(欢迎使用!请投币);//textMoneyDisplay为显示剩余金额的文本框textMoneyDisplay.setEditable(false);Fontmf=newFont(欢迎使用!请投币,Font.ITALIC,20);textMoneyDisplay.setFont(mf);textMoneyDisplay.setBackground(newColor(205,209,250));textMoneyDisplay.setBorder(BorderFactory.createLoweredBevelBorder());textMoneyDisplay.setHorizontalAlignment(SwingConstants.CENTER);textMoneyDisplay.setColumns(10);adImage=newImageIcon(image/000.gif);labelAdvertisement=newJLabel(adImage);//labelAdvertisement显示广告图片内容panelAdvertisement=newJPanel();panelAdvertisement.setBorder(BorderFactory.createLoweredBevelBorder());panelAdvertisement.add(labelAdvertisement);boxControl1=Box.createVerticalBox();//boxControl1为内置panelProductout,boxControl1.add(panelAdvertisement);boxControl1.add(Box.createVerticalStrut(10));boxControl1.add(textMoneyDisplay);boxControl1.add(Box.createVerticalStrut(30));boxControl
本文标题:自动售货机原代码
链接地址:https://www.777doc.com/doc-5213704 .html