如何使用 NetBeans 在 Java 中的 initComponents() 方法中修改/添加代码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2438221/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to modify/add code to the initComponents() method in Java using NetBeans?
提问by Ahmad Farid
How to modify/add code to the initComponents()
method in Java on NetBeans? 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.
如何initComponents()
在 NetBeans 上的 Java 方法中修改/添加代码?当我尝试添加任何代码行时,该区域似乎是只读的,并且以灰色突出显示!这可能是为了安全,但我想有一种方法可以禁用它。
采纳答案by vkraemer
The initComponents() method is regenerated by the IDE as you create your UI in the GUI editor. The method is 'guarded' to prevent this regeneration from overwriting user written code.
在 GUI 编辑器中创建 UI 时,IDE 会重新生成 initComponents() 方法。该方法被“保护”以防止这种重新生成覆盖用户编写的代码。
There are a couple of ways to add code to this method, indirectly:
有几种方法可以间接地向该方法添加代码:
Drop a new component onto the design editor 'canvas' for the window.
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.
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. alt text http://blogs.sun.com/vkraemer/resource/code-properties.pngNote: 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().
将一个新组件拖放到窗口的设计编辑器“画布”上。
输入代码作为以下代码属性之一的一部分:Pre-Creation Code、Post-Creation Code、Pre-Init Code、Post-Init Code、Post-Listener Code、Pre-Population Code、Post-Population Code 和 After-All - 设置代码。
还有一些其他代码属性不会改变 initComponents() 方法......但可能非常有用:Pre-Declaration Code 和 Post-Declaration Code。 替代文本 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() 方法中的代码。
回答by PeterMmm
Yes the initComponents method is read only to keep full control for the IDE. You may add yours in the constructor right after initComponents.
是的,initComponents 方法是只读的,以保持对 IDE 的完全控制。您可以在 initComponents 之后立即将您的添加到构造函数中。
public class NewJFrame extends javax.swing.JFrame {
/** Creates new form NewJFrame */
public NewJFrame() {
initComponents();
myInitComponents();
}
public void myInitComponents() {
}
回答by Tomislav Nakic-Alfirevic
To allow changes in both the source and the Matisse GUI editor, NetBeans prevents editing in what it calls "guarded blocks". 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.
为了允许在源代码和 Matisse GUI 编辑器中进行更改,NetBeans 禁止在它所谓的“受保护的块”中进行编辑。虽然您可以想象 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 文件,但是:
- be sure to save the current version somewhere
- check that your changes didn't break something by opening the class in NetBeans visual editor once your done
- 确保将当前版本保存在某处
- 完成后,通过在 NetBeans 可视化编辑器中打开类来检查您的更改是否会破坏某些内容
回答by FacilityDerek
Presumably what you are doing is writing an application using the Matisse GUI tool.
大概您正在做的是使用 Matisse GUI 工具编写应用程序。
Matisse generates non editable code blocks. This is required by Matisse so that the tool remains synchronized with the code base.
Matisse 生成不可编辑的代码块。这是 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 的属性选项卡和一些代码插入选项。
回答by Mike GH
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). There seems to be very little detailed documentation available on using Matisse. So, I hope this will help others.
我通过反复试验发现,在用户看到面板之前需要完成的初始化应该添加为“预初始化代码”。就我而言,我需要填充一个下拉框(在 AWT 中称为“选择”)。关于使用 Matisse 的详细文档似乎很少。所以,我希望这会帮助其他人。
回答by GLarkin
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. Several lines of explicit code can be customized.
如果在 中的组件上Design View
右击,然后点击“ Customize Code
”选择,就可以修改代码中的InitComponent
代码了。可以自定义几行显式代码。
回答by Kartik Agarwal
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 类中。
回答by Stefanidis
- Close Netbeans
- Go to folder path where there is your form file
- Backup the 2 files with extensions ".form" and ".java"
- Edit the 2 files with extensions ".form" and ".java" in notepad editor. For example if your form name is "myForm" you must have the files "myForm.form" and "myForm.java" in the folder.
- The first file ".form" is xml file and the second ".java" is a code java file
- Edit carefully your code in both files save changes and open NETBEANS. Thats it!
- 关闭 Netbeans
- 转到您的表单文件所在的文件夹路径
- 备份扩展名为“.form”和“.java”的 2 个文件
- 在记事本编辑器中编辑扩展名为“.form”和“.java”的 2 个文件。例如,如果您的表单名称是“myForm”,则文件夹中必须有文件“myForm.form”和“myForm.java”。
- 第一个文件“.form”是xml文件,第二个“.java”是代码java文件
- 仔细编辑两个文件中的代码,保存更改并打开 NETBEANS。就是这样!