java 矩形:为什么报错 "getPreferredSize()"
java Rectangle:why error "getPreferredSize()"
为什么错误代码:
layout.setConstraint(andy, new Rectangle(new Point(10, 10), andy.getPreferredSize()));
错误:
The constructor Rectangle(Point, Dimension) is undefined
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The constructor Rectangle(Point, Dimension) is undefined
The constructor Rectangle(Point, Dimension) is undefined
The constructor Rectangle(Point, Dimension) is undefined
at testdot.testdot.createDiagram(testdot.java:66)
at testdot.testdot.run(testdot.java:23)
at testdot.testdot.main(testdot.java:92)
我不明白这个错误。
请帮忙调试错误代码。
import java.awt.*;
import org.eclipse.draw2d.*;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class testdot {
private void run() {
Shell shell = new Shell(new Display());
shell.setSize(365, 280);
shell.setText("Genealogy");
shell.setLayout(new GridLayout());
Canvas canvas = createDiagram(shell);
canvas.setLayoutData(new GridData(GridData.FILL_BOTH));
Display display = shell.getDisplay();
shell.open();
while (!shell.isDisposed()) {
while (!display.readAndDispatch()) {
display.sleep();
}
}
}
private IFigure createPersonFigure(String name) {
RectangleFigure rectangleFigure = new RectangleFigure();
rectangleFigure.setBackgroundColor(ColorConstants.lightGray);
rectangleFigure.setLayoutManager(new ToolbarLayout());
rectangleFigure.setPreferredSize(100, 100);
rectangleFigure.add(new Label(name));
return rectangleFigure;
}
private Canvas createDiagram(Composite parent) {
// Create a root figure and simple layout to contain
// all other figures
Figure root = new Figure();
root.setFont(parent.getFont());
XYLayout layout = new XYLayout();
root.setLayoutManager(layout);
// Add the father "Andy"
IFigure andy = createPersonFigure("Andy");
root.add(andy);
layout.setConstraint(andy,
new Rectangle(new Point(10, 10), andy.getPreferredSize()));
// Add the mother "Betty"
IFigure betty = createPersonFigure("Betty");
root.add(betty);
layout.setConstraint(betty,
new Rectangle(new Point(230, 10), betty.getPreferredSize()));
// Add the son "Carl"
IFigure carl = createPersonFigure("Carl");
root.add(carl);
layout.setConstraint(carl,
new Rectangle(new Point(120, 120), carl.getPreferredSize()));
// Create a canvas to display the root figure
Canvas canvas = new Canvas(parent, SWT.DOUBLE_BUFFERED);
canvas.setBackground(ColorConstants.white);
LightweightSystem lws = new LightweightSystem(canvas);
lws.setContents(root);
return canvas;
}
public static void main(String[] args) {
new testdot().run();
}
}
您在代码中执行了 3 次此操作(或类似操作):
new Rectangle(new Point(10, 10), andy.getPreferredSize()));
不可编译Java。正如编译错误消息所说:
constructor Rectangle(Point, Dimension) is undefined
为什么这么说?毕竟java.awt.Rectangle
(javadoc).
中有一个Rectangle(Point, Dimension)
构造函数
好吧,让我们看看实际类型。
- 第一个参数显然是
java.awt.Point
。好的。
- 第二个参数是对
IFigure
实例的方法调用的结果。嗯。
如果您查看 IFigure
的文档,您会发现 getPreferredSize
方法 return 是一个 org.eclipse.draw2d.geometry.Dimension
对象,而不是 java.awt.Dimension
。
基本上,您混淆了 SWT 和 AWT 类型。
import java.awt.*;
这是错误的。因为您(显然)正在为 Eclipse/SWT 平台编程,所以您应该专门导入和使用 SWT 类。将 SWT 与 AWT(或 Swing 或 JavaFX 或 Android)api 混合使用会导致问题。
为什么错误代码:
layout.setConstraint(andy, new Rectangle(new Point(10, 10), andy.getPreferredSize()));
错误:
The constructor Rectangle(Point, Dimension) is undefined
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The constructor Rectangle(Point, Dimension) is undefined
The constructor Rectangle(Point, Dimension) is undefined
The constructor Rectangle(Point, Dimension) is undefined
at testdot.testdot.createDiagram(testdot.java:66)
at testdot.testdot.run(testdot.java:23)
at testdot.testdot.main(testdot.java:92)
我不明白这个错误。 请帮忙调试错误代码。
import java.awt.*;
import org.eclipse.draw2d.*;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class testdot {
private void run() {
Shell shell = new Shell(new Display());
shell.setSize(365, 280);
shell.setText("Genealogy");
shell.setLayout(new GridLayout());
Canvas canvas = createDiagram(shell);
canvas.setLayoutData(new GridData(GridData.FILL_BOTH));
Display display = shell.getDisplay();
shell.open();
while (!shell.isDisposed()) {
while (!display.readAndDispatch()) {
display.sleep();
}
}
}
private IFigure createPersonFigure(String name) {
RectangleFigure rectangleFigure = new RectangleFigure();
rectangleFigure.setBackgroundColor(ColorConstants.lightGray);
rectangleFigure.setLayoutManager(new ToolbarLayout());
rectangleFigure.setPreferredSize(100, 100);
rectangleFigure.add(new Label(name));
return rectangleFigure;
}
private Canvas createDiagram(Composite parent) {
// Create a root figure and simple layout to contain
// all other figures
Figure root = new Figure();
root.setFont(parent.getFont());
XYLayout layout = new XYLayout();
root.setLayoutManager(layout);
// Add the father "Andy"
IFigure andy = createPersonFigure("Andy");
root.add(andy);
layout.setConstraint(andy,
new Rectangle(new Point(10, 10), andy.getPreferredSize()));
// Add the mother "Betty"
IFigure betty = createPersonFigure("Betty");
root.add(betty);
layout.setConstraint(betty,
new Rectangle(new Point(230, 10), betty.getPreferredSize()));
// Add the son "Carl"
IFigure carl = createPersonFigure("Carl");
root.add(carl);
layout.setConstraint(carl,
new Rectangle(new Point(120, 120), carl.getPreferredSize()));
// Create a canvas to display the root figure
Canvas canvas = new Canvas(parent, SWT.DOUBLE_BUFFERED);
canvas.setBackground(ColorConstants.white);
LightweightSystem lws = new LightweightSystem(canvas);
lws.setContents(root);
return canvas;
}
public static void main(String[] args) {
new testdot().run();
}
}
您在代码中执行了 3 次此操作(或类似操作):
new Rectangle(new Point(10, 10), andy.getPreferredSize()));
不可编译Java。正如编译错误消息所说:
constructor Rectangle(Point, Dimension) is undefined
为什么这么说?毕竟java.awt.Rectangle
(javadoc).
Rectangle(Point, Dimension)
构造函数
好吧,让我们看看实际类型。
- 第一个参数显然是
java.awt.Point
。好的。 - 第二个参数是对
IFigure
实例的方法调用的结果。嗯。
如果您查看 IFigure
的文档,您会发现 getPreferredSize
方法 return 是一个 org.eclipse.draw2d.geometry.Dimension
对象,而不是 java.awt.Dimension
。
基本上,您混淆了 SWT 和 AWT 类型。
import java.awt.*;
这是错误的。因为您(显然)正在为 Eclipse/SWT 平台编程,所以您应该专门导入和使用 SWT 类。将 SWT 与 AWT(或 Swing 或 JavaFX 或 Android)api 混合使用会导致问题。