gpt4 book ai didi

javafx - 如何设置警报框相对于当前primaryStage的位置? (JavaFX)

转载 作者:行者123 更新时间:2023-12-02 10:00:01 30 4
gpt4 key购买 nike

编辑:如果用户单击“删除”以删除 ListView 中的项目,我会弹出一个警告框。它有效,但我希望它能超越原来的舞台。它出现在我的第一台显示器上。有什么方法可以设置警报显示时的位置吗?

请注意,“所有者”位于不同的类中,我使用 Scenebuilder/FXML 创建了所有内容。我不知道如何让 initOwner() 工作。这是“主”类:

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Assignment_5 extends Application {
public Stage primaryStage;

@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("Assignment_5.fxml"));
primaryStage.setTitle("Plant Pack");
primaryStage.setScene(new Scene(root, 1200, 500));
primaryStage.show();
}

public static void main(String[] args) {
launch(args);
}
}

这是 Controller 类中的工作代码。没有必要实现此警报的模式,但它是一个很好的补充,可以使其更加方便。我根本不知道如何将主窗口从 Main 类传递到此:

    protected void handleDeleteButtonClick(ActionEvent event) {
Alert alertBox = new Alert(Alert.AlertType.CONFIRMATION, "Confirm Delete", ButtonType.OK, ButtonType.CANCEL);
alertBox.setContentText("Are you sure you want to delete this " + plantType.getValue().toString().toLowerCase() + "?");
alertBox.showAndWait();
if(alertBox.getResult() == ButtonType.OK) {
int selectedPlant = plantList.getSelectionModel().getSelectedIndex();
observablePlantList.remove(selectedPlant);
}
else {
alertBox.close();
}
}

我知道这是相当新的,因此很难找到很多资源。如果有人知道我可能错过的任何信息,请告诉我。感谢您提供的任何帮助。我正在使用 Java 8 和 IntelliJ 14.1.5。

最佳答案

正如 @jewelsea 所建议的,设置警报框的模式和所有者将确保警报出现在舞台上,即使舞台发生移动也是如此。

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.control.ListView;
import javafx.scene.layout.BorderPane;
import javafx.stage.Modality;
import javafx.stage.Stage;

public class DeleteAlertDemo extends Application {

Stage owner;
ObservableList<String> observablePlantList;
ListView<String> plantList;

protected void handleDeleteButtonClick(ActionEvent event) {
String item = plantList.getSelectionModel().getSelectedItem();
Alert alertBox = new Alert(Alert.AlertType.CONFIRMATION, "Confirm Delete",
ButtonType.OK, ButtonType.CANCEL);
alertBox.setContentText("Are you sure you want to delete this "
+ item.toLowerCase() + "?");
alertBox.initModality(Modality.APPLICATION_MODAL); /* *** */
alertBox.initOwner(owner); /* *** */
alertBox.showAndWait();
if (alertBox.getResult() == ButtonType.OK) {
int selectedPlant = plantList.getSelectionModel().getSelectedIndex();
observablePlantList.remove(selectedPlant);
} else {
alertBox.close();
}
}

@Override
public void start(Stage primaryStage) {
owner = primaryStage; /* *** */
Button deleteBtn = new Button();
deleteBtn.setText("Delete");
deleteBtn.setOnAction(this::handleDeleteButtonClick);

observablePlantList = FXCollections.observableArrayList("Begonia",
"Peony", "Rose", "Lilly", "Chrysanthemum", "Hosta");
plantList = new ListView<>(observablePlantList);
plantList.getSelectionModel().select(0);
BorderPane root = new BorderPane();
root.setCenter(plantList);
root.setRight(deleteBtn);

Scene scene = new Scene(root, 300, 250);

primaryStage.setTitle("Delete Alert Demo");
primaryStage.setScene(scene);
primaryStage.show();
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}

}

关于javafx - 如何设置警报框相对于当前primaryStage的位置? (JavaFX),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33838293/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com