【问题标题】:JButton not working [capture network traffic]JButton 不工作 [捕获网络流量]
【发布时间】:2012-08-04 14:56:36
【问题描述】:

我的代码应该捕获网络流量并将其显示在 textarea 上,但它没有这样做。请查看代码并检查是否需要进行任何更正。

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());
                }                    
            }
    }
}

【问题讨论】:

    标签: java swing concurrency jbutton jpcap


    【解决方案1】:

    我的代码应该捕获网络流量并将其显示在 textarea 上,但它没有这样做。请查看代码并检查是否需要进行任何更正。

    首先,您的处理程序的 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());
            }
         }
      }
    }
    

    考虑为您的 Print 类创建一个对象(这个类的名称很糟糕,因为已经有一个 Print 类是核心 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
      }
    }
    

    【讨论】:

      【解决方案2】:

      正如 Hovercraft 所说,您除了定义一个内部类之外什么都不做。您在这里缺少的是从您的 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());
        }
      }
      

      【讨论】:

      • () 添加到 new Print。不过,processPacket 可能需要在后台线程中完成。 1+ 的建议。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-04
      • 2014-02-14
      • 1970-01-01
      相关资源
      最近更新 更多