无法实例化类型动作侦听器? [英] 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);
}
}
我收到此错误无法实例化类型动作侦听器.代码行是:
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());
}
}
我正在尝试创建一个 JTextField 来接收来自用户的输入.还是没有成功.是什么导致了错误?
I am trying to create a JTextField to receive input from the user. Still unsuccessful. WHat has caused the error ?
推荐答案
如果您尝试使用匿名内部类,它应该如下所示:
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());
}
}
这篇关于无法实例化类型动作侦听器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文