java - 匿名类不是抽象类,不会重写抽象方法

标签 java packet-capture jpcap

我在java中有这个程序..它用于使用Winpcap和jpcap捕获网络数据包...编译时出现问题..它发送此消息:

C:\Users\IMAX\Desktop\PacketPirate\PacketPirate\src\PkPirate_GUI.java:95: error: <anonymous PkPirate_GUI$1> is not abstract and does not override abstract method actionPerformed(ActionEvent) in ActionListener
    {
    ^

谁能告诉我如何让它工作......这是完整的程序源代码:

这是为了创建GUI界面

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import jpcap.*;

public class PkPirate_GUI
{

    NetworkInterface [] NETWORK_INTERFACES;
    JpcapCaptor CAP;
    PkPirate_CaptureThread CAPTAIN;
    int INDEX=0;
    int COUNTER =0;
    boolean CaptureState=false;

    ////////////////////////////////////////////////////////////////Frame
    JFrame MainWindow = new JFrame("Packet Capture Program");
    ////////////////////////////////////////////////////////////////Text Area
    public static JTextArea TA_OUTPUT=new JTextArea();
    ////////////////////////////////////////////////////////////////Scroll pane
    JScrollPane SP_OUTPUT=new JScrollPane();
    ////////////////////////////////////////////////////////////////Button Groups
    ButtonGroup BG_Filter_Enable_Disable=new ButtonGroup();
    ButtonGroup BG_Ports=new ButtonGroup();
    ////////////////////////////////////////////////////////////////Buttons
    JButton B_Capture=new JButton("Capture");
    JButton B_Stop=new JButton("Stop");
    JButton B_Select=new JButton("Select");
    JButton B_List=new JButton("List");
    JButton B_Filter=new JButton("Filter");


    //////////////////////////////////////////////////////////////////Raido Buttons

    JRadioButton RB_Filter_Enable = new JRadioButton ("Enable");
    JRadioButton RB_Filter_Disable = new JRadioButton ("Disable");
    JRadioButton RB_Port_Special = new JRadioButton ("Special Port");
    JRadioButton RB_Port_DNS = new JRadioButton ("DNS (53)");


    //////////////////////////////////////////////////////////////////////Lables


    JLabel L_Interface = new JLabel("Interface");
    JLabel L_FilterStatus = new JLabel("Port Filter Status");
    JLabel L_FilterStatusBox = new JLabel("Disabled (All Ports)");
    JLabel L_FilterPresets = new JLabel("Port Filter Presets");
    JLabel L_SpecialPort = new JLabel("Special Port");

    ///////////////////////////////////////////////////////////////////////Text Field

    JTextField TF_SelectInterface = new JTextField();
    JTextField TF_SpecialPort = new JTextField();

    public static void main (String args[])
        {
            new PkPirate_GUI();
        }

    public PkPirate_GUI(){
        BuildGUI();
        DisbleButtons();
        }

    public void BuildGUI()
    {
        MainWindow.setSize(765,480);
        MainWindow.setLocation(200,200);
        MainWindow.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        MainWindow.getContentPane().setLayout(null);
        /////////////////////////////////////////////////////////

        TA_OUTPUT.setEditable(false);
        TA_OUTPUT.setFont(new Font("Monospaced",0,12));
        TA_OUTPUT.setForeground(new Color(0,0,153));
        TA_OUTPUT.setLineWrap(true);
    ///////////////////////////////////////////////////////////////////

    SP_OUTPUT.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
    SP_OUTPUT.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    SP_OUTPUT.setViewportView(TA_OUTPUT);
    /////////////////////////////////////////////////////////////////////////////////
    MainWindow.getContentPane().add(SP_OUTPUT);
        SP_OUTPUT.setBounds(10,16,740,290);
    /////////////////////////////////////////////////////////////////////////////////
    /////////////////////////////////////////////////////////////////Buttons

    B_Capture.setBackground(new Color(255,0,0));
    B_Capture.setForeground(new Color(255,255,255));
    B_Capture.setMargin(new Insets(0,0,0,0));
    B_Capture.addActionListener(new ActionListener()
    {
        public void actionPerforemed(ActionEvent x)
        {   
                    Action_B_Capture(x);
                }


        });
        MainWindow.getContentPane().add(B_Capture);
        B_Capture.setBounds(145,310,110,25);
//////////////////////////////////////////////////////////////////


    B_Stop.setBackground(new Color(0,0,0));
    B_Stop.setForeground(new Color(255,255,255));
    B_Stop.setMargin(new Insets(0,0,0,0));
    B_Stop.addActionListener(new ActionListener()
    {
        public void actionPerforemed(ActionEvent x)
        {   Action_B_Stop(x);}


        });
        MainWindow.getContentPane().add(B_Stop);
        B_Stop.setBounds(145,310,110,25);
        //////////////////////////////////////////////////////////////////


    B_Select.setBackground(new Color(0,0,0));
    B_Select.setForeground(new Color(255,255,255));
    B_Select.setMargin(new Insets(0,0,0,0));
    B_Select.addActionListener(new ActionListener()
    {
        public void actionPerforemed(ActionEvent x)
        {   Action_B_Select(x);}


        });
        MainWindow.getContentPane().add(B_Select);
        B_Select.setBounds(0,388,75,20);
        //////////////////////////////////////////////////////////////////


    B_List.setBackground(new Color(0,0,0));
    B_List.setForeground(new Color(255,255,255));
    B_List.setMargin(new Insets(0,0,0,0));
    B_List.addActionListener(new ActionListener()
    {
        public void actionPerforemed(ActionEvent x)
        {   Action_B_List(x);}


        });
        MainWindow.getContentPane().add(B_List);
        B_List.setBounds(0,410,75,20);
        //////////////////////////////////////////////////////////////////

    B_Filter.setBackground(new Color(0,0,0));
    B_Filter.setForeground(new Color(255,255,255));
    B_Filter.setMargin(new Insets(0,0,0,0));
    B_Filter.addActionListener(new ActionListener()
    {
        public void actionPerforemed(ActionEvent x)
        {   Action_B_Filter(x);}


        });
        MainWindow.getContentPane().add(B_Filter);
        B_Filter.setBounds(360,400,80,25);


        //////////////////////////////////////////////////////////////////Radio Button
    BG_Filter_Enable_Disable.add(RB_Filter_Enable);
    RB_Filter_Enable.addActionListener(new ActionListener()
        {

        public void actionPerformed(ActionEvent x)
                {Action_B_ENABLE (x);}
        });
        MainWindow.getContentPane().add(RB_Filter_Enable);
        RB_Filter_Enable.setBounds(290,350,70,25);
///////////////////////////////////////////////////////////////////
    BG_Filter_Enable_Disable.add(RB_Filter_Disable);
    RB_Filter_Disable.setSelected(true);
    RB_Filter_Disable.addActionListener(new ActionListener(){

        public void actionPerformed(ActionEvent x)
                {Action_B_DISABLE (x);}
        });
        MainWindow.getContentPane().add(RB_Filter_Disable);
        RB_Filter_Disable.setBounds(360,350,70,25);
        ///////////////////////////////////////////////////////////////////
        BG_Ports.add(RB_Port_Special);
        RB_Port_Special.setFont(new Font("Tahoma",0,11));
        MainWindow.getContentPane().add(RB_Port_Special);
        RB_Port_Special.setBounds(360,380,90,90);


        ///////////////////////////////////////////////////////////////////
        BG_Ports.add(RB_Port_DNS);
        RB_Port_DNS.setFont(new Font("Tahoma",0,11));
        MainWindow.getContentPane().add(RB_Port_DNS);
        RB_Port_DNS.setBounds(560,400,90,25);
        ///////////////////////////////////////////////////////////////////

    ////////////////////////////////////////////////////////////////////
    L_Interface.setHorizontalAlignment(SwingConstants.CENTER);
    MainWindow.getContentPane().add(L_Interface);
    L_Interface.setBounds(10,344,60,16);
    ////////////////////////////////////////////////////////////////////
    L_FilterStatus.setHorizontalAlignment(SwingConstants.CENTER);
    MainWindow.getContentPane().add(L_FilterStatus);
    L_FilterStatus.setBounds(300,310,110,16);
    ////////////////////////////////////////////////////////////////////
    L_FilterStatusBox.setForeground(new Color(255,51,0));
    L_FilterStatusBox.setHorizontalAlignment(SwingConstants.CENTER);
    L_FilterStatusBox.setBorder(BorderFactory.createLineBorder(new Color(0,0,0)));
    MainWindow.getContentPane().add(L_FilterStatusBox);
    L_FilterStatusBox.setBounds(270,330,170,20);
    ///////////////////////////////////////////////////////////////////////////////////
    L_FilterPresets.setFont(new Font("Tahoma",0,10));
    L_FilterPresets.setForeground(new Color(0,153,0));
    L_FilterPresets.setHorizontalAlignment(SwingConstants.CENTER);
    MainWindow.getContentPane().add(L_FilterPresets);
    L_FilterPresets.setBounds(550,310,110,10);
    ////////////////////////////////////////////////////////////////////
    L_SpecialPort.setForeground(new Color(255,0,0));
    L_SpecialPort.setHorizontalAlignment(SwingConstants.CENTER);
    MainWindow.getContentPane().add(L_SpecialPort);
    L_SpecialPort.setBounds(270,380,80,14);
    ////////////////////////////////////////////////////////////////////Text Field
    TF_SelectInterface.setForeground(new Color(255,0,0));
    TF_SelectInterface.setHorizontalAlignment(JTextField.CENTER);
    MainWindow.getContentPane().add(TF_SelectInterface);
    TF_SelectInterface.setBounds(3,364,70,20);
    ///////////////////////////////////////////////////////////////////////////
    TF_SpecialPort.setForeground(new Color(255,0,0));
    TF_SpecialPort.setHorizontalAlignment(JTextField.CENTER);
    MainWindow.getContentPane().add(TF_SpecialPort);
    TF_SpecialPort.setBounds(270,400,80,22);
    ///////////////////////////////////////////////////////////////////////////
    MainWindow.setVisible(true);
        }
////////////////////////////////////////////////////////////////////////////////
public void Action_B_Capture(ActionEvent X){

    TA_OUTPUT.setText("");
    CaptureState=true;
    CapturePackets();
    }
//////////////////////////////////////////////////////////////////////////////////
public void Action_B_Stop(ActionEvent X){

    CaptureState=false;
    CAPTAIN.finished();
    }
/////////////////////////////////////////////////////////////////////////////////
public void Action_B_Select(ActionEvent X){

    ChooseInterface();
    }
/////////////////////////////////////////////////////////////////////////////////
public void Action_B_List(ActionEvent X){

    ListNetworkInterfaces();
    B_Select.setEnabled(true);
    TF_SelectInterface.requestFocus();
    }
/////////////////////////////////////////////////////////////////////////////////
public void Action_B_Filter(ActionEvent X){

   try
   {
       if (RB_Filter_Enable.isSelected()){
       String PORT=TF_SpecialPort.getText();
       CAP.setFilter("port "+PORT,true);

   }
           else if  (RB_Port_DNS.isSelected())
           { CAP.setFilter("port 53",true);}

    else{
    JOptionPane.showMessageDialog(null,"Filtering is Disabled!");

        }


       }
catch(Exception y) 
{
    System.out.println(y);
}
    }

///////////////////////////////////////////////////////////////////////////////////////
    public void Action_B_ENABLE(ActionEvent X){

    L_FilterStatusBox.setText("Enabled(Selected port)");
    }
///////////////////////////////////////////////////////////////////////////////////////
    public void Action_B_DISABLE(ActionEvent X){

    L_FilterStatusBox.setText("Disabled(All ports)");
    }
///////////////////////////////////////////////////////////////////////////////////////
public void CapturePackets()
    {
        CAPTAIN=new PkPirate_CaptureThread()
        {
            public Object construct(){

                TA_OUTPUT.setText("\nNow Captureing on interface" + INDEX+"..."+
                "\n----------------------------------------------------------"+
                "----------------------------------------------------------\n\n");
                try
                {
                    CAP=JpcapCaptor.openDevice(NETWORK_INTERFACES[INDEX],65535,false,20);
                    while (CaptureState)
                    {
                    CAP.processPacket(1,new PkPirate_PacketContents());

                        }
                        CAP.close();
                    }
                catch (Exception x)
                {System.out.print(x);}
                return 0;

                }
public void finished()
{
    this.interrupt();
}

            };
CAPTAIN.start();
        }
///////////////////////////////////////////////////////////////////////////
    public void DisbleButtons()
    {
        B_Capture.setEnabled(false);
        B_Stop.setEnabled(false);
        B_Select.setEnabled(false);
        B_Filter.setEnabled(false);


        }
//////////////////////////////////////////////////////////////////////////
    public void EnableButtons()
    {
        B_Capture.setEnabled(true);
        B_Stop.setEnabled(true);
        B_Select.setEnabled(true);
        B_Filter.setEnabled(true);

        }
//////////////////////////////////////////////////////////////////////////
    public void ListNetworkInterfaces(){
            NETWORK_INTERFACES=JpcapCaptor.getDeviceList();
            TA_OUTPUT.setText("");
            for(int i=0;i<NETWORK_INTERFACES.length;i++)
            {
        TA_OUTPUT.append("\n\n-----------------------------------interface"+i+
                         "Info-----------------------------------");
              TA_OUTPUT.append("\nInterface Number:"+i);
              TA_OUTPUT.append("\nDescription:"+
                                NETWORK_INTERFACES[i].name+"("+
                                NETWORK_INTERFACES[i].description+")");
              TA_OUTPUT.append("\nDatalink Name:"+
                    NETWORK_INTERFACES[i].datalink_name+"("+
                            NETWORK_INTERFACES[i].datalink_description+")");
              TA_OUTPUT.append("\nMAC address:");
              byte[]R= NETWORK_INTERFACES[i].mac_address;
              for(int a=0;a<=NETWORK_INTERFACES.length;a++)
              {TA_OUTPUT.append(Integer.toHexString(R[a]&0xff)+":");}
              NetworkInterfaceAddress [] INT = NETWORK_INTERFACES[i].addresses;
              TA_OUTPUT.append("\nIP Address:"+ INT[0].address);
              TA_OUTPUT.append("\nSubnet:"+ INT[0].address);
              TA_OUTPUT.append("\nBroadcast Address:"+ INT[0].address);
              COUNTER++;


        }}
    public void ChooseInterface()
    {
            int temp;
         temp = Integer.parseInt(TF_SelectInterface.getText());
        if(temp>-1&&temp<COUNTER){
        INDEX=temp;
        EnableButtons();
        }
    else
    {
        JOptionPane.showMessageDialog(null,"Out Range. # interfaces = 0-"+(COUNTER-1)+".");

        }
    TF_SelectInterface.setText("");
    ///////////////////////////////////////////////////////////////////////////
}


    }

这是针对线程的

import javax.swing.SwingUtilities;

/**
 * This is the 3rd version of SwingWorker (also known as
 * SwingWorker 3), an abstract class that you subclass to
 * perform GUI-related work in a dedicated thread.  For
 * instructions on using this class, see:
 * 
 * http://java.sun.com/docs/books/tutorial/uiswing/misc/threads.html
 *
 * Note that the API changed slightly in the 3rd version:
 * You must now invoke start() on the SwingWorker after
 * creating it.
 */
public abstract class PkPirate_CaptureThread {
    private Object value;  // see getValue(), setValue()
    private Thread thread;

    /** 
     * Class to maintain reference to current worker thread
     * under separate synchronization control.
     */
    private static class ThreadVar {
        private Thread thread;
        ThreadVar(Thread t) { thread = t; }
        synchronized Thread get() { return thread; }
        synchronized void clear() { thread = null; }
    }

    private ThreadVar threadVar;

    /** 
     * Get the value produced by the worker thread, or null if it 
     * hasn't been constructed yet.
     */
    protected synchronized Object getValue() { 
        return value; 
    }

    /** 
     * Set the value produced by worker thread 
     */
    private synchronized void setValue(Object x) { 
        value = x; 
    }

    /** 
     * Compute the value to be returned by the <code>get</code> method. 
     */
    public abstract Object construct();

    /**
     * Called on the event dispatching thread (not on the worker thread)
     * after the <code>construct</code> method has returned.
     */
    public void finished() {
    }

    /**
     * A new method that interrupts the worker thread.  Call this method
     * to force the worker to stop what it's doing.
     */
    public void interrupt() {
        Thread t = threadVar.get();
        if (t != null) {
            t.interrupt();
        }
        threadVar.clear();
    }

    /**
     * Return the value created by the <code>construct</code> method.  
     * Returns null if either the constructing thread or the current
     * thread was interrupted before a value was produced.
     * 
     * @return the value created by the <code>construct</code> method
     */
    public Object get() {
        while (true) {  
            Thread t = threadVar.get();
            if (t == null) {
                return getValue();
            }
            try {
                t.join();
            }
            catch (InterruptedException e) {
                Thread.currentThread().interrupt(); // propagate
                return null;
            }
        }
    }


    /**
     * Start a thread that will call the <code>construct</code> method
     * and then exit.
     */
    public PkPirate_CaptureThread() {
        final Runnable doFinished = new Runnable() {
           public void run() { finished(); }
        };

        Runnable doConstruct = new Runnable() { 
            public void run() {
                try {
                    setValue(construct());
                }
                finally {
                    threadVar.clear();
                }

                SwingUtilities.invokeLater(doFinished);
            }
        };

        Thread t = new Thread(doConstruct);
        threadVar = new ThreadVar(t);
    }

    /**
     * Start the worker thread.
     */
    public void start() {
        Thread t = threadVar.get();
        if (t != null) {
            t.start();
        }
    }
}

最后这是数据包内容:

import jpcap.PacketReceiver;
import jpcap.packet.Packet;

public class PkPirate_PacketContents implements PacketReceiver
{

    public void receivePacket(Packet packet)
{
    PkPirate_GUI.TA_OUTPUT.append(packet.toString()+
    "\n--------------------------------------------------"+
    "-----------------------------------------------------\n\n");
    }

    }

最佳答案

您拥有actionPerforamed,而不是actionPerformed

关于java - 匿名类不是抽象类,不会重写抽象方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33018845/

相关文章:

java - 如何在NGINX反向代理后面运行 "Eclipse Che"?

java - 如何将 Jsoup 文档保存到 HTML 文件?

c++ - 如何在 C++ 中分析流量转储文件中的数据包信息?

iPhone 和 WireShark

java - 嗅探 LAN : Jpcap java program only seems to catch my packets

java - 依赖于抛硬币的 while 循环的预期运行时间是多少?

java - 为基于 Java 的桌面应用程序使用 Webkit 渲染

Python 数据包嗅探器

java - 使用 Jpcap 线程 "main"java.lang.UnsatisfiedLinkError 中出现异常

java - 异常在线程 "main"java.lang.UnsatisfiedLinkError : no jpcap in java. library.path