简体   繁体   English

如何重构Netbeans生成的GUI代码?

[英]How to refactor Netbeans generated GUI code?

I had created a GUI in Netbeans through Netbeans Swing GUI creator. 我通过Netbeans Swing GUI创建者在Netbeans中创建了一个GUI。 So I just dragged and dropped the Swing Components from the "palette" window and all the code was generated by netbeans. 所以我只是从“调色板”窗口拖放Swing组件,所有代码都是由netbeans生成的。

Now the code is too long to maintain (approx. 10,000 lines). 现在代码太长而无法维护(大约10,000行)。 So some experts on SO suggested me to refactor my code. 因此SO的一些专家建议我重构我的代码。

I can refactor the code that was generated by me but I don't know how to refactor the code generated by the Netbeans as It doesn't allow editing in its generated code. 我可以重构我生成的代码,但我不知道如何重构Netbeans生成的代码,因为它不允许在其生成的代码中进行编辑。

Any suggestions? 有什么建议么?

10.000 lines of code sounds like you have everything in that single class. 10.000行代码听起来就像你拥有该单一类中的所有内容。

Start by splitting your source into Model, View and Control ( MVC ). 首先将您的源分为模型,视图和控制( MVC )。


You might also be able to extract some JPanels into separate classes. 您也可以将一些JPanels提取到单独的类中。 One way to do this is to create a new JPanel (new file), and cut/paste your compoments from one main panel into that new JPanel . 一种方法是创建一个新的JPanel (新文件),并将您的组件从一个主面板剪切/粘贴到新的JPanel Save and compile your new panel. 保存并编译新面板。

Then go back to your main frame, select Beans -> Choose Bean from your Palette and choose the newly created class ( com.example.YourPanel for example). 然后返回主框架,从Palette选择Beans - > Choose Bean并选择新创建的类(例如com.example.YourPanel )。

Make sure to have a backup of your application before you try this. 在尝试此操作之前,请确保备份您的应用程序。

Well - if the code is generated, I don't see any advantages in refactoring it as long as the tool which generated it can handle it. 好吧 - 如果生成代码,只要生成它的工具可以处理它,我认为重构它没有任何优势。
The tool (meaning the designer in this case) will "destroy" all your refactoring work as soon as it updates the code. 该工具(在本例中为设计者)将在更新代码后立即“销毁”所有重构工作。

However, you should split your Control/Window/... into multiple controls - then the code will automatically get shorter and you will be able to maintain your UI more easily. 但是,您应该将Control / Window / ...拆分为多个控件 - 然后代码将自动缩短,您将能够更轻松地维护UI。

As a conclusion: Do not refactor the generated code but do refactor your control. 作为结论:不要重构生成的代码,而是重构您的控件。

Handcode the GUI code with layoutmanagers. 使用layoutmanagers手动编写GUI代码。

Using GUI builder tools, makes it nearly impossible to refactor GUI code. 使用GUI构建器工具,几乎不可能重构GUI代码。 I have to use these idiotic Intellij Swing GUI designer forms. 我必须使用这些愚蠢的Intellij Swing GUI设计器表单。 I now cannot even rename my packages in Eclipse because it wont be updated in the forms.XML file. 我现在甚至无法在Eclipse中重命名我的包,因为它不会在forms.XML文件中更新。

Stay away from GUI builders. 远离GUI构建器。 If you want to build really complex, maintainable GUIs then do it by hand by using GridBagLayout and all the rest. 如果你想构建非常复杂,可维护的GUI,那么可以使用GridBagLayout和其他所有GUI手动完成。

If you have to use netbeans, because of project limitations (eg the rest of the team is, or requirements say to) then use Matisse to break up the huge form into smaller panels, each of which the designer can edit. 如果你必须使用netbeans,由于项目限制(例如团队的其他成员,或要求说),然后使用Matisse将巨大的形式分解成更小的面板,设计师可以编辑每个面板。 You can do that by creating a new form, and cutting and pasting panels from the big form into the new form. 您可以通过创建新表单,从大表单切割和粘贴面板到新表单来实现。

But at the same time, make sure all the business logic is moved out of the UI classes. 但与此同时,请确保将所有业务逻辑移出UI类。

If you do not have to use matisse / netbeans, you can open the project in Eclipse, and edit the forms using WindowBuilder, it will do it in real java code instead of the uneditable form, so you can then chop and edit it to your heart's content. 如果您不必使用matisse / netbeans,您可以在Eclipse中打开项目,并使用WindowBuilder编辑表单,它将使用真正的Java代码而不是不可编辑的表单来执行,因此您可以将其剪切并编辑为您的心的内容。

You can extract the application logic into a separate subclass. 您可以将应用程序逻辑提取到单独的子类中。 Then, directly use the subclass. 然后,直接使用子类。 I succeeded with the following method. 我成功了以下方法。

  1. Members defined by us that are relevant to the application logic moved to the newly created subclass. 由我们定义的与应用程序逻辑相关的成员移动到新创建的子类。
  2. Components access modifier made "protected" (they are "private" by default). 组件访问修饰符被“保护”(默认情况下它们是“私有的”)。 To do so: Right click -> Properties -> Code (tab) -> Set "Variable modifier" to "protected" 为此:右键单击 - >属性 - >代码(选项卡) - >将“变量修饰符”设置为“受保护”
  3. Event handling methods moved to the subclass - When you are adding events to a component using properties pane it changes initComponents() function by adding the relevant code like in the following code sample. 事件处理方法移动到子类 - 当您使用属性窗格向组件添加事件时,它通过添加相关代码来更改initComponents()函数,如下面的代码示例所示。 Here definition of btnNum6ActionPerformed() is added to the class with an empty body. 这里将btnNum6ActionPerformed()的定义添加到具有空体的类中。 Unfortunately btnNum6ActionPerformed() is private and no way to change the access modifier using NetBeans IDE. 不幸的是,btnNum6ActionPerformed()是私有的,无法使用NetBeans IDE更改访问修饰符。 Hence, they cannot be overridden. 因此,它们不能被覆盖。 To get rid of this, you can define another intermediary function and call it inside btnNum6ActionPerformed(). 要摆脱这种情况,您可以定义另一个中间函数并在btnNum6ActionPerformed()中调用它。 It is better to make the base class and its intermediary event handling functions abstract. 最好使基类及其中间事件处理函数抽象化。

     btnNum6.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnNum6ActionPerformed(evt);//Definition of this method is added too } }); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM