如何在Mac OS X上的Eclipse中使用Java Swing? [英] How do I use Java Swing in Eclipse on Mac OS X?

查看:3023
本文介绍了如何在Mac OS X上的Eclipse中使用Java Swing?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Mac OS X的新手,使用的是10.6.5版本。 JDK 1.6_u22似乎已经预装在系统上。我已经下载了Eclipse 3.5.2。



如果我创建一个简单的hello世界,但是我不能导入 JFrame 或使用Swing。我收到的错误消息是:


访问限制:JFrame类型由于对所需库/系统/ Java / JavaVirtualMachines / 1.6.0.jdk / Contents / Classes / classes.jar


这里是我试过的简单程序:

  import javax.swing.JFrame; 

public class Test {

public static void main(String [] args){
new JFrame();
}
}

如何在Mac OS上使用Java Swing X?

解决方案

在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?

同时验证你的Swing项目没有不小心包含SWT。

附录:检查这两个对话框:

  Eclipse>偏好设定> Java>构建路径
Eclipse>偏好设定> Java>安装的JRE

您应该看到对 / 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);
}
}


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.

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:

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

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?

解决方案

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?

Also verify that your Swing project hasn't inadvertently included 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/.

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);
    }
}

这篇关于如何在Mac OS X上的Eclipse中使用Java Swing?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆