我正在用Java编写程序,以允许用户查看通过网络发送/接收的DIS(分布式交互式仿真)。我目前已经设置了程序,以便它读取信息并将其显示在控制台中 - 只要它正在运行(即直到我点击“停止&#39”),它才会这样做。
我现在正在尝试创建一个GUI来向用户显示此信息。我有一个Gui.java
类,我还有一个EspduReceiver.java
类,这是我正在读取通过网络发送的DIS信息,并在控制台中显示它。
我现在想通过Gui类向用户显示我正在阅读我的程序的DIS信息。
我的Gui班目前看起来像这样:
package openDIS;
import java.applet.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Gui extends JFrame{
public Gui(){
setTitle("DIS Filter");
setSize(1000, 500);
setLocation (10, 10);
setDefaultCloseOperation(EXIT_ON_CLOSE);
initGui();
}
/*public quitButton(){
initGui();
} */
private void initGui(){
JFrame frame = new JFrame();
JPanel panel = new JPanel();
frame.getContentPane().add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("DIS Filter");
frame.setSize(1000, 500);
panel.setLayout(null);
/*Create a String to hold the PDU information to be displayed in the JTextArea */
String output = EspduReceiver.pdu;
/*Add a JTextArea to display the output DIS information */
//JTextArea output = new JTextArea();
panel.add(new JTextArea(output));
JButton quitButton = new JButton("Quit");
quitButton.setBounds(875, 400, 80, 30); /*Set the location of the button in the window, and its size */
quitButton.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
System.exit(0);
}
});
panel.add(quitButton);
setTitle("Quit");
//setSize(60,30); /*This line was overwriting the previously set values for the size of the window */
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args){ /* I probably don't need a main method here- I have one in EspduReceiver.java */
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run(){
Gui gui = new Gui();
gui.setVisible(true);
}
});
}
}
我的EspduReceiver类目前看起来像这样:
package openDIS;
import java.net.*;
import java.applet.*;
import java.awt.*;
import edu.nps.moves.disutil.*;
import edu.nps.moves.dis.*;
/*Receives PDUs from the network in IEEE format. */
public class EspduReceiver {
/*Max size of a PDU in binary format that we can receive. Outdated- PDUs can be larger- but this is a reasonable starting point */
public static final int MAX_PDU_SIZE = 8192;
public static void main(String args[]){
MulticastSocket socket;
DatagramPacket packet;
InetAddress address;
PduFactory pduFactory = new PduFactory();
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());
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. */
}
} /*end main */
} /*end class */
目前,当我尝试运行我的Gui.java类时,行String output = EspduReceiver.pdu;
出错了 - 它说:
线程中的异常" AWT-EventQueue-0" java.lang.Error:未解决的编译问题:pdu无法解析或不是字段"
我想知道这是因为我的pdu
变量是在我的EspduReceiver.java类中的try- catch
方法内创建的 - 因此在该代码块之外的任何内容都不可见?我已经尝试将它声明为EspduReceiver.java类中的全局变量,但这导致了几个新的错误......
有人可以告诉我如何将我的Gui.java类中的output
字符串的值设置为我的EspduReceiver.java类中的pdu
变量的值吗?或者我应该使用其他数据类型而不是字符串来保存它?基本上,如何通过使用我的Gui.java类在屏幕上显示pdu
变量的数据?
谢谢!
修改2014年4月15日
嘿,谢谢你的答案 - 我已经尝试了你的建议,但我在pdu = pduFactory.createPdu(packet.getData());
行上收到错误,上面写着:"无法对非提出静态引用静电场pdu。"我将public Pdu pdu;
更改为public static Pdu pdu;
,并且已经摆脱了该错误,但是当我尝试运行我的EspduReceiver.java类时,现在没有任何反应。
我的Gui.java类现在编译,但是当我尝试运行它时,控制台会显示一条消息:"线程中的异常" AWT-EventQueue-0"显示java.lang.NullPointerException&#34 ;.它抱怨以下几行:
initGui();
String output = EspduReceiver.pdu.toString();
Gui gui = new Gui();
答案 0 :(得分:0)
如果您输入EspduReceiver.pdu
,则表示您希望获得pdu
类EspduReceiver
字段的值。但是这门课没有这样的领域。所以,只需添加它:
public class EspduReceiver {
public static final int MAX_PDU_SIZE = 8192;
public static Pdu pdu;
...
}
并替换它:
Pdu pdu = pduFactory.createPdu(packet.getData());
有了这个:
pdu = pduFactory.createPdu(packet.getData());
但是稍后,在GUI类中,您应该获得String
pdu
的内容。我从来没有遇到过这个类,但是如果它是一个描述为here的类,我认为你可以通过以下方式获取数据:
String output = EspduReceiver.pdu.toString();