2012-08-04 50 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

相關問題