在哪里可以找到“构建”的源代码? Java 中的类?
在我的 Java 代码的开头,我有:
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.*;
import javax.swing.*;
然后我有以下内容:
public class KeyEventDemo extends JFrame
implements KeyListener,
ActionListener
我试图理解代码的作用。我从头开始。例如,我想知道 JFrame 类的定义(我认为它属于“java.awt.event”或“javax.swing”包。我还想查看接口 KeyListener 的定义。
我的问题是我不知道在哪里可以找到相应的源代码,我尝试了以下操作:
locate event | grep awt
没有找到任何内容,请。
In the beginning of my Java code I have:
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.*;
import javax.swing.*;
And then I have the following:
public class KeyEventDemo extends JFrame
implements KeyListener,
ActionListener
I am trying to understand what the code does. And I started from the beginning. For example I want to know there the class JFrame is defined (I think it belong either to "java.awt.event" or "javax.swing" package. I also would like to see hot the interface KeyListener is defined.
My problem is that I do not know where to find the corresponding source code. I tried the following:
locate event | grep awt
Nothing was found. Can anybody help we with that, pleas.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以使用 IDE(例如 Eclipse 或 Intellij)吗?它将极大地帮助您浏览代码以及 JDK 的代码。如果您还没有使用过,我强烈推荐它。
对于您的特定问题,awt 类是 JDK 包的一部分。您将需要 JDK 源代码。您可以在此处下载版本 6 源代码。
Do you have access to an IDE (such as Eclipse or Intellij) ? It will help enormously with navigating around your code, and that of the JDK. If you're not using one already I'd strongly recommend it.
For your particular question, the
awt
classes are part of the JDK package. You'll need the JDK source code. You can download version 6 source code here.源代码并不总是带有java,只是编译后的类。但是,您实际上并不需要源代码,您需要所使用的接口/类的描述。这些称为 API 文档。
这是 java 5 的 java api 文档:
http://java.sun.com/j2se/1.5.0/docs /api/
The source code doesn't always come with java, just the compiled classes. However you don't really need the source code, you need the description of the interfaces/classes used. These are called API documentation.
Here is the java api documentation for java 5:
http://java.sun.com/j2se/1.5.0/docs/api/
通常有一个名为 src.zip 的文件,其中包含所有源代码 - 尝试解压缩该文件,或者将您的 IDE 指向它,然后在其中查找。
There is usually a file called src.zip which contains all the source code - try unzipping that, or pointing your IDE at it, and looking in there.
源代码通常可以在 JDK 安装目录中名为
src.zip
的文件中找到。至少对于 Sun JDK 来说是这样。The source code can usually be found in a file called
src.zip
that is found inside your JDK installation directory. At least that's true for the Sun JDK.除非您从提供代码的网站下载,否则您看不到其代码。
您可以使用 Decompiler 查看构建文件中的代码
Unless you download from code providing website,you cant see its code.
You can use Decompiler for looking at thecode from the build files
需要考虑的一件事是,使用诸如
相反,尝试
这样的导入通常被认为是不好的做法,并为您需要的每个类执行一个导入语句。这提高了可维护性。它还将帮助您准确地确定需要在 API 中查找哪些类。我绝对同意您应该从 API 而不是源代码开始。根据我的经验,Swing 源代码的导航比 Swing Javadoc 更复杂。您想要做的是查看 JFrame、KeyEventListener 和 ActionListener 的 Javadoc。 Sun 的 Swing Trail 也有助于了解方法和原因,尤其是当谈到事件时。
One thing to consider is that it's generally considered bad practice to use an import such as
Instead, try:
and do one import statement per class that you need. This improves maintainability. It will also help you figure out exactly what classes you need to look up in the API. I would definitely agree that you should start with the API over the source code. The Swing source code is, in my experience, more complex to navigate through than the Swing Javadoc. What you want to do is check out the Javadoc for JFrame, KeyEventListener, and ActionListener. Sun's Swing Trail is also helpful in looking at the hows and whys, especially when it comes to events.