Netbeans: using GUI Builder on regular Java class file

大憨熊 提交于 2019-12-04 04:03:30

问题


I'm using Netbeans. When I create a Java class, I sometimes want to change it to be a GUI component so that I can visually edit it using the GUI Builder.

What is the necessary step to transform a regular Java class to a GUI component so that Netbeans would recognize it and allow me to use GUI Builder ? (i.e. switch between Source and Design)


回答1:


NetBeans' Gui builder, Matisse, works off a .form xml file located adjacent to the source file. Matisse creates and maintains the .form file and the code generator creates/updates methods in the java source to reflect changes to the form.

Unfortunately, there is no support in NetBeans for free-form GUI construction.

The closest I've seen is FormGenerator. It's a contributed NetBeans module that adds a right click action to .java files that will attempt to generate a .form file from the .java source. It's very limited, but it's better than nothing. It works best if you've followed the coding style employed by Matisse.

http://netbeans.org/projects/contrib/downloads/download/Readme.txt http://netbeans.org/projects/contrib/downloads/download/FormGeneratorModule.zip




回答2:


To add a class to the Palette, all that's needed is for your class to conform to the Java Beans model. That is, your class must:

  • be serializable
  • have a public, no-argument constructor.

All fields that have getter and setter methods that are named properly, i.e.:

int count
int getCount()
void setCount(int c)

should by default be recognized as a property.

For a finer control of what properties should and should not be exposed to the GUI Builder, you can associate your class with an implementation of the BeanInfo interface. See this Sun tutorial for more details.

However, NetBeans has several tools to help you in designing a custom bean. You can create new beans using the built-in templates available in the new file dialog, under the "JavaBeans Objects" folder. This tutorial will guide you through creating an Image Bean.

What you could do is create one from scratch, design it as you wish, and then look at the generated code to understand how you can modify your existing class.




回答3:


Try to use properties (Java bean!) for properties which should be changed from the ui designer and look here for more info.



来源:https://stackoverflow.com/questions/1671778/netbeans-using-gui-builder-on-regular-java-class-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!