简体   繁体   English

如何使用 NetBeans 在 Java 中的 initComponents() 方法中修改/添加代码?

[英]How to modify/add code to the initComponents() method in Java using NetBeans?

How to modify/add code to the initComponents() method in Java on NetBeans?如何在 NetBeans 上的 Java 中修改/向initComponents()方法添加代码? When I try to add any line of code this area seems to be like readonly and it's highlighted in gray!当我尝试添加任何代码行时,该区域似乎是只读的,并且以灰色突出显示! It's for security probably, but I suppose there is a way to disable that.这可能是为了安全,但我想有一种方法可以禁用它。

Yes the initComponents method is read only to keep full control for the IDE.是的,initComponents 方法是只读的,以保持对 IDE 的完全控制。 You may add yours in the constructor right after initComponents.您可以在 initComponents 之后立即将您的添加到构造函数中。

public class NewJFrame extends javax.swing.JFrame {

/** Creates new form NewJFrame */
public NewJFrame() {
    initComponents();
    myInitComponents();
}

public void myInitComponents() {
}

The initComponents() method is regenerated by the IDE as you create your UI in the GUI editor.在 GUI 编辑器中创建 UI 时,IDE 会重新生成 initComponents() 方法。 The method is 'guarded' to prevent this regeneration from overwriting user written code.该方法被“保护”以防止这种重新生成覆盖用户编写的代码。

There are a couple of ways to add code to this method, indirectly:有几种方法可以间接地向该方法添加代码:

  1. Drop a new component onto the design editor 'canvas' for the window.将一个新组件拖放到窗口的设计编辑器“画布”上。

  2. Enter code as part of one of the following code properties: Pre-Creation Code, Post-Creation Code, Pre-Init Code, Post-Init Code, Post-Listener Code, Pre-Population Code, Post-Population Code and After-All-Set Code.输入代码作为以下代码属性之一的一部分:Pre-Creation Code、Post-Creation Code、Pre-Init Code、Post-Init Code、Post-Listener Code、Pre-Population Code、Post-Population Code 和 After-All - 设置代码。

    There are a couple other code properties that do not alter the initComponents() method... but can be very useful: Pre-Declaration Code and Post-Declaration Code.还有一些其他代码属性不会改变 initComponents() 方法......但可能非常有用:Pre-Declaration Code 和 Post-Declaration Code。 alt text http://blogs.sun.com/vkraemer/resource/code-properties.png Note: the editor for these properties is not 'rich', so I would recommend creating methods in the "regular editor" that you just call in the initComponents(). 替代文本 http://blogs.sun.com/vkraemer/resource/code-properties.png注意:这些属性的编辑器不是“丰富的”,所以我建议在您刚刚调用的“常规编辑器”中创建方法在 initComponents() 中。

You can modify the code in the initComponents() method by positioning or changing the 'regular' properties of the 'base panel' or controls.您可以通过定位或更改“基本面板”或控件的“常规”属性来修改 initComponents() 方法中的代码。

Presumably what you are doing is writing an application using the Matisse GUI tool.大概您正在做的是使用 Matisse GUI 工具编写应用程序。

Matisse generates non editable code blocks. Matisse 生成不可编辑的代码块。 This is required by Matisse so that the tool remains synchronized with the code base.这是 Matisse 所要求的,以便工具与代码库保持同步。

There are a number of options provided by Matisse to allow insertion of custom code before, after or within code blocks such as initComponents() . Matisse 提供了许多选项来允许在代码块之前、之后或内部插入自定义代码,例如initComponents()

See the image below:见下图:

代码插入

This shows the properties tab for a jPanel and some of the code insertion options.这显示了 jPanel 的属性选项卡和一些代码插入选项。

To allow changes in both the source and the Matisse GUI editor, NetBeans prevents editing in what it calls "guarded blocks".为了允许在源代码和 Matisse GUI 编辑器中进行更改,NetBeans 禁止在它所谓的“受保护的块”中进行编辑。 While you could imagine the IDE being able to interpret almost any kind of GUI code you write, in practice, it is much easier for the IDE developers to encapsulate the automatically generated GUI code in a single method (initComponents()) and leave the rest for you to edit.虽然您可以想象 IDE 能够解释您编写的几乎任何类型的 GUI 代码,但实际上,IDE 开发人员将自动生成的 GUI 代码封装在单个方法 (initComponents()) 中并保留其余部分要容易得多供您编辑。

If you are certain you know what you're doing, you can easily edit the .java file from an external editor, but:如果您确定自己知道自己在做什么,则可以从外部编辑器轻松编辑 .java 文件,但是:

  1. be sure to save the current version somewhere确保将当前版本保存在某处
  2. check that your changes didn't break something by opening the class in NetBeans visual editor once your done完成后,通过在 NetBeans 可视化编辑器中打开类来检查您的更改是否会破坏某些内容

If you right-click on the component in the Design View , and then click on " Customize Code " selection, you can modify the code in the InitComponent code.如果在Design View右键单击该组件,然后单击“ Customize Code ”选择,则可以修改InitComponent代码中的代码。 Several lines of explicit code can be customized.可以自定义几行显式代码。

I discovered by trial and error, that initialization which needs to be done before the user sees the panel should be added as "Pre-Init Code".我通过反复试验发现,在用户看到面板之前需要完成的初始化应该添加为“预初始化代码”。 In my case, I needed to populate a drop down box (called "Choice" in AWT).就我而言,我需要填充一个下拉框(在 AWT 中称为“选择”)。 There seems to be very little detailed documentation available on using Matisse.关于使用 Matisse 的详细文档似乎很少。 So, I hope this will help others.所以,我希望这会帮助其他人。

select all of the code and copy in an external editor like gedit or notepad.选择所有代码并复制到 gedit 或记事本等外部编辑器中。

Then delete your jframe file.然后删除您的 jframe 文件。

Create a new java class in netbeans in the same package with the same name.在同一个包中的 netbeans 中创建一个具有相同名称的新 java 类。

Copy all the contents from the editor file and paste it in your newly created java class.复制编辑器文件中的所有内容并将其粘贴到新创建的 java 类中。

  1. Close Netbeans关闭 Netbeans
  2. Go to folder path where there is your form file转到您的表单文件所在的文件夹路径
  3. Backup the 2 files with extensions ".form" and ".java"备份扩展名为“.form”和“.java”的 2 个文件
  4. Edit the 2 files with extensions ".form" and ".java" in notepad editor.在记事本编辑器中编辑扩展名为“.form”和“.java”的 2 个文件。 For example if your form name is "myForm" you must have the files "myForm.form" and "myForm.java" in the folder.例如,如果您的表单名称是“myForm”,则文件夹中必须有文件“myForm.form”和“myForm.java”。
  5. The first file ".form" is xml file and the second ".java" is a code java file第一个文件“.form”是xml文件,第二个“.java”是代码java文件
  6. Edit carefully your code in both files save changes and open NETBEANS.仔细编辑两个文件中的代码,保存更改并打开 NETBEANS。 Thats it!就是这样!

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

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