为什么导入 javax.swing.* 而不是 java.swing.*

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2004759/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 02:23:04  来源:igfitidea点击:

Why import javax.swing.* instead of java.swing.*

javaswingpackage

提问by giri

I did not understand why there is "x" appended to "java" for few of the import packages. What is the reason? Can't it be just java.swing.*like others java.io.*?

我不明白为什么少数导入包在“java”后面附加了“x”。是什么原因?不能java.swing.*和别人一样java.io.*吗?

采纳答案by Jon Skeet

Swing was originally an extension to Java - a separately downloadable library. It became part of the "main" JRE in Java 1.2. It would have been odd for a separate library to have claimed a java.* package, hence the current situation. There are plenty of other extensions which have a similar story.

Swing 最初是 Java 的扩展 - 一个单独的可下载库。它成为 Java 1.2 中“主要”JRE 的一部分。一个单独的库声明一个 java.* 包会很奇怪,因此是当前的情况。还有很多其他的扩展都有类似的故事。

The Wikipedia article on Swinghas a bit more information on its history.

Swing的维基百科的文章有其历史提供更多的信息。

回答by Ahe

For historical reasons and backward compatibility. Before java 1.2 swing was distributed as an separate package from java (hence javax as extension).

由于历史原因和向后兼容性。在 java 1.2 swing 作为独立于 java 的包分发之前(因此 javax 作为扩展)。

When swing became part of standard java-distribution package name was kept for backward compatibility. More info in wikipediaarticle

当 Swing 成为标准 java-distribution 包名称的一部分时,为了向后兼容而保留。维基百科文章中的更多信息

回答by JRL

From Core Java 2:
alt text http://bks5.books.google.fr/books?id=W6bomXWB-TYC&printsec=frontcover&img=1&zoom=5&edge=curl&sig=ACfU3U3IeRSfENUVokGf-d9GUZZBU-tYMQ

来自 Core Java 2:
替代文本 http://bks5.books.google.fr/books?id=W6bomXWB-TYC&printsec=frontcover&img=1&zoom=5&edge=curl&sig=ACfU3U3IeRSfENUVokGf-d9GUZZBU-tYMQ

The name javaxindicates a Java extension package, not a core package. The Swing classes are indeed an extension to Java 1.1. Because the Swing classes were not part of the core hierarchy, it is possible to load the Swing classes into a Java 1.1-compatible browser.(the security manager of the browser does not allow adding any packages that start with "java.".) On the Java 2 platform, the Swing package is no longer an extension, but is instead part of the core hierarchy. Any Java implementation that is compatible with Java 2 must supply the Swing classes. Nevertheless, the javax name remains, for compatibility with Java 1.1 code. (Actually, the Swing package started out as com.sun.java.swing, then briefly got moved to java.awt.swingduring early Java 2 beta versions, then went back to com.sun.java.swingin late Java 2 beta versions, and after howls of protest by Java programmers, found its final resting place in javax.swing.)

该名称javax表示 Java 扩展包,而不是核心包。Swing 类确实是 Java 1.1 的扩展。因为 Swing 类不是核心层次结构的一部分,所以可以将 Swing 类加载到与 Java 1.1 兼容的浏览器中。(浏览器的安全管理器不允许添加任何以“ java.”开头的包。) Java 2 平台,Swing 包不再是一个扩展,而是核心层次结构的一部分。任何与 Java 2 兼容的 Java 实现都必须提供 Swing 类。尽管如此,为了与 Java 1.1 代码兼容,javax 名称仍然存在。(实际上,Swing 包开始时是com.sun.java.swing,然后java.awt.swing在早期的 Java 2 beta 版本中被短暂转移到,然后又回到com.sun.java.swing在 Java 2 beta 后期版本中,在 Java 程序员的强烈抗议之后,它最终在javax.swing.)

回答by OscarRyz

The xwas because they were extensions. In version 1.1 of java there was not swing and when it was added they decided to name it like that: javax

X是因为他们扩展。在 java 1.1 版本中没有 Swing,当它被添加时,他们决定这样命名:javax

There are a number of packages with similar histories.

有许多具有相似历史的软件包。