简体   繁体   English

如何从 Netbeans 中删除自动生成的代码

[英]How to remove auto generated code from Netbeans

Whenever I create new JPanelForm , NetBeans will create some auto generated code in initComponents() method.每当我创建新的JPanelForm时,NetBeans 都会在initComponents()方法中创建一些自动生成的代码。 How can I remove this auto generated code from my JPanelForm ?如何从我的JPanelForm中删除这个自动生成的代码?

Using a GUI designer like Netbeans or Eclipse, will force you to accept certain conventions. 使用像Netbeans或Eclipse这样的GUI设计器会强制您接受某些约定。 One of these conventions is the automatic generation of initComponents() method by Netbeans. 其中一个约定是Netbeans自动生成initComponents()方法。

Although Netbeans is highly configurable and allows the user to modify lots of things, the initComponents() method is always used by the GUI builder. 尽管Netbeans具有高度可配置性并允许用户修改大量内容,但GUI构建器始终使用initComponents()方法。

When you create a JPanel Form in Netbeans this is the initComponents() you get by default: 在Netbeans中创建JPanel表单时,这是默认情况下获得的initComponents()

private void initComponents() {

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
    this.setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 400, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 300, Short.MAX_VALUE)
    );
}

If you take a close look at it you will see it has only layout instructions. 如果你仔细看看它,你会发现它只有布局说明。 You may want to change the layout. 您可能想要更改布局。 Let's say you want to have a BorderLayout for your JPanel . 假设你想为你的JPanel设置一个BorderLayout Go to the Navigator, change the layout by choosing the right property and this is your new initComponents() now: 转到导航器,通过选择正确的属性来更改布局,这是您的新initComponents()

private void initComponents() {

    setLayout(new java.awt.BorderLayout());
}

If someone is in a Swing learning phase, it is wiser to avoid using a GUI Builder. 如果有人处于Swing学习阶段,那么避免使用GUI Builder会更明智。 Designing the components by hand will give a better understanding of how things work. 手动设计组件可以更好地理解工作原理。 The GUI builder will always be there to automate procedures once the principles of Swing have become familiar. 一旦Swing的原理变得熟悉,GUI构建器将始终在那里自动化程序。

The Problem is that for a class XYZ with generated code there is a file XYZ.form (Same folder as the XYZ.java file). 问题是对于具有生成代码的XYZ类,有一个文件XYZ.form(与XYZ.java文件相同的文件夹)。 Netbeans unfortunately hides this file in it's Files / Projects Views, so you have to remove it by other means. 不幸的是,Netbeans在文件/项目视图中隐藏了这个文件,所以你必须通过其他方式删除它。 After the .form file is removed the design tab for this class should disappear in Netbeans and you can delete the auto-generated code. 删除.form文件后,此类的设计选项卡应在Netbeans中消失,您可以删除自动生成的代码。

Maybe you can check this post out, and find the answer you are looking for. 也许你可以查看这篇文章,找到你想要的答案。 Remove Netbeans Auto Generated Code After Double-Clicking an Object in Design Tab 双击设计选项卡中的对象后删除Netbeans自动生成的代码

在“设计”模式中,选择特定对象,然后单击“属性”下的“事件”,并从事件属性中删除事件。

I know this is a very old question, but I thought it may help you or someone else:)我知道这是一个非常古老的问题,但我认为它可能对您或其他人有所帮助:)

In NetBeans, when you go on a component, there should be a "Properties" section (You can also access it by right-clicking the component), in that section, you should find "Properties", "Events" and "Code", go to "Events" and you should find the events related to that component there.在 NetBeans 中,当你 go 在一个组件上时,应该有一个“属性”部分(你也可以通过右键单击该组件来访问它),在该部分中,你应该找到“属性”,“事件”和“代码” , go 到“事件”,您应该可以在那里找到与该组件相关的事件。 Find the even that you'd like to remove, and click the small 3 dotted button, you'll see the handler, click on it and then click remove.找到您要删除的 even,然后单击 3 点小按钮,您将看到处理程序,单击它,然后单击删除。 That should fix it through NetBeans. Below, I'll explain another method that sometimes works, but not always (I think).这应该通过NetBeans 修复它。下面,我将解释另一种有时有效但并非总是有效(我认为)的方法。

This is rather an unusual way, but I managed to work around the application and edited those parts.这是一种相当不寻常的方式,但我设法解决了该应用程序并编辑了这些部分。 What you can do is open the actual code file using any other text editor, using the "Open with" function (On windows), and search for the part that you want to change, then change it through that text editor, save the changes, and boom: You've tricked the application :)您可以做的是使用任何其他文本编辑器打开实际代码文件,使用“打开方式”function(在 Windows 上),然后搜索要更改的部分,然后通过该文本编辑器进行更改,保存更改,然后繁荣:你欺骗了应用程序 :)

However, keep in mind that this might cause some errors and you'd have to fix them yourself using the same method, because the code is built upon itself automatically obviously.但是,请记住,这可能会导致一些错误,您必须使用相同的方法自己修复它们,因为代码显然是自动构建的。 Remember that you're a problem solver, you should always try to think of a solution for your problems;)请记住,您是一个问题解决者,您应该始终尝试为您的问题想出解决方案;)

I hope this helps: Good luck!我希望这会有所帮助:祝你好运! Have a great day :)祝你有美好的一天 :)

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

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