java.awt.Dimension 类的使用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17389805/
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
Use of the java.awt.Dimension class
提问by uetoyo
I would like to use Dimension
class (java.awt.Dimension
), but it supports only integers. I want make Rectangle and Square classes like these:
我想使用Dimension
class ( java.awt.Dimension
),但它只支持整数。我想让 Rectangle 和 Square 类像这样:
Constructor:
构造函数:
public Rectangle(Dimension dim(double A, double B)) {
// constructor code
}
Is it better to write my own implementation of the Dimension class?
编写自己的 Dimension 类实现是否更好?
采纳答案by Andrew Thompson
Use Dimension.setSize(double,double)
instead.
回答by Oswald
Don't use java.awt.Dimension
just because it happens to provide an interface that looks similar to the one you need. As the introduction says in the documentation:
不要java.awt.Dimension
仅仅因为它提供了一个看起来与您需要的界面相似的界面就使用它。正如文档中的介绍所说:
The Dimension class encapsulates the width and height of a component ... in a single object.
Dimension 类将组件的宽度和高度...封装在单个对象中。
By component, a Java AWT Componentis meant. It seems weird to use a java.awt.Dimension
for anything else.
通过组件,一个Java的AWT组件的意思。将 ajava.awt.Dimension
用于其他任何事情似乎很奇怪。
If you provide your own implementation of java.awt.Dimension
, it must adhere to the same interface as the one provided by the Java Runtime environment. So you do not gain anything. BTW, there is no need to provide your own implementation of java.awt.Dimension
, the Java Runtime environment already provides a perfectly suitable implementation.
如果您提供自己的 实现java.awt.Dimension
,则它必须遵循与 Java 运行时环境提供的接口相同的接口。所以你什么也得不到。顺便说一句,没有必要提供您自己的 实现java.awt.Dimension
,Java 运行时环境已经提供了一个非常合适的实现。
Write your own class that can handle float values as width and height.
编写您自己的类,可以将浮点值处理为宽度和高度。
回答by jlordo
See JavaDoc:
请参阅JavaDoc:
The
Dimension
class encapsulates the width and height of a component (in integer precision) in a single object.
所述
Dimension
类封装组件的宽度和高度(在整数精度在一个单一的对象)。
So, if you need a Dimension class holding width and height in floating point precision, you can't use java.awt.Dimension
. Looking at the Methods java.awt.Dimension
provides it should be fairly easy to provide an own implementation.
因此,如果您需要以浮点精度保存宽度和高度的 Dimension 类,则不能使用java.awt.Dimension
. 查看方法java.awt.Dimension
提供,提供自己的实现应该相当容易。