我正在尝试创建一个类似用户注册面板的警报框类型窗口。现在,当我使用右上角按钮关闭窗口时(我没有实现取消/关闭按钮)并重新打开它会弹出这个错误:
Caused by: java.lang.IllegalStateException: Cannot set modality once stage has been set visible
at javafx.stage.Stage.initModality(Stage.java:525)
at elibraryserver.NewStudentController.initRegister(NewStudentController.java:150)
at elibraryserver.serverController.addStudentButtonAction(serverController.java:71)
... 53 more
我不明白,因为我关闭窗口不应该重置并打开另一个窗口?我从错误中看到的是它认为窗口仍然是打开的。或者我错了吗?
以下是舞台的代码:
public void initRegister() throws IOException {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("newStudent.fxml"));
Parent root = loader.load();
Rectangle2D screenBounds = Screen.getPrimary().getVisualBounds();
registerStage.setX((screenBounds.getWidth() - registerStage.getWidth()) / 2);
registerStage.setY((screenBounds.getHeight() - registerStage.getHeight()) / 2);
Scene scene = new Scene(root);
registerStage.setScene(scene);
registerStage.setTitle("Register Student");
registerStage.initModality(Modality.APPLICATION_MODAL);
registerStage.showAndWait();
}
public void addStudentButtonAction(ActionEvent event) throws IOException {
NewStudentController nsc = new NewStudentController();
nsc.initRegister();
}
答案 0 :(得分:0)
不确定您的问题究竟是什么我认为是因为您在初始化新窗口时没有创建新阶段
以下是一些示例代码
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.stage.Modality;
import javafx.stage.Stage;
import java.io.IOException;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
Button button = new Button("Open Test window");
VBox vbox = new VBox();
vbox.getChildren().addAll(button);
Scene scene = new Scene(vbox);
Stage stage = new Stage();
stage.setScene(scene);
stage.show();
button.setOnAction(event -> {
try {
openAction();
} catch (IOException e) {
e.printStackTrace();
}
});
}
private void openAction() throws IOException {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("Test.fxml"));
Parent root = loader.load();
Stage stage = new Stage();
Scene scene = new Scene(root);
stage.setScene(scene);
stage.setTitle("Register Student");
stage.initModality(Modality.APPLICATION_MODAL);
stage.show();
}
public static void main(String[] args) { launch(args); }
}
testFXML控制器
import javafx.fxml.Initializable;
import javafx.geometry.Insets;
import javafx.scene.control.ColorPicker;
import javafx.scene.layout.*;
import javafx.scene.paint.Paint;
import java.net.URL;
import java.util.ResourceBundle;
public class TestFXMLController implements Initializable{
public ColorPicker colorPicker;
public VBox vBox;
@Override
public void initialize(URL location, ResourceBundle resources) {
colorPicker.setOnAction(event -> {
Paint fill = colorPicker.getValue();
BackgroundFill backgroundFill =
new BackgroundFill(fill,
CornerRadii.EMPTY,
Insets.EMPTY);
Background background = new Background(backgroundFill);
vBox.setBackground(background);
});
}
}
Test.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.ColorPicker?>
<?import javafx.scene.layout.VBox?>
<VBox fx:id="vBox" alignment="CENTER" prefHeight="300.0" prefWidth="300.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="TestFXMLController">
<children>
<ColorPicker fx:id="colorPicker" opacity="0.5" VBox.vgrow="ALWAYS" />
</children>
</VBox>