简体   繁体   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?

I am writing a Java program that will listen for PDUs that are being sent across a network, and present information about those PDUs (such as entity state & position within the simulation) to the user. 我正在编写一个Java程序,该程序将侦听通过网络发送的PDU,并将有关这些PDU的信息(例如仿真中的实体状态和位置)呈现给用户。

I currently have one class which is being used to listen for the PDUs being sent over the network (EspduReceiver.java), and another class that I am using to create the GUI that the user will use to interact with the program (Gui.java). 目前,我有一个用于监听通过网络发送的PDU的类(EspduReceiver.java),另一类是我用来创建供用户与程序交互的GUI的类(Gui.java) )。

The EspduReceiver.java class currently works as intended- when I run it, it listens for any PDUs that are being sent over the network, and displays information about each individual PDU it receives in the console. EspduReceiver.java类当前可以按预期工作,当我运行它时,它侦听通过网络发送的所有PDU,并在控制台中显示有关它接收的每个PDU的信息。 The information that it displays includes the entity ID, and its position within the simulation. 它显示的信息包括实体ID及其在模拟中的位置。

However, this method literally just listens for a PDU message, and prints the relevant information about it to the console when it 'hears' one- so the information about the Entity State PDU that is being printed in the console, is not actually stored anywhere in the program at present. 但是,此方法实际上只是侦听PDU消息,并在“听到”消息时将有关它的相关信息打印到控制台,因此,正在控制台中打印的有关实体状态PDU的信息实际上并未存储在任何地方。目前在程序中。

Now, what I would like to do with my Gui.java class, is to create a user interface that will display what I am seeing being printed in the console to the user. 现在,我想对Gui.java类进行的操作是创建一个用户界面,该界面将向用户显示我在控制台中看到的内容。 I have written the foundation of a class that will do this, but I am having trouble retrieving the data that is being read into my program from the EspduReceiver.java class... 我已经写了一个可以做到这一点的类的基础,但是我很难从EspduReceiver.java类检索正在读入程序的数据。

The method that I am using to retrieve the PDU information, and display it in the console currently looks like this: 我目前用于检索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.  */
    }
}

Then, in my Gui.java class, I have a public void initGui(){...} method, which I am trying to use to call the receivePdu() method from EspduReceiver.java. 然后,在我的Gui.java类中,我有一个public void initGui(){...}方法,我试图使用该方法从EspduReceiver.java调用receivePdu()方法。 I have done this by declaring a new instance of my EspduReceiver class, and creating a new String variable to hold the data generated by the receivePdu() method: 为此,我声明了一个EspduReceiver类的新实例,并创建了一个新的String变量来保存receivePdu()方法生成的数据:

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

However, I get an error on the String data = receiver.receivePdu(); 但是,我在String data = receiver.receivePdu();上遇到错误String data = receiver.receivePdu(); line that says: "Type mismatch: cannot convert from void to String", and suggests that I change return type of receivePdu() to String... 这行说:“类型不匹配:无法从void转换为String”,并建议我将receivePdu()返回类型更改为String ...

I understand the reason that I get the error- because my receivePdu() method has a return type of "void", and here I am trying to state that it is String- I had originally tried to write the receivePdu method with a return type of String, but kept getting errors with that, and couldn't fix it other than by changing it to void. 我知道我收到错误的原因-因为我的receivePdu()方法的返回类型为“ void”,在这里我试图指出它是String-我本来尝试用返回类型编写receivePdu方法的字符串,但始终会出错,并且只能通过将其更改为void来解决。

Is there a way that I can access the data retrieved by the receivePdu() method, so that I can display it to the user? 有没有一种方法可以访问receivePdu()方法检索的数据,从而可以将其显示给用户? How would I do this? 我该怎么做? Could I possibly save the data that it retrieves to another variable, and use that to display it to the user? 我可以将它检索的数据保存到另一个变量中,并用它来显示给用户吗?

You can't usefully call the receivePdu() method from your GUI, because receivePdu() never returns - it loops forever - and doesn't return anything. 您不能从GUI上有效地调用receivePdu()方法,因为receivePdu()永远不会返回-它会永远循环-而且不会返回任何内容。

Creating a new instance of EspduReceiver won't help, since the receivePdu method is static ie shared by all instances. 创建新的EspduReceiver实例将无济于事,因为receivePdu方法是静态的,即被所有实例共享。

You could get receivePdu() to store data in a variable at the top level of EspduReceiver, visible to the GUI, then notify the GUI that it should collect the data and display it. 您可以获取receivePdu()来将数据存储在EspduReceiver顶层的变量中,该变量对GUI可见,然后通知GUI应该收集数据并显示它。 Or you could push the data to the GUI from within receivePdu , though that tangles all your code up somewhat. 或者,您可以从receivePdu内将数据推送到GUI,尽管这receivePdu您的所有代码有些receivePdu

Either way, you will need to notify the GUI from the "Event Dispatching Thread" - see the tutorial here . 无论哪种方式,您都需要从“事件调度线程”中通知GUI-请参见此处教程

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

相关问题 Java 博士 - 静态错误:此类没有接受 String[] 的静态 void main 方法 - Dr Java- Static Error: This class does not have a static void main method accepting String[] 如何从一个方法,另一个类的变量数据中读取公共静态变量并获取更新的数据 - How do I read in a public static, with variable data from a method, from another class and get updated data 如何在 Java 中调用公共 static void main(String[] args) class 中的方法? - How to call a method inside the public static void main(String[] args) class in Java? 我在哪里添加公共 static void main(String[] args) - where do i add the public static void main(String[] args) 如何使用 JUnit 5 测试 Java 中的 Public Static Void Main 方法? - How to test the Public Static Void Main method in Java using JUnit 5? 我应该把 public static void main(String[] args) 放在哪里? - Where do I put public static void main(String[] args)? java - 如何在我的主要方法中实现公共无效方法 - How can I implement a public void method in my main method in java 如何在Java中调用我的void方法? - how do I invoke my void method in Java? Java-如何将数字字符串转换为字母数组 - Java- How do i convert a string of numbers to an Array of letters 我如何称呼另一个类中的公共静态void。 - How do I call a public static void that is in a different class.
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM