我在我的代码中使弹出菜单工作有问题,我设置它的方式是,如果点击一个按钮,它将打开一个新场景(如果他们想删除某些东西会提示)
然而,新场景是另一个带有它自己的控制器的FXML文件,当我试图让新的FXML控制器删除某些东西时,它不会起作用,因为代码不在同一个控制器,所以我不能从FIRST控制器执行代码。
现在我只想在同一个班级中打开一个对话框,而且我不知道如何将代码转换为同一个控制器。这是我想要保存在同一个控制器中的FXML代码
<AnchorPane id="AnchorPane" prefHeight="89.0" prefWidth="388.0"
xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8"
fx:controller="finalprojectjava.DeletePopupController">
<children>
<Label layoutX="48.0" layoutY="14.0" prefHeight="32.0" prefWidth="293.0" text="Are you sure you want to delete contact?">
<font>
<Font size="16.0" />
</font>
</Label>
<Button layoutX="92.0" layoutY="50.0" mnemonicParsing="false" onAction="#acceptButton" prefHeight="25.0" prefWidth="91.0" text="Yes" />
<Button layoutX="195.0" layoutY="50.0" mnemonicParsing="false" onAction="#declineButton" prefHeight="25.0" prefWidth="91.0" text="No" />
</children>
</AnchorPane>
答案 0 :(得分:0)
以下是使用JavaFX Dialogs通过弹出窗口从用户获取响应的示例。您可以详细了解此API here的强大功能。
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
VBox pane = new VBox(10);
pane.setPadding(new Insets(10));
Button btnShowDialog = new Button("Show Popup");
// Set the action to call the showPopup() method when clicked
btnShowDialog.setOnAction(e -> showPopup());
pane.getChildren().add(btnShowDialog);
Scene scene = new Scene(pane);
primaryStage.setScene(scene);
primaryStage.show();
}
private void showPopup() {
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
alert.setTitle("Delete Contact?");
alert.setHeaderText("Are you sure you want to delete this contact?");
// Set the available buttons for the alert
ButtonType btnYes = new ButtonType("Yes");
ButtonType btnNo = new ButtonType("No");
alert.getButtonTypes().setAll(btnYes, btnNo);
// This allows you to get the response back from the user
Optional<ButtonType> result = alert.showAndWait();
if (result.isPresent()) {
if (result.get() == btnYes) {
System.out.println("User clicked Yes!");
} else if (result.get() == btnNo) {
System.out.println("User clicked No!");
}
}
}
}