Java 如何在 Netbeans 中插入 main 方法(快捷方式)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18636463/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 09:43:31  来源:igfitidea点击:

How to insert main method in Netbeans (shortcut)

javanetbeans

提问by Lukasz Czerwinski

Sometimes you would like to run a single file to test some code quickly. Typing in public static void main(String[] args) {each time is tedious. How to do it quicker?

有时您希望运行单个文件来快速测试某些代码。public static void main(String[] args) {每次打字都很乏味。怎么做比较快?

回答by Lukasz Czerwinski

Thanks to predefined code templates in Netbeans it's simple: just type psvmand press Tab.

多亏了 Netbeans 中预定义的代码模板,这很简单:只需键入psvm并按 Tab。

psvmis an acronym for: publicstaticvoidmain

psvm是首字母缩写词:p ublics taticv oidmain

回答by Tobias Kremer

If you want to just run some test why not use your testing framework? like JUnit:

如果您只想运行一些测试,为什么不使用您的测试框架?像 JUnit:

@Test
public void test() {
    // do something
}

This way you can even store the test for later usage. It is properbly in most cases not a good idear to think of tests as something to execute once and then throw away.

这样您甚至可以存储测试以备后用。在大多数情况下,将测试视为执行一次然后扔掉的东西并不是一个好主意。

回答by exe2bin

"psvm" is not the most intuitive abbreviation I can think of when I want to quickly insert a main method, so I created a new one more to my liking in the Code Templates library.

当我想快速插入一个 main 方法时,“psvm”并不是我能想到的最直观的缩写,所以我在代码模板库中又根据自己的喜好创建了一个新的缩写。

"main" seemed to be more natural for me, and since there's nothing else like it in the list of templates, I used it as an abbreviation and copied the same code from "psvm" in.

“main”对我来说似乎更自然,因为模板列表中没有其他类似的东西,我用它作为缩写,并从“psvm”中复制了相同的代码。

Now when I type "main" + TAB (without the quotes of course) I get my main method.

现在,当我输入“main”+TAB(当然没有引号)时,我得到了我的主要方法。

It is redundant, but more intuitive for me.

它是多余的,但对我来说更直观。

To create "main" go to Tools->Options, click the "Editor" Icon, then the "Code Templates" tab.

要创建“main”,请转到“工具”->“选项”,单击“编辑器”图标,然后单击“代码模板”选项卡。

  • Make sure that the "Language" combo is set to "Java"
  • Click the "New" button that's to the right of the "Templates" window
  • Enter "main" (without quotes) in the "Abbreviation" textbox that pops up
  • Enter the template code in the "Expanded Text" window below
  • 确保“语言”组合设置为“Java”
  • 单击“模板”窗口右侧的“新建”按钮
  • 在弹出的“缩写”文本框中输入“main”(不带引号)
  • 在下面的“扩展文本”窗口中输入模板代码

my entry looks like this:

我的条目是这样的:

Abbreviation

缩写

main           

Expanded Text

扩展文本

public static void main(String[] args) {$cursor}

Expanded Text (Code Window)

扩展文本(代码窗口)

public static void main(String[] args) {
    ${cursor}
}

Of course, you can always have Netbeans create your application's main class with the main method inserted by default.

当然,您始终可以使用默认插入的 main 方法让 Netbeans 创建应用程序的主类。

You do that by Choosing "Java Main Class" from the "New File" dialog instead of "Java Class". That will do it.

您可以通过从“新文件”对话框中选择“Java 主类”而不是“Java 类”来实现这一点。这样就可以了。

Cheers!

干杯!