繁体   English   中英

Java-如何将通过公共静态void方法读取到程序中的数据保存到String中?

[英]Java- how do I save data that is being read into my program by a public static void method into a String?

我正在编写一个Java程序,该程序将侦听通过网络发送的PDU,并将有关这些PDU的信息(例如仿真中的实体状态和位置)呈现给用户。

目前,我有一个用于监听通过网络发送的PDU的类(EspduReceiver.java),另一类是我用来创建供用户与程序交互的GUI的类(Gui.java) )。

EspduReceiver.java类当前可以按预期工作,当我运行它时,它侦听通过网络发送的所有PDU,并在控制台中显示有关它接收的每个PDU的信息。 它显示的信息包括实体ID及其在模拟中的位置。

但是,此方法实际上只是侦听PDU消息,并在“听到”消息时将有关它的相关信息打印到控制台,因此,正在控制台中打印的有关实体状态PDU的信息实际上并未存储在任何地方。目前在程序中。

现在,我想对Gui.java类进行的操作是创建一个用户界面,该界面将向用户显示我在控制台中看到的内容。 我已经写了一个可以做到这一点的类的基础,但是我很难从EspduReceiver.java类检索正在读入程序的数据。

我目前用于检索PDU信息并将其显示在控制台中的方法如下所示:

public static void receivePdu(){

    try{
        /*Specify the socket to receive the data */
        socket = new MulticastSocket(EspduSender.PORT);
        address = InetAddress.getByName(EspduSender.DEFAULT_MULTICAST_GROUP);
        socket.joinGroup(address); 

        /*Loop infinitely, receiving datagrams */
        while(true){
            byte buffer[] = new byte[MAX_PDU_SIZE];
            packet = new DatagramPacket(buffer, buffer.length);

            socket.receive(packet);

            Pdu pdu = pduFactory.createPdu(packet.getData()); /*    Commented on 15/04/2014 @ 09:15 */

            if(pdu != null){
                System.out.print("Got PDU of type: " + pdu.getClass().getName());
                if(pdu instanceof EntityStatePdu){
                    EntityID eid = ((EntityStatePdu)pdu).getEntityID();
                    Vector3Double position = ((EntityStatePdu)pdu).getEntityLocation();
                    System.out.print(" EID:[" + eid.getSite() + ", " + eid.getApplication() + ", " + eid.getEntity() + "] ");
                    System.out.print(" Location in DIS coordinates: [" + position.getX() + ", " + position.getY() + ", " + position.getZ() + "]");
                } else if(!(pdu instanceof EntityStatePdu)){
                    System.out.println("There are no PDUs currently being received.");
                }
                System.out.println();
            }
        } /*end while */
    } /*end try */
    catch(Exception e){
        System.out.println(e);
        e.printStackTrace();
        System.out.println("This is where the error is being generated");
        /*09/04/2014 @ 17:100
         * If this exception gets called, presumably it either means that pdu is not an instance of EntityStatePdu, or
         * that pdu does not actually hold a packet.  */
    }
}

然后,在我的Gui.java类中,我有一个public void initGui(){...}方法,我试图使用该方法从EspduReceiver.java调用receivePdu()方法。 为此,我声明了一个EspduReceiver类的新实例,并创建了一个新的String变量来保存receivePdu()方法生成的数据:

EspduReceiver receiver = new EspduReceiver();
String data = receiver.receivePdu();

但是,我在String data = receiver.receivePdu();上遇到错误String data = receiver.receivePdu(); 这行说:“类型不匹配:无法从void转换为String”,并建议我将receivePdu()返回类型更改为String ...

我知道我收到错误的原因-因为我的receivePdu()方法的返回类型为“ void”,在这里我试图指出它是String-我本来尝试用返回类型编写receivePdu方法的字符串,但始终会出错,并且只能通过将其更改为void来解决。

有没有一种方法可以访问receivePdu()方法检索的数据,从而可以将其显示给用户? 我该怎么做? 我可以将它检索的数据保存到另一个变量中,并用它来显示给用户吗?

您不能从GUI上有效地调用receivePdu()方法,因为receivePdu()永远不会返回-它会永远循环-而且不会返回任何内容。

创建新的EspduReceiver实例将无济于事,因为receivePdu方法是静态的,即被所有实例共享。

您可以获取receivePdu()来将数据存储在EspduReceiver顶层的变量中,该变量对GUI可见,然后通知GUI应该收集数据并显示它。 或者,您可以从receivePdu内将数据推送到GUI,尽管这receivePdu您的所有代码有些receivePdu

无论哪种方式,您都需要从“事件调度线程”中通知GUI-请参见此处教程

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM