是否可以定义自己的“控制器”?在 Eclipse 的 Window Builder 中?
我想同时使用 Window Builder 和 MVC 范例。当 Window Builder 将所有代码添加到一个文件中时,复杂的窗口会变得非常混乱。
我希望创建的默认文件是“视图”。
我想将我的控制操作(事件侦听器)保留在“控制器”类中。有没有一种方法可以让 Window 生成器自动将事件侦听器放入您选择的类中,而不是添加到一个整体文件中?
I would like to use Window Builder and use the MVC paradigm simultaneously. It is very messy with a complex window when Window Builder adds all the code to just one file.
I would like the default file created to be the 'view'.
I would like to keep my control actions (event listeners) in a 'controller' class. Is there a way to have Window builder automatically put the event listeners in a class of your choice rather than adding to one monolithic file?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不知道 Eclipse 的 Window Builder 是如何工作的,但我确实知道 NetBean 创建匿名内部类,这些类为每个按钮调用自定义方法,然后允许程序员更改自定义方法的主体。如果 Eclipse 类似,那么您可以简单地让这个自定义方法调用您的 Control 对象的方法。当然,它增加了一个间接层,但为了让您完全控制自己的控制权,付出的代价很小。
例如,如果我创建一个名为“myButton”的 JButton,然后让代码生成器为我的按钮创建一个操作,它将创建以下代码:
并允许我在生成的方法 myButtonActionPerformed:
和内部 访问和编写代码我会将此方法称为我的控件的方法:
控件类可能类似于
GUI 需要一个 setControl(MyControl myControl) 方法,以便将控件“注入”到 GUI 中。
I don't know how Eclipse's Window Builder works, but I do know that NetBean's creates anonymous inner classes that call a custom method for each button and then allows the programmer to alter the body of the custom method. If Eclipse is similar, then you can simply have this custom method call a method of your Control object. Sure it adds a layer of indirection, but it's a small price to pay to give you complete control over your control.
For instance, if I create a JButton called "myButton" and then have the code generator create an action for my button, it will create this code:
and will allow me to access and write code in the generated method, myButtonActionPerformed:
And inside of this method I would call my Control's method:
The control class could look something like
The GUI would need a setControl(MyControl myControl) method in order to "inject" the control into the GUI.
拆分代码的另一种方法是使用向导创建 JPanel 作为您自己的控件,然后使用调色板中的“选择组件”工具(看起来像三个 bean)将它们添加到主窗口的布局中。简单而且干净得多。虽然我理解它仍然合并了MVC中的“V”和“C”......
Another way of splitting up your code is to use the wizard to create JPanels as your own controls, then add those in a layout in the main window using the "Choose component" tool (looks like three beans) from the palette. Easy and much cleaner. Although I understand it still merges the "V" and "C" in MVC...