如果几个 javaFx 窗口位于单独的类中,如何运行它们?
例如,在本例中:
public class Main {
public static void main(String[] args) {
Form1 form1 = new Form1();
Form2 form2 = new Form2();
// run form1
// run form2
}
public static class Form1 extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
Stage stage = new Stage();
stage.setScene(new Scene(new Group(new Button("Window 1"))));
stage.show();
}
}
public static class Form2 extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
Stage stage = new Stage();
stage.setScene(new Scene(new Group(new Button("Window 2"))));
stage.show();
}
}
}
我只需要同时显示两个窗口,但找不到任何简单的示例。
最佳答案
在javaFx中窗口被认为是阶段,因此不能创建多个窗口,可以考虑使用以下代码作为示例
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
form1().show();
form2().show();
}
public static void main(String[] args) {
launch(args);
}
private Stage form1(){
Stage stage=new Stage();
stage.setTitle("Window 1");
stage.setScene(new Scene(new Group(new Button("Window 1"))));
return stage;
}
private Stage form2(){
Stage stage=new Stage();
stage.setTitle("Window 2");
stage.setScene(new Scene(new Group(new Button("Window 2"))));
return stage;
}
}
关于java - JavaFx 窗口很少,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33808868/