addActionListener上的语法错误

时间:2011-04-03 06:50:20

标签: java swing textfield

import org.jsoup.Jsoup;


@SuppressWarnings({ "unused", "serial" })

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




    @Override
    yourInputField.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub

    }









    public SimpleWebCrawler() throws MalformedURLException  {


        _resultArea.setEditable(false);

        String word2 = yourInputField.getText();

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


        this.setContentPane(content);
        this.setTitle("Crawled Links");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.pack();
        JPanel content2 = new JPanel();
        this.setContentPane(content2);
        this.setTitle("Input the URL");
        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);

    }



}

我在addActionListener上遇到了这个错误语法错误,(=在此令牌之后是预期的)

yourInputField.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub

    }

任何人都可以给我一些指针吗?

3 个答案:

答案 0 :(得分:3)

您必须将actionlistener添加到方法体中,在本例中为SimpleWebCrawler的构造函数,并且缺少一些括号。

 import org.jsoup.Jsoup;


 @SuppressWarnings({ "unused", "serial" })

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



public SimpleWebCrawler() throws MalformedURLException  {


    yourInputField.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub

   }});

    _resultArea.setEditable(false);

    String word2 = yourInputField.getText();

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


    this.setContentPane(content);
    this.setTitle("Crawled Links");
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.pack();
    JPanel content2 = new JPanel();
    this.setContentPane(content2);
    this.setTitle("Input the URL");
    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);

}

}

答案 1 :(得分:1)

你正在调用一个方法(yourInputField.addActionListener),它必须在其他方法中发生,而不仅仅是在类中。

答案 2 :(得分:1)

试试这个:

yourInputField.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub

    }
});

您错过了语句的结尾:右括号和分号。

另外,我在这段代码之前不明白@Override的原因。