在Java JFrame中显示通过网络接收的信息
我正在用Java编写一个程序,允许用户查看通过网络发送/接收的DIS(分布式交互模拟)。我目前已经设置了程序,以便它读取信息,并在控制台中显示它-只要它在运行(即,直到我单击“停止”)它就会这样做 我现在正试图创建一个GUI来向用户显示这些信息。我有一个在Java JFrame中显示通过网络接收的信息,java,swing,class,user-interface,Java,Swing,Class,User Interface,我正在用Java编写一个程序,允许用户查看通过网络发送/接收的DIS(分布式交互模拟)。我目前已经设置了程序,以便它读取信息,并在控制台中显示它-只要它在运行(即,直到我单击“停止”)它就会这样做 我现在正试图创建一个GUI来向用户显示这些信息。我有一个Gui.java类,还有一个EspduReceiver.java类,在这里我读取通过网络发送的DIS信息,并将其显示在控制台中 现在,我想通过Gui类向用户显示正在读取到程序中的DIS信息 我的Gui类当前如下所示: package openDI
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);
}
});
}
}
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 */
我的EspduReceiver课程目前看起来是这样的:
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);
}
});
}
}
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中出现异常。错误:未解决的编译问题:pdu无法解决或不是字段
我想知道这是否是因为我的pdu
变量是在我的espdureciver.java类中的try-catch
方法中创建的,因此对该代码块之外的任何东西都不可见?我尝试在espdureciver.java类中将其声明为全局变量,但这导致了几个新的错误
有人能告诉我如何将Gui.java类中的输出
字符串的值设置为espdureciver.java类中的pdu
变量的值吗?或者我应该使用另一种数据类型而不是字符串来保存它吗?基本上,我如何通过使用我的Gui.java类
谢谢
编辑2014年4月15日
嘿,谢谢你的回答-我已经尝试了你的建议,但是我在pdu=pduFactory.createPdu(packet.getData());
这行上遇到了一个错误,它说:“无法对非静态字段pdu进行静态引用。”。“我更改了公共Pdu Pdu代码>至<代码>公共静态Pdu Pdu代码>,这样就消除了这个错误,但是现在当我尝试运行我的EspduReceiver.java类时,什么也没有发生
我的Gui.java类现在可以编译了,但是当我尝试运行它时,控制台会显示一条消息:“线程中的异常”AWT-EventQueue-0“java.lang.NullPointerException”。它在抱怨以下几句话:
initGui();
String output = EspduReceiver.pdu.toString();
Gui gui = new Gui();
如果您正在键入espddureceiver.pdu
,这意味着您希望获得espddureceiver
类的pdu
字段的值。但是这个类没有这样的字段。因此,只需添加以下内容:
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类中,您应该获得pdu
的String
内容。我从来没有遇到过这个类,但是如果它是一个描述的类,我认为您可以通过以下方式获得数据:
String output = EspduReceiver.pdu.toString();
嘿,谢谢你的回答-我已经试着做了你建议的更改,但我仍然遇到一些问题-我修改了我原来的问题,以显示所做的更改和我现在遇到的错误。@someone2088我的打字错误,编辑了我的答案,你应该把它设置为static
nikis-我试过了-正如我在编辑我原来的帖子中提到的那样。这消除了错误,但现在当我尝试运行espdureciver.java类时什么也没有发生,我的Gui.java类中又出现了一个错误…@someone2088您应该在createPdu
执行之后检查pdu
的值我想您的意思是通过在控制台中打印来检查值?我不能这样做,因为我的类不再运行——正如我在编辑中提到的那样。我在尝试运行EspduReceiver.java类时根本没有得到任何输出。。。当尝试运行Gui.java类时,我得到了我提到的NullPointerException。。。有没有关于如何解决这些问题的建议?