简体   繁体   English

如何在 netbeans 中更改不可编辑/生成的代码

[英]How to change non-editable/generated code in netbeans

I want to change the non-editable code in Netbeans ,我想更改 Netbeans 中的不可编辑代码,
I want to replace the我想更换
javax.swing.JTextFeild with ObservingTextField带有 ObservingTextField 的 javax.swing.JTextFeild
for which I have a class imported into my project in order to Implement a date picker为此,我将一个类导入到我的项目中以实现日期选择器
But Netbeans Does not allow me to edit the code .但是 Netbeans 不允许我编辑代码。 Please help请帮忙

// Variables declaration - do not modify                     
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable1;
`private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;`
// End of variables declaration   

Another option delete the special comments.另一个选项是删除特殊注释。 You can't see in NetBeans but if you open in other text editor (for example notepad) you will see that comments on the begining and ending of non editable section.您在 NetBeans 中看不到,但是如果您在其他文本编辑器(例如记事本)中打开,您将看到关于不可编辑部分的开头和结尾的注释。 The comments look like this:评论看起来像这样:

//GEN-BEGIN:initComponents
//GEN-END:initComponents

If you want to add custom component to a file that uses the Netbeans GUI editor (called: Matisse), there are several ways:如果要将自定义组件添加到使用 Netbeans GUI 编辑器(称为:Matisse)的文件中,有以下几种方法:

First:第一的:

You could add that component to the "Palette" manager.您可以将该组件添加到“调色板”管理器。 I, personally, have had limited success with this, especially when adding components from my own, custom libraries.我个人在这方面取得的成功有限,尤其是在从我自己的自定义库中添加组件时。 However, you can add components and just select "from a project", then select your project and you should see your component listed (if you don't, run a clean & build).但是,您可以添加组件并只选择“从项目中”,然后选择您的项目,您应该会看到您的组件列出(如果没有,请运行干净并构建)。

Notice in this project I have two files, CustomTextField and NewJFrame .注意在这个项目中我有两个文件, CustomTextFieldNewJFrame

在此处输入图片说明

The code for CustomTextField is just something that extends a JTextField : CustomTextField 的代码只是扩展JTextField东西:

package test4;

import javax.swing.JTextField;

public class CustomTextField extends JTextField {

}

Then, in the Palette window, you right click and click "Palette Manager..."然后,在调色板窗口中,右键单击并单击“调色板管理器...”

在此处输入图片说明

Then in the Palette Manager, you click "Add from Project..." (if you are adding from a library, you click "Add from Library...", etc.然后在调色板管理器中,单击“从项目添加...”(如果是从库添加,则单击“从库添加...”等。

在此处输入图片说明

Then I select CustomTextField然后我选择CustomTextField

在此处输入图片说明

And boom there it is in the Palette Manager, and I can drag & drop it into my GUI.它就在调色板管理器中,我可以将它拖放到我的 GUI 中。

在此处输入图片说明

Note: this way is not very good if you need this component across multiple projects.注意:如果您需要跨多个项目使用此组件,这种方式不是很好 The palette manager is for Netbeans as a whole, so if you try to use a component that exists in another project, you'll have trouble.调色板管理器作为一个整体用于 Netbeans,因此如果您尝试使用存在于另一个项目中的组件,则会遇到麻烦。

Note #2: I had problems with Java Web Start and my own custom library.注意#2:我在使用 Java Web Start 和我自己的自定义库时遇到了问题。 There is a bug that was released with 7u25 that causes a NPE from the JNLPClassLoader whenever my custom libraries are loaded, so I had to import the components from a Project instead of a Library. 7u25 发布了一个错误,每当加载我的自定义库时,都会导致来自 JNLPClassLoader 的 NPE ,因此我必须从项目而不是库中导入组件。

Second:第二:

There is a more hackish way of doing things, but sometimes it's necessary: In the GUI-editor, you can right-click the component and click "Customize code."有一种更骇人听闻的做事方式,但有时是必要的:在 GUI 编辑器中,您可以右键单击组件并单击“自定义代码”。在此处输入图片说明 You can then change the variable definition ( not the declaration ).然后您可以更改变量定义(而不是声明)。 If you need to change the variable declaration , then it gets even MORE hackish, and I would recommend you just hand-write your code at that point.如果您需要更改变量声明,那么它会变得更加骇人听闻,我建议您此时只需手写代码。 But, to do it through this window, you can "comment off" the portion in "variable declaration code" by adding /* and */ around private javax.swing.JTextField jTextField1;但是,要通过此窗口执行此操作,您可以通过在private javax.swing.JTextField jTextField1;周围添加/**/来“注释掉”“变量声明代码”中的部分private javax.swing.JTextField jTextField1; and add your own variable declaration.并添加您自己的变量声明。

Third:第三:

Your other option is to hand write.您的另一个选择是手写。 :) If you need simple Swing components or components that can be added to the palette, I recommend the GUI Editor. :) 如果您需要简单的 Swing 组件或可以添加到面板中的组件,我推荐使用 GUI 编辑器。 If you need more complex and customized Swing components, then you'll want to start hand writing this code.如果您需要更复杂和自定义的 Swing 组件,那么您将需要开始手写此代码。 Most people will recommend you do so anyways.无论如何,大多数人都会建议您这样做。 I, however, proudly love Netbeans` GUI Editor. 然而,我自豪地喜欢 Netbeans 的 GUI 编辑器。

It depends on WHY netbeans is preventing you from editing that source file.这取决于为什么 netbeans 阻止您编辑该源文件。

  1. It is part of some library you just import into your project.它是您刚刚导入到项目中的某个库的一部分。 This means your project is really using the compiled class/jar files.这意味着您的项目实际上使用的是编译好的类/jar 文件。 There is no point in editing this file.编辑此文件没有意义。 Find the author of the library and file a change request.查找库的作者并提交更改请求。 This also applies for the java API itself.这也适用于 java API 本身。 Of course the chances that they will actually accept the change request might be slim.当然,他们实际接受更改请求的机会可能很小。

  2. It is write protected on the disk for no special reason.它在磁盘上没有特殊原因被写保护。 => make it writable => 使其可写

  3. It is controlled by some version control system which prevents editing before checking a file out of the system.它由一些版本控制系统控制,在将文件检出系统之前防止编辑。 Well check it out.看看吧。

  4. It is some file generated by netbeans (which might actually be the case, judging from the source code you posted).它是由 netbeans 生成的一些文件(从您发布的源代码来看,实际上可能是这种情况)。 In this case you probably can edit it in another editor (notepad/vi), but your changes will probably be overwritten on the next code generation, or break the file for whatever tools you use.在这种情况下,您可能可以在另一个编辑器 (notepad/vi) 中对其进行编辑,但是您的更改可能会在下一次代码生成时被覆盖,或者您使用的任何工具都会破坏文件。 => Find the tool and how to apply the changes appropriately with that tool. => 找到该工具以及如何使用该工具适当地应用更改。

Net beans let's you use custom code there from the form view page. Net bean 让您可以在表单视图页面中使用自定义代码。 I'm not on a computer with net beans right now but if you right click on the text area and its near the bottom我现在不在一台带有 net beans 的计算机上,但是如果您右键单击文本区域及其靠近底部的位置

When you open a form file in netbeans, this point to the java file inteast of the form file.当您在 netbeans 中打开一个表单文件时,这指向该表单文件的 java 文件 inteast。

The solution for edit this non-editable files is:编辑此不可编辑文件的解决方案是:

  1. Open Netbeans and go > Tools > Options > Miscellaneous打开 Netbeans 并转到 > 工具 > 选项 > 杂项
  2. Files Create new File Extension>form> and in Associated file mime select: HTML Files文件创建新的文件扩展名>表单>并在关联文件mime中选择:HTML文件

I use a simple workaround to edit the file.我使用一个简单的解决方法来编辑文件。 First I copy the entire code in notepad and the I delete the class.首先,我将整个代码复制到记事本中,然后删除该类。 Then I create a Java class and paste the code back.然后我创建一个 Java 类并将代码粘贴回去。 Keep in mind though you won't get the GUI builder now.请记住,尽管您现在无法获得 GUI 生成器。 So keep in mi所以留在我

Easiest method to done it.最简单的方法来完成它。

You can Simply done it by using ur text editor instead of netbeans.您可以使用您的文本编辑器而不是 netbeans 来简单地完成它。 but few things to remind::::但有几件事要提醒::::

1-Note the line of code in netbeans. 1-注意netbeans 中的代码行。

2-Close the netbeans IDE. 2-关闭netbeans IDE。

3-then open the file you want to change other text editor in your system. 3-然后打开要更改系统中其他文本编辑器的文件。


In my case i need to change Registration.java file so i open it with text editor就我而言,我需要更改 Registration.java 文件,以便使用文本编辑器打开它


4- Find the line of code you need to change 4- 找到您需要更改的代码行

5- change the code 5-更改代码

6- Save the file 6-保存文件

Through this way you can done.通过这种方式你可以做到。 Hope you will find the solution希望你能找到解决办法

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

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