无法实例化类型actionlistener?

发布于 2024-10-29 00:56:43 字数 3555 浏览 1 评论 0原文

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

    }
}

我收到此错误无法实例化类型动作侦听器。代码行是:

yourInputField.addActionListener(new ActionListener());

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

我正在尝试创建一个 JTextField 来接收用户的输入。还是没有成功。是什么导致了错误?

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

    }
}

I got this error cannot instantiate type actionlistener. The line of code is :

yourInputField.addActionListener(new ActionListener());

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

I am trying to create a JTextField to receive input from the user. Still unsuccessful. WHat has caused the error ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

極樂鬼 2024-11-05 00:56:43

如果您尝试使用匿名内部类,它应该如下所示:

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

If your trying to use an anonymous inner class, it should look like that:

yourInputField.addActionListener(new ActionListener() {

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

But if you want to use a nested class it can look like this:

yourInputField.addActionListener(new MyActionListener());

and then somewhere out of the method you declare the nested class:

      private class MyActionListener implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent e) {
            TextField textfield = (JTextField)evt.getSource();
                             process(textfield.getText());
        }
    }
塔塔猫 2024-11-05 00:56:43

ActionListener 是一个接口而不是类,并且您不能实例化接口。

替换:

yourInputField.addActionListener(new ActionListener());

为:

yourInputField.addActionListener(new MyActionListener());

ActionListener is an interface not a class, and you can not instantiate interfaces.

Replace:

yourInputField.addActionListener(new ActionListener());

with:

yourInputField.addActionListener(new MyActionListener());
澜川若宁 2024-11-05 00:56:43

ActionListener 是一个接口,您无法实例化像 new ActionListener(); 这样的

接口,我认为在您的情况下您想要

yourInputField.addActionListener(new MyActionListener());

ActionListener is an interface and you cannot instantiate an interface like new ActionListener();

I think in your case you want

yourInputField.addActionListener(new MyActionListener());
遗失的美好 2024-11-05 00:56:43

首先,您需要导入 ActionListener 和 ActionEvent,方法是将以下几行放在类的顶部:

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

接下来,将 MyActionListener 内部类移动到构造函数之外的其他位置 - 一个好的位置是在 main 方法之后,在类的底部(虽然不在 main 方法内)。

最后,将以下行中的“new ActionListener()”替换为“new MyActionListener()”:

yourInputField.addActionListener(new ActionListener());

它将变为:

yourInputField.addActionListener(new MyActionListener());

Firstly you need to import ActionListener and ActionEvent, by putting the following lines at the top of your class:

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

Next, move your MyActionListener inner class somewhere other than your constructor - a good place would be after your main method, at the bottom of the class (not inside the main method though).

Finally, replace 'new ActionListener()' with 'new MyActionListener()' in the following line:

yourInputField.addActionListener(new ActionListener());

It will become:

yourInputField.addActionListener(new MyActionListener());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文