如何在 Mac OS X 上的 Eclipse 中使用 Java Swing?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4611295/
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
How do I use Java Swing in Eclipse on Mac OS X?
提问by Jonas
I am new to Mac OS X and is using version 10.6.5. JDK 1.6_u22 seems to be preinstalled on the system. I have downloaded Eclipse 3.5.2.
我是 Mac OS X 的新手,使用的是 10.6.5 版。系统上似乎预装了 JDK 1.6_u22。我已经下载了 Eclipse 3.5.2。
It works fine if I create a simple hello world, but I can not import JFrame
or use Swing. The error message that I get is:
如果我创建一个简单的 hello world,它工作正常,但我无法导入JFrame
或使用 Swing。我得到的错误信息是:
Access restriction: The type JFrame is not accessible due to restriction on required library /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Classes/classes.jar
访问限制:由于对所需库/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Classes/classes.jar的限制,无法访问JFrame类型
Here is the simple program that I have tried with:
这是我尝试过的简单程序:
import javax.swing.JFrame;
public class Test {
public static void main(String[] args) {
new JFrame();
}
}
How do I use Java Swing in Eclipse on Mac OS X?
如何在 Mac OS X 上的 Eclipse 中使用 Java Swing?
回答by trashgod
On Mac OS X 10.5.8, Eclipse 3.4.2 and Xcode 3.1.4, the example below builds and runs using recent revisions of either Java 1.5 or Java 1.6. As Xcode includes the JDK, what version of Mac OS X and Xcode are you using?
在 Mac OS X 10.5.8、Eclipse 3.4.2 和 Xcode 3.1.4 上,以下示例使用 Java 1.5 或 Java 1.6 的最新版本构建和运行。由于 Xcode 包含 JDK,您使用的是什么版本的 Mac OS X 和 Xcode?
Also verify that your Swing project hasn't inadvertently included SWT.
还要验证您的 Swing 项目是否无意中包含了 SWT。
Addendum: Check these two dialogs:
附录:检查这两个对话框:
Eclipse > Preferences > Java > Build Path
Eclipse > Preferences > Java > Installed JREs
You should see references to /System/Library/Frameworks/JavaVM.framework/
.
您应该会看到对/System/Library/Frameworks/JavaVM.framework/
.
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
public class X {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new JPanel() {
private final int SIZE = 200;
private final int INSET = 20;
@Override
public Dimension getPreferredSize() {
return new Dimension(SIZE, SIZE);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.blue);
Line2D line1 = new Line2D.Double(INSET, INSET,
getWidth() - INSET, getHeight() - INSET);
Line2D line2 = new Line2D.Double(getWidth() - INSET,
INSET, INSET, getHeight() - INSET);
g2.setStroke(new BasicStroke(16,
BasicStroke.CAP_ROUND,
BasicStroke.JOIN_BEVEL));
g2.draw(line1);
g2.draw(line2);
}
});
frame.pack();
frame.setVisible(true);
}
}
回答by Andrew T Finnell
My best guess is that you included the runtime JAR files from Eclipse for JDK 1.6 but Eclipse is trying to run with JDK 1.5.
我最好的猜测是,您为 JDK 1.6 包含了来自 Eclipse 的运行时 JAR 文件,但 Eclipse 正在尝试使用 JDK 1.5 运行。
Eclipse uses SWT which relies on 32-bit binaries. The only 32-bit JVM on Mac OSX is 1.5 which means Eclipse has to run using JDK 1.5. It is plausible you set your project up to run with 1.5 and are trying to use 1.6 classes.jar
Eclipse 使用依赖于 32 位二进制文件的 SWT。Mac OSX 上唯一的 32 位 JVM 是 1.5,这意味着 Eclipse 必须使用 JDK 1.5 运行。您将项目设置为使用 1.5 运行并尝试使用 1.6 classes.jar 是合理的
In other words verify Eclipse is launching your application with the correct JVM.
换句话说,验证 Eclipse 是否使用正确的 JVM 启动您的应用程序。