从Java事件中访问全局对象



我的Java经验很少,而且我似乎找不到任何东西来解释如何解决我的问题;我已经试了好几个小时了。

我正在使用Phidgets RFID Java库(http://www.phidgets.com/programming_resources.php)和JLayer,目的是根据传感器范围内的RFID标签播放不同的mp3文件。一旦RFID标签不在范围内,游戏需要立即停止。

mp3类:
// Import the JLayer classes
import javazoom.jl.player.*;
// Import the Java classes
import java.io.*;
public class mp3 {
    private Player player;
    private InputStream is;
    /** Creates a new instance of MP3Player */
    public mp3() 
    {
        //
    }
    public void play( String filename )
    {
        try
        {
            // Create an InputStream to the file
            is = new FileInputStream( filename );
        }
        catch( Exception e )
        {
            e.printStackTrace();
        }
        try
        {
            player = new Player( is );
            PlayerThread pt = new PlayerThread();
            pt.start();
        }
        catch( Exception e )
        {
            e.printStackTrace();
        }
    }
    public void stop()
    {
        player.close();
    }
    class PlayerThread extends Thread
    {
        public void run()
        {
            try
            {
                player.play();
            }
            catch( Exception e )
            {
                e.printStackTrace();
            }
        }
    }
}

和其他所有事情发生的类:

import com.phidgets.*;
import com.phidgets.event.*;
public class ParrotDJ
{        
    public static final void main(String args[]) throws Exception {
            RFIDPhidget rfid;
            mp3 song = new mp3();
    System.out.println(Phidget.getLibraryVersion());
    rfid = new RFIDPhidget();
    rfid.addAttachListener(new AttachListener() {
        public void attached(AttachEvent ae)
        {
            try
            {
                ((RFIDPhidget)ae.getSource()).setAntennaOn(true);
                ((RFIDPhidget)ae.getSource()).setLEDOn(true);
            }
            catch (PhidgetException ex) { }
            System.out.println("attachment of " + ae);
        }
    });
    rfid.addDetachListener(new DetachListener() {
        public void detached(DetachEvent ae) {
            System.out.println("detachment of " + ae);
        }
    });
    rfid.addErrorListener(new ErrorListener() {
        public void error(ErrorEvent ee) {
            System.out.println("error event for " + ee);
        }
    });
    rfid.addTagGainListener(new TagGainListener()
    {
        public void tagGained(TagGainEvent oe)
        {
            //System.out.println(oe);
                            if(oe.getValue().equals("0107ee6ed5") || oe.getValue().equals("0107ee75d5"))
                            {
                                System.out.println("Amanda Palmer - Leeds United");
                                song.play("leedsunited.mp3");
                            }else if(oe.getValue().equals("0107ee82c7") || oe.getValue().equals("0107ee89f0"))
                            {
                                System.out.println("Paolo Nutini - 10/10");
                                song.play("1010.mp3");
                            }else if(oe.getValue().equals("0107ee8644") || oe.getValue().equals("0107ee6ff2"))
                            {
                                System.out.println("Mozart - Eine Kleine Nachtmusik");
                                song.play("einekleinenachtmusik.mp3");
                            }
        }
    });
    rfid.addTagLossListener(new TagLossListener()
    {
        public void tagLost(TagLossEvent oe)
        {
            //System.out.println(oe);
                            System.out.println("Stop");
                            song.stop();
        }
    });
    rfid.addOutputChangeListener(new OutputChangeListener()
    {
        public void outputChanged(OutputChangeEvent oe)
        {
            System.out.println(oe);
        }
    });
    rfid.openAny();
    System.out.println("waiting for RFID attachment...");
    rfid.waitForAttachment(1000);
    System.out.println("Serial: " + rfid.getSerialNumber());
    System.out.println("Outputs: " + rfid.getOutputCount());
    System.out.println("Outputting events.  Input to stop.");
    System.in.read();
    System.out.print("closing...");
    rfid.close();
    rfid = null;
    System.out.println(" ok");
    if (false) {
        System.out.println("wait for finalization...");
        System.gc();
    }
}
}

我想象有一个合乎逻辑的解决方案,我只是在努力让我的头脑在事件驱动的东西,和Java的面向对象。我查找了构建器模式,但我不知道如何将其应用于这种情况。

我不确定具体问题是否清楚,除非根据摘要。但是根据我的猜测,看起来您在内部类方法中访问类中定义的对象时遇到了问题。主要问题是,由于Java不知道字段的状态,因此不能访问非"final"字段。如果字段被设置为final,您应该能够访问事件方法

中的外部类字段。

如果您想从匿名类的方法中访问一个本地变量,如这里所示:

  rfid.addTagGainListener(new TagGainListener() {
            public void tagGained(TagGainEvent oe) {

其中new TagGainListener(){}创建一个匿名类。

你需要声明这个变量为final,只要你不想修改它就不会有任何问题。所以修改后的代码是:

import com.phidgets.*;
import com.phidgets.event.*;
public class ParrotDJ {
    public static final void main(String args[]) throws Exception {
        RFIDPhidget rfid;
        //  you need to make it final to access to it from anonymous classes as there rfid.addTagGainListener(new TagGainListener() {
        final mp3 song = new mp3();
        System.out.println(Phidget.getLibraryVersion());
        rfid = new RFIDPhidget();
        rfid.addAttachListener(new AttachListener() {
            public void attached(AttachEvent ae) {
                try {
                    ((RFIDPhidget) ae.getSource()).setAntennaOn(true);
                    ((RFIDPhidget) ae.getSource()).setLEDOn(true);
                } catch (PhidgetException ex) {
                }
                System.out.println("attachment of " + ae);
            }
        });
        rfid.addDetachListener(new DetachListener() {
            public void detached(DetachEvent ae) {
                System.out.println("detachment of " + ae);
            }
        });
        rfid.addErrorListener(new ErrorListener() {
            public void error(ErrorEvent ee) {
                System.out.println("error event for " + ee);
            }
        });
        rfid.addTagGainListener(new TagGainListener() {
            public void tagGained(TagGainEvent oe) {
                //System.out.println(oe);
                if (oe.getValue().equals("0107ee6ed5") || oe.getValue().equals("0107ee75d5")) {
                    System.out.println("Amanda Palmer - Leeds United");
                    song.play("leedsunited.mp3");
                } else if (oe.getValue().equals("0107ee82c7") || oe.getValue().equals("0107ee89f0")) {
                    System.out.println("Paolo Nutini - 10/10");
                    song.play("1010.mp3");
                } else if (oe.getValue().equals("0107ee8644") || oe.getValue().equals("0107ee6ff2")) {
                    System.out.println("Mozart - Eine Kleine Nachtmusik");
                    song.play("einekleinenachtmusik.mp3");
                }
            }
        });
        rfid.addTagLossListener(new TagLossListener() {
            public void tagLost(TagLossEvent oe) {
                //System.out.println(oe);
                System.out.println("Stop");
                song.stop();
            }
        });
        rfid.addOutputChangeListener(new OutputChangeListener() {
            public void outputChanged(OutputChangeEvent oe) {
                System.out.println(oe);
            }
        });
        rfid.openAny();
        System.out.println("waiting for RFID attachment...");
        rfid.waitForAttachment(1000);
        System.out.println("Serial: " + rfid.getSerialNumber());
        System.out.println("Outputs: " + rfid.getOutputCount());
        System.out.println("Outputting events.  Input to stop.");
        System.in.read();
        System.out.print("closing...");
        rfid.close();
        rfid = null;
        System.out.println(" ok");
        if (false) {
            System.out.println("wait for finalization...");
            System.gc();
        }
    }
}

相关内容

  • 没有找到相关文章

最新更新