如何从javafx中的GUI打开另一个GUI



我正在尝试制作一个带有GUI的库存系统,该GUI使用javaFX ive为电视编码了一个GUI,现在是另一个用于让所有者决定投入多少的GUI,在使用按下添加按钮后,我想调用TVGUI让客户指定更多内容,但使用此代码会给我一个错误"JavaFX应用程序线程";java.lang.IollegalStateException:不能多次调用应用程序启动还有别的办法我能做到吗?感谢的帮助

import javafx.event.ActionEvent;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.GridPane;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class addstock extends Application{
public static void main(String[] args) {
launch(args);
}
public void start(Stage PrimaryStage)throws NumberFormatException {
BorderPane Bpane = new BorderPane();

FlowPane fpane = new FlowPane();
fpane.setPadding(new Insets(10));
Text title = new Text("ADD STOCK");
title.setFont(Font.font("Courier", FontWeight.EXTRA_BOLD,FontPosture.REGULAR, 22) );
Text Ttotal = new Text("  How many products to add?  ");
Ttotal.setFont(new Font("Times New Roman", 16));    
TextField enter = new TextField();
enter.setPrefColumnCount(3);
fpane.getChildren().addAll(title,Ttotal,enter); 

fpane.setAlignment(Pos.CENTER);
Bpane.setTop(fpane);
Button btSave = new Button();
btSave.setText("Add");

btSave.setOnAction(e-> 
Application.launch(TVGUI.class));

fpane.getChildren().addAll(btSave); 

Scene scene = new Scene(Bpane,460,400);
PrimaryStage.setTitle("Stock Deduct");
PrimaryStage.setResizable(false);
PrimaryStage.setScene(scene);
PrimaryStage.show();


}
}```

您可以创建一个新的Stage并显示它。您可以将所有内容添加到此Stage,就像将任何内容添加到主Stage一样。

要显示第二个窗口,您只需要创建一个新阶段并显示它:

btSave.setOnAction(e-> {
Stage stg = new Stage();
stg.show();
});

我不认为你可以同时在两个阶段上运行两个单独的循环,例如,你需要多线程来实现这一点,但我不确定,不要把我的话视为理所当然。

最新更新