Java中无法使用Point2D类问题

Java API在javafx.geometry包中有一个便于使用的Point2D类,用于表示二维平面上的点。该类的UML图如下所示。

javafx.geometry.Point2D

+Point2D(x: double,y: double)		//用给定的x和y坐标来创建一个Point2D对象

+distance(x: double,y: double): double		//返回该点到定点(x,y)的距离

+distance(p: Point2D): double		//返回该点到p点的距离

+getX(): double				//返回该点的x的坐标

+getY(): double				//返回该点的y的坐标

+toString(): String				//返回该点的字符串表示

可以使用TestPoint2D.java文件对其功能进行验证

import java.util.*;
import javafx.geometry.Point2D;
public class TestPoint2D {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner input =new Scanner (System.in);
		System.out.print("Enter point1's x-, y-coordinates: ");
		double x1 = input.nextDouble();
		double y1 = input.nextDouble();
		System.out.print("Enter point2's x-, y-coordinates: ");
		double x2 = input.nextDouble();
		double y2 = input.nextDouble();
		
		Point2D p1 = new Point2D(x1, y1);
		Point2D p2 = new Point2D(x2, y2);
		System.out.println("P1 is " + p1.toString());
		System.out.println("P2 is " + p2.toString());
		System.out.println("The distance between p1 and p2 is " + p1.distance(p2));
	}

}

然而在eclipse中直接运行程序会出错,当import Point2D的时候显示没有此类。这是因为这个类是Eclipse的JRE带的,但并不属于JAVA的公开API。解决方法:

右击项目,选择最下行的Properties,然后点击java Compiler/Errors/Warnings.

在界面中点开Deprecated and restriced API将Forbidden reference (access rules) 改成Ignore。

然后在运行程序就没错了。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值