2012-02-23 17 views
0

Javaのイベントに問題があります。私は2つのヒスイのクラス持っている:Java Jadeでイベントが機能しない

ファーストクラスを

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 catalogue 
    private BookSellerGui myGui; 

    // Put agent initializations here 
    protected void setup() { 
     // Create the catalogue 
     catalogue = new Hashtable(); 

     // Create and show the GUI 
     myGui = new BookSellerGui(this); 
     myGui.showGui(); 
     if(myGui.var==true) 
      System.out.println("it is work"); 
     // 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 here 
    protected 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(price.intValue())); 
       } 
       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 
} 

セカンドクラス

import jade.core.AID; 

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

/** 
    @author Giovanni Caire - TILAB 
*/ 
class BookSellerGui extends JFrame {  
     private BookSellerAgent myAgent; 

    private JTextField titleField, priceField; 
    boolean var; 

    BookSellerGui(BookSellerAgent a) { 
     super(a.getLocalName()); 

     myAgent = a; 

     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(""); 
        var=true; 
       } 
       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 showGui() { 
     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.setVisible(true); 
    } 
} 

問題は私がを表示するGUIで[追加]をクリックしたときに、「それがあるということです仕事 "。なぜそれは動作しません?

+0

これはStackOverflowに属します。すぐに移行されます。どのような種類の質問がServerFaultに適しているかの詳細については、http://serverfault.com/faq#questionsを参照してください。 –

+0

私はリスナーについても考えましたが、私はそれをどうやって行うことができるのか分かりません。それについてもっと詳しく教えてもらえますか? – Texicans

答えて

0

私はジェイドを知らないが、追加ボタンをクリックしたときに、あなたのコード内で、VARがtrueに設定されています。あなたは番目のクラスに行var=trueSystem.out.println("something")を置く場合は、期待どおりのボタンをクリックしたときにそれが起こることがわかります。

あなたは、このようなファーストクラスに変更した場合:

if(myGui.var==true) 
    System.out.println("it is work"); 
else 
    System.out.println("Add button has not been pressed yet"); 

を、あなたもその条件がテストされたときに、まだ追加ボタンをクリックする時間がなかったので、そのvarはまだ偽で表示されます。

ボタンをクリックすると、それが起こる適切な何かを実行しているとき、それは警告を取得しますように、最初のクラスが、第二のクラスに耳を傾ける持っているだろうこれを処理する適切な方法。

関連する問題