我想要一个模态窗口,所以我在程序中添加了这些代码行:
primaryStage.initOwner(primaryStage);
primaryStage.initModality(Modality.WINDOW_MODAL);
但如果我用这些代码行开始我的程序,我会得到一个例外:
Exception in Application start method Exception in thread "main"
java.lang.NoSuchMethodException: P8.Hauptfenster.main([Ljava.lang.String;)
at java.lang.Class.getMethod(Class.java:1786)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:126)
这个例外的原因是什么?
主窗口:
public class Hauptfenster extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
BorderPane bp = new BorderPane();
FlowPane fp = new FlowPane();
Button bild = new Button("Bilderfassung");
Button audio = new Button("Audioerfassung");
Bildererfassung bildererfassung = new Bildererfassung();
bild.setOnAction(event -> bildererfassung.showBilderfassungDialog());
HBox hb = new HBox();
hb.setAlignment(Pos.CENTER);
hb.getChildren().addAll(bild, audio);
hb.setSpacing(10.0);
bp.setCenter(hb);
Scene scene = new Scene(bp, 400, 400);
primaryStage.initOwner(primaryStage);
primaryStage.initModality(Modality.WINDOW_MODAL);
primaryStage.setTitle("Medienverwaltung");
//primaryStage.initOwner(primaryStage);
//primaryStage.initModality(Modality.WINDOW_MODAL);
primaryStage.setScene(scene);
primaryStage.show();
}
}
2。窗口:
public class Bildererfassung {
public void showBilderfassungDialog() {
Stage stage = new Stage();
GridPane gp = new GridPane();
gp.setAlignment(Pos.CENTER);
gp.setPadding(new Insets(5));
gp.setHgap(5);
gp.setVgap(5);
Label lblTitel = new Label("Titel:");
gp.setHalignment(lblTitel, HPos.RIGHT);
Label lblOrt = new Label("Ort:");
gp.setHalignment(lblOrt, HPos.RIGHT);
Label lblAufnahmejahr = new Label("Aufnahmejahr:");
gp.setHalignment(lblAufnahmejahr, HPos.RIGHT);
TextField tfTitel = new TextField();
TextField tfOrt = new TextField();
TextField tfAufnahmejahr = new TextField();
Button btnNeu = new Button("Neu");
Button btnAbbrechen = new Button("Abbrechen");
HBox hb = new HBox();
hb.getChildren().addAll(btnNeu, btnAbbrechen);
hb.setPadding(new Insets(10.0, 0.0, 0.0, 0.0));
hb.setAlignment(Pos.CENTER);
hb.setSpacing(5.0);
gp.add(lblTitel, 0, 0);
gp.add(lblOrt, 0, 1);
gp.add(lblAufnahmejahr, 0, 2);
gp.add(tfTitel, 1, 0);
gp.add(tfOrt, 1, 1);
gp.add(tfAufnahmejahr, 1, 2);
gp.add(hb, 1, 3);
BorderPane bp = new BorderPane();
bp.setCenter(gp);
Scene scene = new Scene(bp);
stage.setScene(scene);
stage.setTitle("Bilderfassung");
stage.show();
}
}