我什么时候需要导入 java.awt.*?如果我想导入 javax.swing.* 是否还需要导入 java.awt.*?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20207360/
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
When do I need to import java.awt.*? If I want to import javax.swing.* do I need to also import java.awt.*?
提问by krisharmas
I'm getting back into Java and reviewing some of my old code and I'm seeing a lot of places where I have done
我正在重新使用 Java 并查看我的一些旧代码,并且我看到了很多我已经完成的地方
import javax.swing.*;
import java.awt.*;
or actually importing specific classes from the swing/awt packages. I can't remember if I need to import java.awt.* to use anything in javax.swing.* or would I have imported java.awt.* for another reason? In other words, what is the purpose of importing java.awt.* ? I was under the impression everything I needed to use JButton, JFrame, etc. was in javax.swing.*.
或者实际上从swing/awt 包中导入特定的类。我不记得是否需要导入 java.awt.* 才能使用 javax.swing.* 中的任何内容,或者我是否会出于其他原因导入 java.awt.*?换句话说,导入 java.awt.* 的目的是什么?我的印象是使用 JButton、JFrame 等所需的一切都在 javax.swing.* 中。
采纳答案by Hovercraft Full Of Eels
You import awt if you use awt types, no more, no less.
如果您使用 awt 类型,则导入 awt,不多也不少。
I can't remember if I need to import java.awt.* to use anything in javax.swing.* or would I have imported java.awt.* for another reason? In other words, what is the purpose of importing java.awt.* ?
我不记得是否需要导入 java.awt.* 才能使用 javax.swing.* 中的任何内容,或者我是否会出于其他原因导入 java.awt.*?换句话说,导入 java.awt.* 的目的是什么?
The purpose is to use any classes in the AWT library. Most would recommend that you import only the specific classes that you're using, no more, no less.
目的是使用 AWT 库中的任何类。大多数人会建议您只导入您正在使用的特定类,不多也不少。
I was under the impression everything I needed to use JButton, JFrame, etc. was in javax.swing.*.
我的印象是使用 JButton、JFrame 等所需的一切都在 javax.swing.* 中。
This will almost never be true. Most Java programs that are more than toy programs will need to use many libraries. For instance, most layout classes are held in the AWT library. Most listeners are held in the java.awt.event
library.
这几乎永远不会是真的。大多数 Java 程序不仅仅是玩具程序,还需要使用许多库。例如,大多数布局类都保存在 AWT 库中。大多数听众都在java.awt.event
图书馆里。
Note that you don't have to import anythingif you choose to use fully qualified type names.
请注意,如果您选择使用完全限定的类型名称,则不必导入任何内容。
e.g.,
例如,
java.awt.BorderLayout myBorderLayout = new java.awt.BorderLayout(5, 5);
回答by Henry
These are from javax.swing.*
这些来自 javax.swing.*
JFrame
JButton
These are from java.awt.*
这些来自 java.awt.*
Component
Color
You can make a window without java.awt.*
你可以制作一个没有 java.awt.* 的窗口
public class Frame extends JFrame{
public static void main(String[] args){
new Frame();
}
public Frame(){
super("Frame");
this.setVisible(true);
}
}
If you wanted to, I prefer not to, you can use a fully qualified class name.
如果您愿意,我不想这样做,您可以使用完全限定的类名。
javax.swing.JFrame = new javax.swing.JFrame();
If you do this, you can make a program without importing anything.
如果你这样做,你可以在不导入任何东西的情况下制作一个程序。