简体   繁体   English

如何在NetBeans中为编辑器折叠标签添加代码模板

[英]How to Add Code Template In NetBeans For editor-fold tag

正如问题所述,如何通过在netbeans中的Java编辑器中选择一些代码来制作代码模板或快捷方式,并通过编辑器折叠使提示看起来像是自动换行

netbeans选项图像

create with the below code reference 使用以下代码参考创建

 // <editor-fold defaultstate="collapsed" desc="${comment}">
 ${selection}${cursor}// </editor-fold>

When you got large source files it can sometimes be helpful to create foldings for certain sections of your code. 当您获得大型源文件时,有时可以为代码的某些部分创建折叠。 NetBeans got a nifty functionality for this using non-intrusive XML code (similar to Visual Studio). NetBeans使用非介入式XML代码(类似于Visual Studio)为此提供了一个很好的功能。

To create a custom code folding section simply insert the following before the content to fold: 要创建自定义代码折叠部分,只需在要折叠的内容之前插入以下内容:

//<editor-fold defaultstate="collapsed" desc="My custom code folding">

and the following after the content to fold: 和以下内容折叠后:

//</editor-fold>

So for example, if you had a JSF backing bean where you want to create a custom code folding for your properties, action listeners, and action handlers you could do as follows: 因此,例如,如果您有一个JSF支持bean,您想在其中为属性,动作侦听器和动作处理程序创建自定义代码折叠,则可以执行以下操作:

package backingbeans;

import javax.faces.model.*;
import javax.faces.event.*;

public class MyBackingBean {

    private String prop1 = "";

    public MyBackingBean() {
    }

    //<editor-fold defaultstate="collapsed" desc="Properties">
    public String getProp1() {
        return this.prop1;
    }

    public void setProp1(String prop1) {
        this.prop1 = prop1;
    }
   //</editor-fold>


   //<editor-fold defaultstate="collapsed" desc="Action listeners">
    public void myFirstListener(ActionEvent event) {
        ... do something ...
    }

   public void mySecondListener(ActionEvent event) {
        ... do something else ...
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="Action handlers">
    public String myFirstActionHandler() {
        ... do something ...
        return "OUTCOME1";
    }

    public String mySecondActionHandler() {
        ... do something else ...
        return "OUTCOME2";
    }
    //</editor-fold>
}

This will create three custom folders that are collapsed by default when the file is opened. 这将创建三个自定义文件夹,在打开文件时默认情况下它们会折叠。

If you like this, I'd suggest setting up a coding template for inserting the XML code as it can otherwise be tricky to remember. 如果您喜欢这样,我建议您设置一个编码模板以插入XML代码,否则很难记住。 This is done by following these steps: 通过执行以下步骤来完成:

Click Tools -> Options

Select “Editor” from the top options

Select the “Code Templates” tab

Select “Java” in the Language dropdown

Click the “New” button

Enter the shorthand for inserting the template, for example I use ‘efold’

Click the “OK” button

The template has been created, now enter the following code into the Expanded Text textbox:

//<editor-fold defaultstate="collapsed" desc="${cursor}">

//</editor-fold>

Click the “OK” button.

You can try out the template by opening a Java source file, place the cursor where you want to insert the folding and type efold followed by the tab key. 您可以通过打开Java源文件来尝试模板,将光标放在要插入折叠的位置,然后键入efold,然后按Tab键。 It will now have inserted the complete editor-fold and place the cursor ready for you to enter a description for the folding. 现在它将插入完整的编辑器折叠,并准备好光标以供您输入折叠说明。

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

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