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

2020-02-10 14:50发布

问题:

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.*?

回答1:

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.

The Wikipedia article on Swing has a bit more information on its history.



回答2:

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

The name javax indicates 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.swing during early Java 2 beta versions, then went back to com.sun.java.swing in late Java 2 beta versions, and after howls of protest by Java programmers, found its final resting place in javax.swing.)



回答3:

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

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



回答4:

The x was 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

There are a number of packages with similar histories.