是否可以在Window Builder for Eclipse中定义自己的“控制器"? [英] Is it possible to define your own 'controller' in Window Builder for Eclipse?

查看:71
本文介绍了是否可以在Window Builder for Eclipse中定义自己的“控制器"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想同时使用Window Builder和MVC范例.当Window Builder将所有代码添加到一个文件中时,对于一个复杂的窗口来说,这是非常混乱的.

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'.

我想将控制动作(事件侦听器)保留在"controller"类中.有没有一种方法可以让窗口"构建器自动将事件侦听器放入您选择的类中,而不是将其添加到一个整体文件中?

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?

推荐答案

我不知道Eclipse的Window Builder是如何工作的,但是我知道NetBean会创建匿名内部类,这些内部类为每个按钮调用自定义方法,然后允许程序员可以更改自定义方法的主体.如果Eclipse很相似,那么您只需让此自定义方法调用Control对象的方法即可.当然,它增加了一个间接层,但是要完全控制您的控制权,付出的代价很小.

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.

例如,如果我创建一个名为"myButton"的JButton,然后让代码生成器为我的按钮创建一个动作,它将创建以下代码:

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:

    myButton.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            myButtonActionPerformed(evt);
        }
    });

,并允许我在生成的方法myButtonActionPerformed中访问和编写代码:

and will allow me to access and write code in the generated method, myButtonActionPerformed:

private void myButtonActionPerformed(java.awt.event.ActionEvent evt) {
   // TODO add your handling code here:
}

在此方法中,我将调用控件的方法:

And inside of this method I would call my Control's method:

private void myButtonActionPerformed(java.awt.event.ActionEvent evt) {
   if (myControl != null) {
      myControl.myButtonAction();
   }
}

控件类可能类似于

class MyControl {

    void myButtonAction() {
        //TODO: implement control code
    }

}

GUI将需要一个setControl(MyControl myControl)方法,以便将控件注入"到GUI中.

The GUI would need a setControl(MyControl myControl) method in order to "inject" the control into the GUI.

这篇关于是否可以在Window Builder for Eclipse中定义自己的“控制器"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆