2012-08-04 72 views
0

我的代码应该捕获网络流量并将其显示在textarea上,但它没有这样做。请查看代码并检查是否有任何更正制作。JButton无法正常工作[捕获网络流量]

public class NewClass { 

    public static JTextArea textarea = new JTextArea(); 

    NewClass() throws IOException{ 

     JButton capture = new JButton("Capture"); 
     JFrame frame = new JFrame();   
     JScrollPane scroll; 
     NetworkInterface[] NI= JpcapCaptor.getDeviceList(); 
     int INDEX=0; 
     JpcapCaptor JPCAP = JpcapCaptor.openDevice(NI[INDEX], 65536, false, 20); 

     frame.setSize(700,500); 
     frame.setLocation(200,200); 
     frame.getContentPane().setLayout(null); 
     frame.setBackground(Color.yellow);   

     textarea.setEditable(false); 
     textarea.setFont(new Font("Tahoma",0,14)); 
     textarea.setForeground(Color.RED); 
     textarea.setLineWrap(true); 
     //textarea.setBackground(Color.WHITE); 

     scroll = new JScrollPane(); 
     scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); 
     scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); 
     scroll.setViewportView(textarea); 

     frame.getContentPane().add(scroll); 
     scroll.setBounds(10,16,740,290); 

     capture.setBackground(Color.RED); 
     capture.setForeground(Color.GREEN); 
     frame.getContentPane().add(capture); 

     handler ob = new handler(); 
     capture.addActionListener(ob); 
     capture.setBounds(100, 400, 90, 25); 

     frame.setVisible(true); 
    } 

    public class handler implements ActionListener{ 
     public void actionPerformed(ActionEvent Event){ 

      class Print implements PacketReceiver{ 
       public void receivePacket(Packet packet){ 
        String info = packet.toString(); 
        textarea.append(info); 
        //System.out.println(packet.toString()); 
       }      
      } 
    } 
} 
+0

显示什么错误? – Reimeus 2012-08-04 15:01:49

回答

2

我这是为了捕获网络流量和textarea的显示,但它不是做it.Please代码看看代码,并检查是否存在要作出的任何更正。

对于其中一个,您的处理程序的actionPerformed方法并没有真正做任何事情。它定义了一个内部类,但创建此类没有对象,所以没有任何与它:

public class handler implements ActionListener { 
    public void actionPerformed(ActionEvent Event) { 
    class Print implements PacketReceiver { 
     public void receivePacket(Packet packet) { 
      String info = packet.toString(); 
      textarea.append(info); // System.out.println(packet.toString()); 
     } 
    } 
    } 
} 

考虑创建您的打印类的对象(一类可怕的名字,因为已经有一个打印类是部分的核心Java库),并让这个Print对象做一些有用的事情,可能会接收数据包(不过它应该这样做)。注意不要在主Swing线程中执行长时间运行的进程,但EDT会冻结您的Swing GUI。

编辑
例如,

// note that class names such as "Handler" should begin with a capital letter. 
public class Handler implements ActionListener { 
    public void actionPerformed(ActionEvent Event) { 
    class Print implements PacketReceiver { 
     public void receivePacket(Packet packet) { 
      String info = packet.toString(); 
      textarea.append(info); // System.out.println(packet.toString()); 
     } 
    } 

    // create a Print instance so that it can do something: 
    final Print myPrint = new Print(); 

    // do something with myPrint here so that it gets packets and displays them 
    // I suspect that you'll likely want to do this in a background thread 
    // using a SwingWorker 
    } 
} 
1

如气垫船说你在做什么,但定义一个内部类。你在这里丢失的是从你的JPCAP类调用processPacket()或loopPacket()。所以我提出以下几点:

public class handler implements ActionListener{ 
    public void actionPerformed(ActionEvent Event){ 

    class Print implements PacketReceiver{ 
     public void receivePacket(Packet packet){ 
      String info = packet.toString(); 
      textarea.append(info);            
     } 

    } 

    // this captures 10 packets . 
    JPCAP.processPacket(10,new Print()); 
    } 
} 
+0

'()'添加到'新打印'。尽管如此,processPacket可能需要在后台线程中完成。 1+的建议。 – 2012-08-04 15:34:43