JADE的booktrading中GUI套用解析.doc_第1页
JADE的booktrading中GUI套用解析.doc_第2页
JADE的booktrading中GUI套用解析.doc_第3页
JADE的booktrading中GUI套用解析.doc_第4页
JADE的booktrading中GUI套用解析.doc_第5页
已阅读5页,还剩2页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

内容摘要: . &.那末某个例子看不懂,便览java的读书还要加强一点. &. jade的bookTrading中关于于Agent与gui应用界面结合的例子,实则gui在jade中的套用多么简约,主要是要把jade的Agent类当成一个平常类来应用就足以了。那末某个例子看不懂,便览java的读书还要加强一点。现在我来解析一下,主要是对准BoolSellerAgent类材料和BookSellerGui类文书。大家看BookSellerGui.java的主要框架:import java.awt.*;/表明要用哪些类来兑现gui界面,import java.awt.event.*;import javax.swing.*;/*author Giovanni Caire - TILAB*/class BookSellerGui extends JFrame private BookSellerAgent myAgent;/该Agent作为一个分子,所以在添加图书的时机要调用该事物成员的诀要 private JTextField titleField, priceField; BookSellerGui(BookSellerAgent a) super(a.getLocalName(); myAgent = a;/Agent成员初始化 JPanel p = new JPanel(); p.setLayout(new GridLayout(2, 2); p.add(new JLabel(Book title:); titleField = new JTextField(15); p.add(titleField); p.add(new JLabel(Price:); priceField = new JTextField(15); p.add(priceField); getContentPane().add(p, BorderLayout.CENTER); JButton addButton = new JButton(Add); addButton.addActionListener( new ActionListener() public void actionPerformed(ActionEvent ev) try String title = titleField.getText().trim(); String price = priceField.getText().trim(); myAgent.updateCatalogue(title, Integer.parseInt(price);/调用该成员的法子对图书数据进行更新 titleField.setText(); priceField.setText(); catch (Exception e) JOptionPane.showMessageDialog(BookSellerGui.this, Invalid values. +e.getMessage(), Error, JOptionPane.ERROR_MESSAGE); ); p = new JPanel(); p.add(addButton); getContentPane().add(p, BorderLayout.SOUTH); / Make the agent terminate when the user closes / the GUI using the button on the upper right corner addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e) myAgent.doDelete();/同样是调用某个方法,干什么的自己猜猜 ); setResizable(false); public void show() pack(); Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); int centerX = (int)screenSize.getWidth() / 2; int centerY = (int)screenSize.getHeight() / 2; setLocation(centerX - getWidth() / 2, centerY - getHeight() / 2); super.show(); 然后是卖书者文件的框架:package examples.bookTrading;import jade.core.Agent;import jade.core.behaviours.*;import jade.lang.acl.ACLMessage;import jade.lang.acl.MessageTemplate;import jade.domain.DFService;import jade.domain.FIPAException;import jade.domain.FIPAAgentManagement.DFAgentDescription;import jade.domain.FIPAAgentManagement.ServiceDescription;import java.util.*;public class BookSellerAgent extends Agent / The catalogue of books for sale (maps the title of a book to its price)private Hashtable catalogue;/ The GUI by means of which the user can add books in the catalogueprivate BookSellerGui myGui;/经意看是个gui界面类的成员/ Put agent initializations hereprotected void setup() / Create the catalogue catalogue = new Hashtable(); / Create and show the GUI myGui = new BookSellerGui(this); myGui.show();/这两句发明当该Agent类一运转,就调用该界面对象的show方法显示出来 / Register the book-selling service in the yellow pages DFAgentDescription dfd = new DFAgentDescription(); dfd.setName(getAID(); ServiceDescription sd = new ServiceDescription(); sd.setType(book-selling); sd.setName(JADE-book-trading); dfd.addServices(sd); try DFService.register(this, dfd); catch (FIPAException fe) fe.printStackTrace(); / Add the behaviour serving queries from buyer agents addBehaviour(new OfferRequestsServer(); / Add the behaviour serving purchase orders from buyer agents addBehaviour(new PurchaseOrdersServer();/ Put agent clean-up operations hereprotected void takeDown() / Deregister from the yellow pages try DFService.deregister(this); catch (FIPAException fe) fe.printStackTrace(); / Close the GUI myGui.dispose(); / Printout a dismissal message System.out.println(Seller-agent +getAID().getName()+ terminating.);/* This is invoked by the GUI when the user adds a new book for sale */public void updateCatalogue(final String title, final int price) addBehaviour(new OneShotBehaviour() public void action() catalogue.put(title, new Integer(price); System.out.println(title+ inserted into catalogue. Price = +price); ); /* Inner class OfferRequestsServer. This is the behaviour used by Book-seller agents to serve incoming requests for offer from buyer agents. If the requested book is in the local catalogue the seller agent replies with a PROPOSE message specifying the price. Otherwise a REFUSE message is sent back. */ private class OfferRequestsServer extends CyclicBehaviour public void action() MessageTemplate mt = MessageTemplate.MatchPerformative(ACLMessage.CFP); ACLMessage msg = myAgent.receive(mt); if (msg != null) / CFP Message received. Process it String title = msg.getContent(); ACLMessage reply = msg.createReply(); Integer price = (Integer) catalogue.get(title); if (price != null) / The requested book is available for sale. Reply with the price reply.setPerformative(ACLMessage.PROPOSE); reply.setContent(String.valueOf(Value(); else / The requested book is NOT available for sale. reply.setPerformative(ACLMessage.REFUSE); reply.setContent(not-available); myAgent.send(reply); else block(); / End of inner class OfferRequestsServer /* Inner class PurchaseOrdersServer. This is the behaviour used by Book-seller agents to serve incoming offer acceptances (i.e. purchase orders) from buyer agents. The seller agent removes the purchased book from its catalogue and replies with an INFORM message to notify the buyer that the purchase has been sucesfully completed. */ private class PurchaseOrdersServer extends CyclicBehaviour public void action() MessageTemplate mt = MessageTemplate.MatchPerformative(ACLMessage.ACCEPT_PROPOSAL); ACLMessage msg = myAgent.receive(mt); if (msg != null) / ACCEPT_PROPOSAL Message received. Process it String title = msg.getContent(); ACLMessage reply = msg.createReply(); Integer price = (Integer) catalogue.remove(title); if (price != null) reply.setPerformative(ACLMessage.INFORM); System.out.println(title+ sold to agent +msg.getSender().getName(); else / The requested book has been sold to another buyer in the meanwhile . reply.setPerformative(ACLMessage.FAILURE); reply.setContent(not-available); myAgent.send(reply); else block(); / End of inner class OfferRequestsServer两个类相互调用以使用gui界面的精神意思我方才断句出来了,其实就跟平常写的java程序是一样的。这下知道gui在jade中怎么用了八。 作者提交的简介: .那末某个例子看不懂,便览java的读书还要加强一点.大家看BookSellerGui.java的主要框架: import java.awt.*;/

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论