我正在设计一个GUI来显示二叉搜索树。 GUI进行一些计算以使用调用printer.print(...)
在GUI帧上定位树节点。例如,彼此相隔一个深度的树节点彼此分开Y坐标vGap
。
某些计算也使用getWidth()
,但如果尚未配置UI的大小,则getWidth()
将返回0。这个事实在我最初使用的以下代码块中产生了一个问题:
myPrinter <Integer> printer = new myPrinter <> ();
printer.print(tree.root); // HERE
Construct frame = new Construct (printer);
// code to display the JFrame
// code to size the frame
在此块 中,使用printer.print(tree.root)
调用负责绘制搜索树的代码。但是,printer.print(tree.root)
在设置宽度之前访问getWidth()。因此,printer.print(tree.root)
使用getWidth(),就好像它是0.
为了避免这个问题,我不得不在绘制之前调整GUI大小。
Construct frame = new Construct (printer);
// code to display the JFrame
// code to size the frame
myPrinter <Integer> printer = new myPrinter <> ();
printer.print(tree.root); // HERE
如果我想让客户使用我的打印机,我必须限制我的客户使用printer.print(tree.root)
来构建和设置GUI框架。有没有办法让这个要求合同化或完全放弃它的需要?
我想到了一个解决方案:我将printer.print(someRoot root)
更改为printer.print(someRoot root, int frameWidth)
还有其他方法吗?