简体   繁体   English

如何在public static main中使用public void方法

[英]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.

相关问题 如何使用 JUnit 5 测试 Java 中的 Public Static Void Main 方法? - How to test the Public Static Void Main method in Java using JUnit 5? Java拒绝public static void main方法,请求public static void main方法 - Java rejects public static void main method, requests public static void main method 对main方法的误解/“ public static void main(String [] args)”的观点 - Misunderstanding main method/the point of “public static void main(String[] args)” Java - public static void main() - Java - public static void main() Kotlin 中的 public static void main - public static void main in Kotlin Java-使用JUnit测试公共静态void主方法 - Java - test a public static void main method with JUnit 覆盖范围不能覆盖public static void main方法 - Coverage can't cover the public static void main method 如何在 Java 中调用公共 static void main(String[] args) class 中的方法? - How to call a method inside the public static void main(String[] args) class in Java? public static void main(String args[]) 为什么 string args[] 是强制性的,为什么我们不使用 public static void main(Object args[])? - public static void main(String args[]) why string args[] is compulsory ,why we not use public static void main(Object args[])? 在哪些情况下我们可以使用public static void main(String ... args)而不是public static void main(String [] args) - In which situations we can use public static void main(String… args) instead of public static void main(String[] args)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM