[英]How to use public void method in public static main
i need help, in a project i have a button set in public void method but i need to use it in my public static void main , here the problem, the method ask me to be static but in static i does not work at all and said my method is null.我需要帮助,在一个项目中,我在 public void 方法中设置了一个按钮,但我需要在 public static void main 中使用它,这里的问题是,该方法要求我是静态的,但在静态中我根本不起作用,并且说我的方法是空的。
public static void main(String[] args)
{
the app
}
@FXML
private AnchorPane Accueil;
@FXML
private AnchorPane Sport;
public void Entree()
{
Accueil.setVisible(false);
Sport.setVisible(true);
}
so basically i want to do this所以基本上我想这样做
RunComplexe.Entree();
went i do this i got an error : Cannot make a static reference to the non-static method Entree() from the type RunComplexe我这样做了,但出现错误:无法从 RunComplexe 类型对非静态方法 Entree() 进行静态引用
if u know how to fix it, tell me, thanks.如果你知道如何解决它,告诉我,谢谢。
Create a new class as follows:创建一个新类,如下所示:
public class OtherClass {
@FXML
private AnchorPane Accueil;
@FXML
private AnchorPane Sport;
public void entree() {
Accueil.setVisible(false);
Sport.setVisible(true);
}
}
Now in your main method you need to create an object of such class so that you can call the method:现在在你的 main 方法中,你需要创建一个这样的类的对象,以便你可以调用该方法:
public static void main(String[] args) {
OtherClass otherClass = new OtherClass();
otherClass.entree();
}
As a side note, in Java, the standard is to have method names following snakeCase
, so it should be entree()
instead.附带说明一下,在 Java 中,标准是在snakeCase
有方法名称,所以它应该是snakeCase
entree()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.