如何在 main 中使用我自己的 class?
How to use my own class in main?
我收到了一份让我发疯的学校作业。我可以在网上搜索时找到我正在寻找的答案,所以我真的很想得到一些关于这个的建议。
任务是创建一个基本的 JavaFX 应用程序,上面写着 "Hello World" 但我应该包含扩展 BorderPane 的 helloMain.java 和 class HelloWorld.java。我已经使用 helloMain 解决了分配问题,但是当我尝试将一些代码(根节点及其包含的所有内容)移动到 HelloWorld.java 时,当我 运行 时,场景似乎没有加载应用程序(只是一个空 window)。所以很明显我做错了什么,但我不知道是什么。
在 helloJava 中我得到了以下代码(我很确定这是正确的)
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class helloWorldMain extends Application {
@Override
public void start(Stage primaryStage) {
HelloWorld helloWorld = new HelloWorld();
Scene scene = new Scene(helloWorld, 300, 300);
primaryStage.setTitle("Hello World");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
在我的 HelloWorld.java 中我得到了这个代码
public class HelloWorld extends BorderPane {
public HeloWorld() {
final Text text = new Text(0, 130, "Hello World");
Pane txtPane = new Pane();
Pane txtPane2 = new Pane();
VBox root = new VBox();
txtPane.getChildren().add(text);
txtPane2.getChildren().add(text);
root.getChildren().addAll(txtPane, txtPane2);
}
}
我认为可以说我真的不知道自己在做什么,但我正在努力学习。
- 我在 HelloWorld class 中缺少什么?它是构造函数中的东西吗?还是我应该使用某种领域?还是其他原因?
- 我已经搜索了几个小时试图解决这个问题,但我没有找到任何与我正在尝试做的事情相关的搜索结果。我在哪里可以阅读 "this approach"(我真的不知道当您创建自己的 class 并尝试在 main?
中使用它时,它叫什么,可能还有为什么我找不到任何东西)
很多代码都喜欢可以帮助我理解这一点的人:)
在 HelloWorld
中,您将 Node
添加到 VBox
中,但您没有在任何地方添加,因此它们是 "hanging in the air"。
将这一行添加到 HelloWorld 构造函数的末尾
...
getChildren().add(root);
...大功告成。
我收到了一份让我发疯的学校作业。我可以在网上搜索时找到我正在寻找的答案,所以我真的很想得到一些关于这个的建议。
任务是创建一个基本的 JavaFX 应用程序,上面写着 "Hello World" 但我应该包含扩展 BorderPane 的 helloMain.java 和 class HelloWorld.java。我已经使用 helloMain 解决了分配问题,但是当我尝试将一些代码(根节点及其包含的所有内容)移动到 HelloWorld.java 时,当我 运行 时,场景似乎没有加载应用程序(只是一个空 window)。所以很明显我做错了什么,但我不知道是什么。
在 helloJava 中我得到了以下代码(我很确定这是正确的)
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class helloWorldMain extends Application {
@Override
public void start(Stage primaryStage) {
HelloWorld helloWorld = new HelloWorld();
Scene scene = new Scene(helloWorld, 300, 300);
primaryStage.setTitle("Hello World");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
在我的 HelloWorld.java 中我得到了这个代码
public class HelloWorld extends BorderPane {
public HeloWorld() {
final Text text = new Text(0, 130, "Hello World");
Pane txtPane = new Pane();
Pane txtPane2 = new Pane();
VBox root = new VBox();
txtPane.getChildren().add(text);
txtPane2.getChildren().add(text);
root.getChildren().addAll(txtPane, txtPane2);
}
}
我认为可以说我真的不知道自己在做什么,但我正在努力学习。
- 我在 HelloWorld class 中缺少什么?它是构造函数中的东西吗?还是我应该使用某种领域?还是其他原因?
- 我已经搜索了几个小时试图解决这个问题,但我没有找到任何与我正在尝试做的事情相关的搜索结果。我在哪里可以阅读 "this approach"(我真的不知道当您创建自己的 class 并尝试在 main? 中使用它时,它叫什么,可能还有为什么我找不到任何东西)
很多代码都喜欢可以帮助我理解这一点的人:)
在 HelloWorld
中,您将 Node
添加到 VBox
中,但您没有在任何地方添加,因此它们是 "hanging in the air"。
将这一行添加到 HelloWorld 构造函数的末尾 ...
getChildren().add(root);
...大功告成。