java 类型无法解析
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14510258/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
type cannot be resolved
提问by user2008937
heres my code:
继承人我的代码:
import javax.swing.*;
import java.awt.*;
public class FirstGui extends JFrame {
private JLabel label;
private JButton button;
public FirstGui() {
setLayout(new FlowLayout());
button = new JButton("Click for sex");
add(button);
label = new JLabel("");
add(label);
event e = new event();
button.addActionListener(e);
}
public class event implements ActionListener {
public void actionPerformed(ActionEvent e) {
label.setText("how you can see wors here");
}
}
public static void main(String [] args) {
FirstGui gui = new FirstGui();
gui.setDefaultCloseOperation(EXIT_ON_CLOSE);
gui.setSize(200, 200);
gui.setTitle("Title");
gui.setVisible(true);
}
}
And it generates a errors:
它会产生一个错误:
ActionEvent cannot be resolved to a type FirstGui.java /Test/src line 26 Java Problem
ActionEvent 无法解析为类型 FirstGui.java /Test/src 第 26 行 Java 问题
ActionListener cannot be resolved to a type FirstGui.java /Test/src line 24 Java Problem
ActionListener 无法解析为类型 FirstGui.java /Test/src 第 24 行 Java 问题
The method addActionListener(ActionListener) in the type AbstractButton is not applicable for the arguments (FirstGui.event) FirstGui.java /Test/src line 21 Java Problem
AbstractButton 类型中的 addActionListener(ActionListener) 方法不适用于参数 (FirstGui.event) FirstGui.java /Test/src line 21 Java Problem
what is wrong with it??? im new to java.
有什么问题吗???我是 Java 新手。
回答by GeorgeVremescu
Import the following:
导入以下内容:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
回答by Dan D.
ActionEvent
and ActionListener
are located into the java.awt.event
package.
ActionEvent
并ActionListener
位于java.awt.event
包中。
Importing java.awt.*
is not enough.
进口java.awt.*
还不够。
回答by Foggzie
Both of these classes require you to import them. You can do so by importing everything in java.awt.event:
这两个类都需要您导入它们。您可以通过导入 java.awt.event 中的所有内容来实现:
import java.awt.event.*;
or you may just want to import specifically what you're using:
或者您可能只想专门导入您正在使用的内容:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
Remember that it's considered good practice to import individual classes (the latter option) instead of importing whole packages.
请记住,导入单个类(后一个选项)而不是导入整个包被认为是一种很好的做法。
If you ever get stuck like this again, looking at The Docs for any Java Classwill tell you exactly what you need to import with a little diagram that looks like this:
如果您再次遇到这种情况,查看任何 Java 类的文档都会通过一个如下所示的小图准确地告诉您需要导入的内容:
java.lang.Object
java.util.EventObject
java.awt.AWTEvent
java.awt.event.ActionEvent