Java 包声明:Netbeans 说:“包不正确”

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

Java package declaration: Netbeans says: 'Incorrect Package'

javanetbeans

提问by Audrey Smith

I am new to Java, Netbeans, and the IB Java API.

我是 Java、Netbeans 和 IB Java API 的新手。

I downloaded the IB Java API software and I am using Netbeans to look at it.

我下载了 IB Java API 软件,我正在使用 Netbeans 来查看它。

On one of the files, Netbeans is indicating a problem with the file.

在其中一个文件上,Netbeans 表明该文件存在问题。

At the very top of the file, the author has placed a package declaration:

在文件的最顶部,作者放置了一个包声明:

package samples.rfq;

包装样品.rfq;

Netbeans is using a red-dot to the left of the package declaration to tell me that it has a problem with the package declaration.

Netbeans 使用包声明左侧的红点来告诉我包声明有问题。

When I mouse-hover the package declaration, Netbeans tells me this:

当我将鼠标悬停在包声明上时,Netbeans 告诉我:

Incorrect Package (Alt-Enter shows hints)

不正确的包(Alt-Enter 显示提示)

On my Mac-keyboard I press Alt-Enter and Netbeans just interprets that as an Enter (and then I need to undo that Enter).

在我的 Mac 键盘上,我按 Alt-Enter,Netbeans 只是将其解释为 Enter(然后我需要撤消该 Enter)。

I have 2 questions:

我有两个问题:

  1. How do I work around the Alt-Enter-bug to see the hints?

  2. What do you typically do when Netbeans indicates 'Incorrect Package' on one of your package declarations?

  1. 如何解决 Alt-Enter-bug 以查看提示?

  2. 当 Netbeans 在您的一个包声明中指出“不正确的包”时,您通常会怎么做?

回答by josefx

In java packages have to follow the directory structure. So if you declare your package as package samples.rfq;then your class has to be in the directory samples/rfq/.

在java包中必须遵循目录结构。因此,如果您将包声明为 aspackage samples.rfq;那么您的类必须位于目录 samples/rfq/ 中。

So to fix this error you either change the package declaration or move your class into the directory given as package.

因此,要修复此错误,您可以更改包声明或将类移动到作为 package.json 给出的目录中。

回答by Mahmoud Hanafy

package declaration should be the first statement in a java class,unless the class is in the default package

包声明应该是java类中的第一条语句,除非该类在默认包中

when netbeans says "Incorrect Package",I add this line before the beginning of the class

当 netbeans 说“不正确的包”时,我在课程开始之前添加了这一行

package Test;

assuming the class is inside the folder "Test"

假设该类位于文件夹“Test”中