JavaFX - 如何知道是否按下了取消
JavaFX - how to know if cancel was pressed
我如何知道在此 JavaFX 对话框中是否按下了“确定”按钮或“取消”按钮。
对话代码:
public String delimiter;
public void delimiterYES() throws IOException {
delimiter=new String();
TextInputDialog dialog = new TextInputDialog();
dialog.setTitle("Delimiter");
dialog.setHeaderText("Enter the delimiter");
Optional<String> result = dialog.showAndWait();
if (result.isPresent()) {
delimiter=result.get();
}
}
好的,我在这里找到了答案JavaFX Dialogs
如果用户取消对话框,result.isPresent() 将 return 为 false。
如果存在结果,则用户按下确定。如果没有结果,则用户可能按下了取消,但他们可能刚刚使用 OS 关闭 window 函数关闭了对话框 window。
Optional<String> result = new TextInputDialog().showAndWait();
if (result.isPresent()) {
// ok was pressed.
} else {
// cancel might have been pressed.
}
要真正了解某个按钮是否被按下,您可以使用 Dialog javadoc 部分 "Dialog Validation / Intercepting Button Actions" 中所述的过滤器。
final Button cancel = (Button) dialog.getDialogPane().lookupButton(ButtonType.CANCEL);
cancel.addEventFilter(ActionEvent.ACTION, event ->
System.out.println("Cancel was definitely pressed")
);
示例代码:
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.stage.Stage;
import java.util.Optional;
public class DialogSample extends Application {
@Override
public void start(Stage stage) throws Exception {
Button showButton = new Button("show");
showButton.setOnAction(event -> showDialog(stage));
showButton.setPrefWidth(100);
stage.setScene(new Scene(showButton));
stage.show();
showButton.fire();
}
private void showDialog(Stage stage) {
TextInputDialog dialog = new TextInputDialog();
dialog.initOwner(stage);
dialog.setTitle("Delimiter");
dialog.setHeaderText("Enter the delimiter");
final Button ok = (Button) dialog.getDialogPane().lookupButton(ButtonType.OK);
ok.addEventFilter(ActionEvent.ACTION, event ->
System.out.println("OK was definitely pressed")
);
final Button cancel = (Button) dialog.getDialogPane().lookupButton(ButtonType.CANCEL);
cancel.addEventFilter(ActionEvent.ACTION, event ->
System.out.println("Cancel was definitely pressed")
);
Optional<String> result = dialog.showAndWait();
if (result.isPresent()) {
System.out.println("Result present => OK was pressed");
System.out.println("Result: " + result.get());
} else {
System.out.println("Result not present => Cancel might have been pressed");
}
}
public static void main(String[] args) {
Application.launch();
}
}
您可以使用 Optional<ButtonType>
而不是 Optional<String>
。基本上使用下面的代码。
Optional<ButtonType> result = dialog.showAndWait();
if (result.isPresent() && result.get() == ButtonType.OK){
System.out.println("Ok button is pressed");
} else if(result.isPresent() && result.get() == ButtonType.CANCEL){
System.out.println("Cancel button was pressed");
}
希望对您有所帮助。如果您需要进一步说明,请告诉我。
我如何知道在此 JavaFX 对话框中是否按下了“确定”按钮或“取消”按钮。
对话代码:
public String delimiter;
public void delimiterYES() throws IOException {
delimiter=new String();
TextInputDialog dialog = new TextInputDialog();
dialog.setTitle("Delimiter");
dialog.setHeaderText("Enter the delimiter");
Optional<String> result = dialog.showAndWait();
if (result.isPresent()) {
delimiter=result.get();
}
}
好的,我在这里找到了答案JavaFX Dialogs
如果用户取消对话框,result.isPresent() 将 return 为 false。
如果存在结果,则用户按下确定。如果没有结果,则用户可能按下了取消,但他们可能刚刚使用 OS 关闭 window 函数关闭了对话框 window。
Optional<String> result = new TextInputDialog().showAndWait();
if (result.isPresent()) {
// ok was pressed.
} else {
// cancel might have been pressed.
}
要真正了解某个按钮是否被按下,您可以使用 Dialog javadoc 部分 "Dialog Validation / Intercepting Button Actions" 中所述的过滤器。
final Button cancel = (Button) dialog.getDialogPane().lookupButton(ButtonType.CANCEL);
cancel.addEventFilter(ActionEvent.ACTION, event ->
System.out.println("Cancel was definitely pressed")
);
示例代码:
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.stage.Stage;
import java.util.Optional;
public class DialogSample extends Application {
@Override
public void start(Stage stage) throws Exception {
Button showButton = new Button("show");
showButton.setOnAction(event -> showDialog(stage));
showButton.setPrefWidth(100);
stage.setScene(new Scene(showButton));
stage.show();
showButton.fire();
}
private void showDialog(Stage stage) {
TextInputDialog dialog = new TextInputDialog();
dialog.initOwner(stage);
dialog.setTitle("Delimiter");
dialog.setHeaderText("Enter the delimiter");
final Button ok = (Button) dialog.getDialogPane().lookupButton(ButtonType.OK);
ok.addEventFilter(ActionEvent.ACTION, event ->
System.out.println("OK was definitely pressed")
);
final Button cancel = (Button) dialog.getDialogPane().lookupButton(ButtonType.CANCEL);
cancel.addEventFilter(ActionEvent.ACTION, event ->
System.out.println("Cancel was definitely pressed")
);
Optional<String> result = dialog.showAndWait();
if (result.isPresent()) {
System.out.println("Result present => OK was pressed");
System.out.println("Result: " + result.get());
} else {
System.out.println("Result not present => Cancel might have been pressed");
}
}
public static void main(String[] args) {
Application.launch();
}
}
您可以使用 Optional<ButtonType>
而不是 Optional<String>
。基本上使用下面的代码。
Optional<ButtonType> result = dialog.showAndWait();
if (result.isPresent() && result.get() == ButtonType.OK){
System.out.println("Ok button is pressed");
} else if(result.isPresent() && result.get() == ButtonType.CANCEL){
System.out.println("Cancel button was pressed");
}
希望对您有所帮助。如果您需要进一步说明,请告诉我。