WebGL Tutorial
and more

NetBeans使用技巧

撰写时间:2022-05-10

修订时间:2024-02-15

NetBeans是使用Java开发的IDE(Integrated Development Enviroment,集成开发环境),界面整洁,功能强大。支持Java, HTML5/JavaScript, C/C++, PHP等多种编程语言,且版本更新速度较快。

代码模板

自定义代码模板

菜单NetBeans - Preferences...,弹出Options窗口。单击Editor标签页,单击Code Templates标签页,在Language中选择XML。按New按钮,在弹出的New Code Template窗口的Abbreviation文本框中输入sect,单击确定按钮。New Code Template窗口关闭,并返回Options窗口。在该窗口Templates列表中,已自动选择刚刚输入的sect项。在下面的Expanded Text标签页中,输入:

${cursor}

Description标签页中,输入“DocBook section”。单击OK按钮,关闭Options窗口。

此后,在NetBeans编辑区域,输入sect,再按键盘上的Tab键,NetBeans将自动在光标所在的位置插入上面的代码,并将光标定位在title标签的中间。很方便。

根据个人习惯及使用频率,下面是一些可以为其设置代码模板的标签:

标签名称
para
section
table

默认代码模板

HTML

触发短语生成的代码
a <a href="${url}">${cursor}</a>
a:blank <a href="${url}" target="_blank"> ${cursor}
a:target <a href="${url}" target="${target}">${cursor}</a>
ad <address>${cursor}</address>
art <article class="${class}">${cursor}</article>
as <aside>${cursor}</aside>
b <b>${text}</b>${cursor}
bo <body> ${cursor} </body>
c <!-- ${comment} -->
d <div>${content}${cursor}</div>
datal <datalist id="${list}"> <option value="${first}">${text1}</option> <option value="${second}">${text2}</option> <option value="${third}">${text3}</option>${cursor} </datalist>
doc <!doctype html>
exp {{${cursor}}}
footer <footer>${cursor}</footer>
form <form action="${action}"> ${cursor} </form>
h1 <h1>${title}</h1>${cursor}
h2 <h2>${title}</h2>${cursor}
h3 <h3>${title}</h3>${cursor}
h4 <h4>${title}</h4>${cursor}
h5 <h5>${title}</h5>${cursor}
h6 <h6>${title}</h6>${cursor}
he <head> ${cursor} </head>
header <head>${cursor}</head>
hg <hgroup>${cursor}</hgroup>
html <html> <head> <title>${title}</title> </head> <body> ${cursor} </body> </html>
i <i>${text}</i>${cursor}
img <img src="${src}" alt="${alt}"${cursor}/>
img:wh <img src="${src}" width="${width}" height="${height}" alt="${alt}" ${cursor}/>
in <input type="${type}" name="${name}"${cursor}>
in:button <input type="button" value="${Button}"${cursor}>
in:check <input type="checkbox" id="${id}" name="${name}" value="${value}"${cursor}>
in:checkl <label>${text} <input type="checkbox" id="${id}" name="${name}" value="${value}"> </label>${cursor}
in:checklf <input type="checkbox" id="${id}" name="${name}" value="${value}"> <label for="${id}">${text}</label>${cursor}
in:color <input type="color"${cursor}>
in:colorv <input type="color" value="${#ffffff}"${cursor}>
in:date <input type="date"${cursor}>
in:datetl <input type="datetime-local"${cursor}>
in:email <input type="email" id="${id}"${cursor}>
in:file <input type="file" id="${id}" accept="${image/png,image/jpeg}" ${cursor}>
in:filem <input type="file" id="${id}" accept="${image/png,image/jpeg}" multiple="multiple" ${cursor}>
in:hidden <input type="hidden" id="${id}" name="${name}" value="${value}" ${cursor}>
in:image <input type="image" id="${id}" src="${src}" width="${width}" height="${height}" alt="${alt}" ${cursor}>
in:month <input type="month"${cursor}>
in:number <input type="number"${cursor}>
in:pass <input type="password" id="${id}" minlength="${8}"${cursor}>
in:radio <input type="radio" id="${id}" name="${name}" value="${value}"${cursor}>
in:radiol <label>${text} <input type="radio" id="${id}" name="${name}" value="${value}"> </label>${cursor}
in:radiolf <input type="radio" id="${id}" name="${name}" value="${value}"> <label for="${id}">${text}</label>${cursor}
in:range <input type="range" min="${0}" max="${50}"${cursor}>
in:reset <input type="reset" value="${Reset}"${cursor}>
in:search <input type="search" id="${id}"${cursor}>
in:submit <input type="submit" value="${Submit}"${cursor}>
in:tel <input type="tel" id="${id}"${cursor}>
in:telp <input type="tel" id="${id}" pattern="${pattern}"${cursor}>
in:text <input type="text" id="${id}"${cursor}>
in:time <input type="time" id="${id}"${cursor}>
in:url <input type="url" id="${id}" name="${name}"${cursor}>
in:week <input type="week" id="${id}"${cursor}>
label <label>${text}</label>${cursor}
labelf <label for="${id}">${text}</label>${cursor}
li
  • ${item}
  • ${cursor}
    link <link rel="stylesheet" href="${url}"/>
    main <main>${cursor}</main>
    me:char <meta charset="${UTF-8}">${cursor}
    me:content-type <meta http-equiv="content-type" content="text/html; charset=${UTF-8}">${cursor}
    me:http <meta http-equiv="${content-type}" content="${content}"${cursor}>
    me:key <meta name="keywords" content="${keywords}">
    me:name <meta name="${name}" content="${content}"${cursor}>
    me:viewport <meta name="viewport" content="width=device-width, initial-scale=1.0">${cursor}
    nav <nav>${cursor}</nav>
    ol <ol> <li>${first}</li> <li>${second}</li> <li>${third}</li> ${cursor} </ol>
    op <option value="${value}">${text}</option>${cursor}
    p <p>${text}${cursor}</p>
    scr <script src="${link}">${cursor}</script>
    sec <section>${cursor}</section>
    sel <select id="${id}"> <option value="${first}">${text1}</option> <option value="${second}">${text2}</option> <option value="${third}">${text3}</option>${cursor} </select>
    sp <span class="${class}">${text}</span>${cursor}
    st <strong>${text}</strong>${cursor}
    ta <table>${cursor}</table>
    tab <table> <tr> <th>${Header}</th> <th>${Header2}</th> </tr> <tr> <td>${Cell1}</td> <td>${Cell2}</td> </tr> <tr> <td>${Cell3}</td> <td>${Cell4}</td> </tr>${cursor} </table>
    td <td>${cell}</td>${cursor}
    texta <textarea id="${id}" name="${name}" rows="${5}" cols="${10}">${cursor}</textarea>
    th <th>${header}</th>${cursor}
    tit <title>${title}</title>
    tr <tr>${cursor}</tr>
    trtd <tr> <td>${cell}</td>${cursor} </tr>
    trth <tr> <th>${header}</th>${cursor} </tr>
    ul <ul> <li>${first}</li> <li>${second}</li> <li>${third}</li>${cursor} </ul>

    我个人用的较多的是:

    • a:blank
    • b
    • d
    • h1
    • i
    • img
    • li
    • link
    • ol
    • p
    • sec
    • sp
    • st
    • ul

    录制宏

    菜单NetBeansSettings...,弹出Options窗口。单击Editor标签页,单击Macros标签页,按New按钮,在弹出的New Macro Dialog窗口的Macro Name文本框中输入surround-with-<filename>,单击OK按钮。New Macro Dialog窗口关闭,并返回Options窗口。在该窗口Macros列表中,已自动选择刚刚输入的surround-with-<filename>项。在下面的Macro Code文本框中,输入:

    cut-to-clipboard "" paste-from-clipboard ""

    单击Set Shortcut...按钮,输入快捷键后,按OK按钮返回。再按OK按钮关闭Options窗口。

    在NetBeans编辑区域,使用鼠标刷选一个文件名,如C:\Hello.c,在键盘上按⌥Option+⌘Command+F键,NetBeans将自动将C:\Hello.c转换为<filename>C:\Hello.c</filename>

    还可以通过图标栏上的宏录制按钮来进行录入。

    下面是一些常用的宏:

    宏名称快捷键
    surround-with-<em>^Control+⌘Command+E
    surround-with-<filename>⌥Option+⌘Command+F
    surround-with-<func-name>^Control + ⌥Option + ⌘Command + F
    surround-with-<q>^Control + ⌥Option + Q
    surround-with-<var-name>⌥Option+⌘Command+v

    添加DocBook 5 的DTD

    菜单Toos -> DTDs and XML Schemas,在DTDs and XML Schemas窗口中,选中User Catalog[read-write],单击Add Local DTD or Schema..按钮,单击Browse按钮,找到并打开docbook.dtd文件,Public ID:-//OASIS//DTD DocBook V5.0//EN,确定。

    然后,在XML文档中添加下面的代码:

    <!DOCTYPE chapter PUBLIC '-//OASIS//DTD DocBook V5.0//EN' 'null'>

    这样,就能在NetBeans中实现代码辅助完成功能了。

    为保证在NetBeans中能正确转换,应确保catalog.xml文件有以下内容:

    <public publicId="-//OASIS//DTD DocBook V5.0//EN" uri="dtd/docbook.dtd" /> <uri name="docbook.xsl" uri="docbook-xsl-ns-1.79.1/html/docbook.xsl" /> <uri name="chunk.xsl" uri="docbook-xsl-ns-1.79.1/html/chunk.xsl" /> <uri name="fop.xsl" uri="docbook-xsl-ns-1.79.1/fo/docbook.xsl" /> <rewriteURI uriStartString="http://docbook.sourceforge.net/release/xsl-ns/current/" rewritePrefix="docbook-xsl-ns-1.79.1/" />

    第一行意思是,当xsltproc遇到XML文档中有

    <!DOCTYPE chapter PUBLIC '-//OASIS//DTD DocBook V5.0//EN' 'null'>

    的内容时,它就会根据catalog.xml文件所指定的uri,找到对应的docbook.dtd,这样就不会出现无法识别null的错误。

    NetBeans常用快捷键

    快捷键含义是否常用
    ⌘ C复制
    ⌘ E删除当前行或所选
    ⌘ V粘贴
    ⌘ [将光标移到匹配的标签
    ⇧ ⌘ [选中标签内的所有文本
    ⇧ ⌃ F对所选进行自动排版
    ⇧ ⌃ H切换高亮显示搜索结果
    ⇧ ⌃ L复制当前文件的路径
    ⇧ ⌃ ↑将当前行或所选上移一行
    ⇧ ⌃ ↓将当前行或所选下移一行
    ⇧ ⌃ ←将当前行或所选左移
    ⇧ ⌃ →将当前行或所选右移
    ⇧ ⌥ ↑将当前行复制到上一行
    ⇧ ⌥ ↓将当前行复制到下一行
    ⇧ ⌘ -收起折叠
    ⇧ ⌘ +展开折叠
    ⇧ ⌘ 1在项目窗格中选择当前文件
    ⇧ ⌘ 2在文件窗格中选择当前文件
    ⇧ ⌘ D显示剪贴板历史内容(最多9行)
    ⇧ ⌘ M切换设置标签
    ⇧ ⌘ V粘贴并自动排版(断行、缩进)
    ⇧ 回车在当前行之下添加新行并定位到新行
    ⌃ G定位到行数
    ⌃ Q定位到最后编辑的位置
    [⌃⌥] 鼠标滚轮放大或缩小显示比例
    ⌘ U, U将选择转换为大写
    ⌘ U, L将选择转换为小写
    ⌘ U, S将选择切换大小写

    在特定范围内查找与替换

    + H键,可在全局范围内查找与替换。但有时候,如果我们只希望在一个局部范围内进行查找与替换,但不要影响到所选之外的范围。此时,可这样操作:

    1、先选择要操作的范围。(注意,所选范围必须大于1行,否则会处于全局范围。)

    2、按 + H键。NetBeans会高亮显示所选中部分。

    3、在高亮显示的范围内放心地进行查找、替换。不会影响到文档其他部分。

    4、操作后,关闭查找替换窗口。返回全局操作。

    一个较好的特点是,NetBeans能够记住最近的10项查找内容与10项替换内容。

    参考资源

    1. NetBeans.org