很少有JavaFx窗口
如果javaFx窗口在单独的类中,如何运行它们 例如,如本例所示:很少有JavaFx窗口,java,javafx,Java,Javafx,如果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 e
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;
}
}
应用程序
不是窗口:窗口由阶段
表示。您应该只有一个应用程序类(以及由launch
为您创建的实例):您可以创建任意多个Stage
s。您必须创建两个Stage
,请查看答案中的示例代码。