如何在Java Swing中的一个GUI中为多个按钮添加多个ActionListener

时间:2016-05-03 12:48:32

标签: java eclipse swing actionlistener

我正在使用Jadex进行模拟,这是基于Java的。我正在研究不同代理商之间的按摩广播。 我有一个带有两个按钮的GUI,一个是发送按钮,另一个是检查按钮。 它工作正常,当我按下发送按钮时,它将消息发送给其他代理。我的问题是我还要将检查按钮的结果发送给其他代理。 感谢您的帮助。 我的代理的GUI代码。

public class TranslationGuiBDI extends BobGuiBDI
{
    public TranslationGuiBDI(final IExternalAccess agent)
    {
        super(agent);
        JButton Checkes = new JButton("Checkes");
        JPanel p = new JPanel();//new BorderLayout());
        p.add(Checkes);//, BorderLayout.CENTER);
        getContentPane().add(p, BorderLayout.NORTH);
        Checkes.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                agent.scheduleStep(new IComponentStep<Void>()
                {
                    public IFuture<Void> execute(IInternalAccess ia)
                    {
                        IFuture<Collection<ITranslationService>>    transser    = ia.getComponentFeature(IRequiredServicesFeature.class).getRequiredServices("transser");
                        transser.addResultListener(new DefaultResultListener<Collection<ITranslationService>>()
                        {
                            public void resultAvailable(Collection<ITranslationService> result)
                            {
                                for(Iterator<ITranslationService> it=result.iterator(); it.hasNext(); )
                                {
                                    ITranslationService cs = it.next();
                                    cs.getUserProfile().addResultListener(new DefaultResultListener<UserProfile>()
                                    {
                                        public void resultAvailable(UserProfile result)
                                        {
                                            addMessage(result.toString());
                                        }
                                    });
                                }
                            }
                        });
                        return IFuture.DONE;
                    }
                });
            }
        });
    }

Bob的GUI:

public class BobGuiBDI extends JFrame {

    /** The textfield with received messages. */
    protected JTextArea received;

    /** The agent owning the gui. */
    protected IExternalAccess agent;

    //-------- constructors --------

    /**
     *  Create the user interface
     */
    public BobGuiBDI(IExternalAccess agent)
    {
        super(agent.getComponentIdentifier().getName());
        this.agent  = agent;
        this.setLayout(new BorderLayout());

        received = new JTextArea(10, 20);
        final JTextField message = new JTextField();
        JButton send = new JButton("send");

        JPanel panel = new JPanel(new BorderLayout());
        panel.add(message, BorderLayout.CENTER);
        panel.add(send, BorderLayout.EAST);

        getContentPane().add(received, BorderLayout.CENTER);
        getContentPane().add(panel, BorderLayout.SOUTH);

        send.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                final String text = message.getText(); 
                BobGuiBDI.this.agent.scheduleStep(new IComponentStep<Void>()
                {
                    public IFuture<Void> execute(IInternalAccess ia)
                    {
                        IIntermediateFuture<IlogSevice> fut = ia.getComponentFeature(IRequiredServicesFeature.class).getRequiredServices("chatservices");
                        fut.addResultListener(new IIntermediateResultListener<IlogSevice>()
                        {
                            public void resultAvailable(Collection<IlogSevice> result)
                            {
                                for(Iterator<IlogSevice> it=result.iterator(); it.hasNext(); )
                                {
                                    IlogSevice cs = it.next();
                                    try
                                    {
                                        cs.message(BobGuiBDI.this.agent.getComponentIdentifier().getName(), text,text);
                                    }
                                    catch(Exception e)
                                    {
                                        System.out.println("Could not send message to: "+cs);
                                    }
                                }
                            }

                            public void intermediateResultAvailable(IlogSevice cs)
                            {
                                System.out.println("found: "+cs);
                                cs.message(BobGuiBDI.this.agent.getComponentIdentifier().getName(), text,text);
                            }

                            public void finished()
                            {
                            }

                            public void exceptionOccurred(Exception exception)
                            {
                            }

                        });
                        return IFuture.DONE;
                    }
                });
            }
        });

        addWindowListener(new WindowAdapter()
        {
            public void windowClosed(WindowEvent e)
            {
                BobGuiBDI.this.agent.killComponent();
                BobGuiBDI.this.agent    = null;
            }
        });

        pack();
        setVisible(true);
    }

    /**
     *  Method to add a new text message.
     *  @param text The text.
     */
    public void addMessage(final String text)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                received.append(text+"\n");
            }
        });
    }

}

0 个答案:

没有答案