Java 无法实例化类型actionlistener?
我发现此错误,无法实例化actionlistener类型。代码行是:Java 无法实例化类型actionlistener?,java,swing,textfield,Java,Swing,Textfield,我发现此错误,无法实例化actionlistener类型。代码行是: import org.jsoup.Jsoup; @SuppressWarnings("unused") public class SimpleWebCrawler extends JFrame implements ActionListener { JTextField yourInputField = new JTextField(20); static JTextArea _resultArea =
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);
}
}
我试图创建一个JTextField来接收用户的输入。仍然不成功。是什么导致了这个错误 ActionListener是一个接口,您不能实例化像
new ActionListener()这样的接口代码>
我想在你的情况下你想要
yourInputField.addActionListener(new ActionListener());
class MyActionListener implements ActionListener {
public void actionPerformed(ActionEvent evt) {
JTextField textfield = (JTextField)evt.getSource();
process(textfield.getText());
}
}
ActionListener是一个接口而不是类,您不能实例化接口
替换:
yourInputField.addActionListener(new MyActionListener());
与:
如果您试图使用匿名内部类,则应如下所示:
yourInputField.addActionListener(new MyActionListener());
但是,如果要使用嵌套类,它可以如下所示:
yourInputField.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
JTextField textfield = (JTextField)evt.getSource();
process(textfield.getText());
}
});
然后在方法之外的某个地方声明嵌套类:
yourInputField.addActionListener(new MyActionListener());
首先,您需要导入ActionListener和ActionEvent,方法是将以下行放在类的顶部:
private class MyActionListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
TextField textfield = (JTextField)evt.getSource();
process(textfield.getText());
}
}
接下来,将MyActionListener内部类移动到构造函数之外的某个位置—一个好的位置是在主方法之后,在类的底部(但不是在主方法内部)
最后,将以下行中的“new ActionListener()”替换为“new MyActionListener()”:
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
它将成为:
yourInputField.addActionListener(new ActionListener());
我已经尝试过了,但是我得到的错误是无法解析为类型。这是我对代码的定位吗?是的,将MyActionListener类的定义移出SimpleWebCrawler构造函数。的可能重复
yourInputField.addActionListener(new ActionListener());
yourInputField.addActionListener(new MyActionListener());