当我使用Swing时,这是我的代码片段:
private static Gui gui;
public static void main(String[] args){
gui = new Gui(); //(1)
gui.menu(); //(2)
gui.show(); //(3)
}
JFrame
JLabel
和JPanel
并将它们添加到框架中。true
。我一直在尝试,但我无法使用Java-FX获得我想要的结果。这就是我上次尝试的方式:
private static Gui gui;
public static void main(String[] args){
gui = new Gui();
Application.launch(Gui.class, args);
gui.menu();
gui.show();
}
桂班:
private static Stage stage;
public class Gui extends Application {
publc void start(Stage primaryStage){
Gui.stage = primaryStage;
stage.setTitle("title");
//I read that some method blocking is involved here, I want to go back!
}
public void menu(){
BorderPane abc = new BorderPane();
Scene scene = new Scene(abc, 200, 200);
stage.setScene(scene);
//if I somehow mange to reach this part, there will be some null pointer
//exception, well everything looks real initialised to me
}
public void show(){
stage.show();
//I have never reach this part
}
}
答案 0 :(得分:1)
最简单的方法是使用Application.launch,如下所示:
package gui;
import javafx.application.Application;
public class NewClass {
public static void main(String[] args){
Application.launch(Gui.class, args);
}
}
让你的班级像这样:
package gui;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class Gui extends Application {
private Scene scene;
private Stage stage;
@Override
public void start(Stage stage) {
this.stage=stage;
Button btn = new Button("test");
this.scene = new Scene(btn, 200, 250);
stage.setTitle("title");
stage.setScene(scene);
stage.show();
}
public void menu(){
BorderPane abc = new BorderPane();
this.scene = new Scene(abc, 200, 200);
this.stage.setScene(scene);
//if I somehow mange to reach this part, there will be some null pointer
//exception, well everything looks real initialised to me
}
}
您想要实现的目标有更多样本here。