簡體   English   中英

無法實例化類型actionlistener?

[英]cannot instantiate type actionlistener?

import org.jsoup.Jsoup;

@SuppressWarnings("unused")

public class SimpleWebCrawler extends JFrame implements ActionListener {

    JTextField yourInputField = new JTextField(20);
    static JTextArea _resultArea = new JTextArea(200, 200);
    JScrollPane scrollingArea = new JScrollPane(_resultArea);
    private final static String newline = "\n";
    JButton jButton = new JButton("Send Text");

    public SimpleWebCrawler() throws MalformedURLException  {

        yourInputField.addActionListener(new ActionListener());

        class MyActionListener implements ActionListener {
            public void actionPerformed(ActionEvent evt) {
                JTextField textfield = (JTextField)evt.getSource();
                process(textfield.getText());
            }
        }

        String word2 = yourInputField.getText();

        _resultArea.setEditable(false);

        try {
            URL my_url = new URL("http://" + word2 + "/");
            BufferedReader br = new BufferedReader(new InputStreamReader(
                    my_url.openStream()));
            String strTemp = "";
            while (null != (strTemp = br.readLine())) {
                _resultArea.append(strTemp + newline);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        _resultArea.append("\n");
        _resultArea.append("\n");
        _resultArea.append("\n");


        String url = "http://" + word2 + "/";
        print("Fetching %s...", url);

        try{
        Document doc = Jsoup.connect(url).get();
        Elements links = doc.select("a[href]");

        System.out.println("\n");

        BufferedWriter bw = new BufferedWriter(new 
                FileWriter("C:\\Users\\user\\fypworkspace\\FYP\\Link\\abc.txt"));
        _resultArea.append("\n");
        for (Element link : links) {
            print("  %s  ", link.attr("abs:href"), trim(link.text(), 35));

            bw.write(link.attr("abs:href"));
            bw.write(System.getProperty("line.separator"));
        }
        bw.flush();
        bw.close();
        } catch (IOException e1) {

        }
        JPanel content = new JPanel();
        content.setLayout(new BorderLayout());
        content.add(scrollingArea, BorderLayout.CENTER);
        content.add(yourInputField,BorderLayout.SOUTH);
        content.add(jButton, BorderLayout.EAST);

        this.setContentPane(content);
        this.setTitle("Crawled Links");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.pack();


        }

        private static void print(String msg, Object... args) {

            _resultArea.append(String.format(msg, args) +newline);
        }

        private static String trim(String s, int width) {
            if (s.length() > width)
                return s.substring(0, width - 1) + ".";
            else
                return s;
        }

        //.. Get the content pane, set layout, add to center




    public static void main(String[] args) throws IOException {

        JFrame win = new SimpleWebCrawler();
        win.setVisible(true);

    }
}

我得到這個錯誤無法實例化類型actionlistener。 代碼行是:

yourInputField.addActionListener(new ActionListener());

        class MyActionListener implements ActionListener {
            public void actionPerformed(ActionEvent evt) {
                JTextField textfield = (JTextField)evt.getSource();
                process(textfield.getText());
            }
        }

我正在嘗試創建一個JTextField來接收來自用戶的輸入。 仍然不成功。 什么導致錯誤?

如果您嘗試使用匿名內部類,它應該看起來像這樣:

yourInputField.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
             JTextField textfield = (JTextField)evt.getSource();
                         process(textfield.getText());
        }
    });

但是如果你想使用嵌套類,它看起來像這樣:

yourInputField.addActionListener(new MyActionListener());

然后在聲明嵌套類的方法之外的某個地方:

      private class MyActionListener implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent e) {
            TextField textfield = (JTextField)evt.getSource();
                             process(textfield.getText());
        }
    }

ActionListener是一個接口而不是類,您無法實例化接口。

更換:

yourInputField.addActionListener(new ActionListener());

有:

yourInputField.addActionListener(new MyActionListener());

ActionListener是一個接口,您無法實例化new ActionListener();

我認為在你的情況下你想要

yourInputField.addActionListener(new MyActionListener());

首先,您需要通過將以下行放在類的頂部來導入ActionListener和ActionEvent:

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

接下來,將MyActionListener內部類移動到構造函數之外的其他位置 - 在主要方法之后,在類的底部(不在main方法內部),這是一個好地方。

最后,將'new ActionListener()'替換為以下行中的'new MyActionListener()':

yourInputField.addActionListener(new ActionListener());

它將成為:

yourInputField.addActionListener(new MyActionListener());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM