我有一个名为GUI的类来管理我的应用程序。当用户想要在我的程序中删除他的帐户时,我想要弹出警告框并要求他确认或取消他的操作,因此他不会意外删除他的帐户。
package application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.*;
/**
* An Alert Box for the GUI
* to display errors to the user.
*
*/
public class AlertBox
{
Stage window;
public boolean display(String title, String message)
{
boolean cancel = false;
window = new Stage(); // Create the stage.
window.initModality(Modality.APPLICATION_MODAL); // If window is up, make user handle it.
window.setTitle(title);
window.setMinHeight(350);
window.setMinWidth(250);
VBox root = new VBox(); // Root layout.
Label warning = new Label(message); // Message explaining what is wrong.
HBox buttonLayout = new HBox(); // The yes and cancel button.
Button yesButton = new Button("Yes"); // Yes button for the user.
Button noButton = new Button("No"); // No button for the user.
yesButton.setOnAction(e ->
{
cancel = false;
});
noButton.setOnAction(e ->
{
cancel = true;
});
buttonLayout.getChildren().addAll(yesButton, noButton);
root.getChildren().addAll(warning, buttonLayout);
root.setAlignment(Pos.CENTER);
Scene scene = new Scene(root);
window.setScene(scene);
window.show();
}
/**
* Closes the window and returns whether the user said yes or no.
* @param variable
* @return
*/
private boolean close(boolean variable)
{
window.close();
return variable;
}
}
我希望我的GUI类能够确切地知道用户在AlertBox类中发生了什么。用户是否单击是或否?所以我考虑将display方法设为boolean。问题所在,我的事件监听器表达式不能返回任何值,因为它位于void类型的回调中。然后我想,"哦,我只是让close方法返回一个布尔值"。但后来我记得原来我调用的函数是:
AlertBox warning = new AlertBox;
boolean userWantsToDelete = warning.display("Warning!", "You are about to delete your account. Are you sure you would like to do this?");
希望display方法返回变量,而不是close方法。我也不能打电话给关闭,因为那不会起作用。我该怎么做才能解决这个问题?非常感谢你。
答案 0 :(得分:5)
您可以使用JavaFX警报轻松执行任务:
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
alert.setTitle("Confirmation Dialog");
alert.setHeaderText("Warning !");
alert.setContentText("Are you sure you want to perform this action ?");
Optional<ButtonType> result = alert.showAndWait();
if (result.get() == ButtonType.OK) {
// delete user
}
result.get()返回一个布尔值。因此,您可以通过在if条件中进行比较来检查用户是否点击了ButtonType.OK
或ButtonType.CANCEL
。
使用默认警报,您将获得两个按钮,一个是OK按钮,另一个是取消。但是如果您想自定义警报,您可以将自己的按钮类型添加到其中,如下所示:
ButtonType buttonTypeOne = new ButtonType("One");
ButtonType buttonTypeTwo = new ButtonType("Two");
ButtonType buttonTypeThree = new ButtonType("Three");
并将它们添加到警报中:
alert.getButtonTypes().setAll(buttonTypeOne, buttonTypeTwo, buttonTypeThree);
因此,您可以查看用户按下了哪个按钮
Optional<ButtonType> result = alert.showAndWait();
if (result.get() == buttonTypeOne){
// ... user chose "One"
} else if (result.get() == buttonTypeTwo) {
// ... user chose "Two"
} else{
// ... user chose "Three"
}
答案 1 :(得分:1)
您可以添加Mutable Object(MutableBool
class)作为传递参数。由于Mutable对象作为指针传递,因此您可以指定一个值来检查方法。
添加课程:
class MutableBool{
private boolean value;
public void setValue(boolean value) {
this.value = value;
}
public boolean getValue() {
return value;
}
}
并对您的代码进行了一些更改:
public boolean display(String title, String message, MutableBool answer){
...
answer.setValue(true);
}
答案 2 :(得分:1)
如果没有AlertBox alert = new AlertBox("title","message");
alert.setYesAction( e -> {
//TO-DO
});
alert.setNoAction( e -> {
//TO-DO
});
alert.showAndWait();
,我认为你无法做到。如果你真的想要使用自己的类,你可以做这样的事情:
cancel
如果您想检查alert.isCanceled()
状态,无论用户是否按下按钮,您都可以致电:import javafx.event.ActionEvent;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.*;
/**
* An Alert Box for the GUI to display errors to the user.
*
*/
public class AlertBox extends Stage {
private boolean cancel = false;
private EventHandler<ActionEvent> yesAction = null;
private EventHandler<ActionEvent> noAction = null;
public AlertBox(String title, String message) {
super();
initModality(Modality.APPLICATION_MODAL); // If window is up,
// make user handle
// it.
setTitle(title);
setMinHeight(350);
setMinWidth(250);
VBox root = new VBox(); // Root layout.
Label warning = new Label(message); // Message explaining what is wrong.
HBox buttonLayout = new HBox(); // The yes and cancel button.
Button yesButton = new Button("Yes"); // Yes button for the user.
Button noButton = new Button("No"); // No button for the user.
yesButton.setOnAction(e -> {
cancel = false;
yesAction.handle(new ActionEvent());
});
noButton.setOnAction(e -> {
cancel = true;
noAction.handle(new ActionEvent());
});
buttonLayout.getChildren().addAll(yesButton, noButton);
root.getChildren().addAll(warning, buttonLayout);
root.setAlignment(Pos.CENTER);
Scene scene = new Scene(root);
setScene(scene);
}
public void setYesAction(EventHandler<ActionEvent> yesAction){
this.yesAction = yesAction;
}
public void setNoAction(EventHandler<ActionEvent> noAction){
this.noAction = noAction;
}
public boolean isCanceled(){
return cancel;
}
}
;
/**
* @hide
* Sets the capture preset.
* Use this audio attributes configuration method when building an {@link AudioRecord}
* instance with {@link AudioRecord#AudioRecord(AudioAttributes, AudioFormat, int)}.
* @param preset one of {@link MediaRecorder.AudioSource#DEFAULT},
* {@link MediaRecorder.AudioSource#MIC}, {@link MediaRecorder.AudioSource#CAMCORDER},
* {@link MediaRecorder.AudioSource#VOICE_RECOGNITION} or
* {@link MediaRecorder.AudioSource#VOICE_COMMUNICATION}.
* @return the same Builder instance.
*/
@SystemApi
public Builder setCapturePreset(int preset) {
switch (preset) {
case MediaRecorder.AudioSource.DEFAULT:
case MediaRecorder.AudioSource.MIC:
case MediaRecorder.AudioSource.CAMCORDER:
case MediaRecorder.AudioSource.VOICE_RECOGNITION:
case MediaRecorder.AudioSource.VOICE_COMMUNICATION:
mSource = preset;
break;
default:
Log.e(TAG, "Invalid capture preset " + preset + " for AudioAttributes");
}
return this;
}
答案 3 :(得分:0)
或者你可以在主类中处理:
package application;
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
public class Main extends Application {
@Override
public void start(final Stage primaryStage) {
try {
final BorderPane root = new BorderPane();
final Scene scene = new Scene(root, 400, 400);
final AlertBox warning = new AlertBox("Warning!",
"You are about to delete your account. Are you sure you would like to do this?");
warning.addCancelListener(new ChangeListener<Boolean>() {
@Override
public void changed(final ObservableValue<? extends Boolean> observable, final Boolean oldValue, final Boolean newValue) {
if (newValue) {
System.out.println("Tschüss");
} else {
System.out.println("Thanks for confidence");
}
}
});
primaryStage.setScene(scene);
primaryStage.show();
} catch (final Exception e) {
e.printStackTrace();
}
}
public static void main(final String[] args) {
launch(args);
}
}
package application;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.value.ChangeListener;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Modality;
import javafx.stage.Stage;
/**
* An Alert Box for the GUI
* to display errors to the user.
*/
public class AlertBox {
Stage window;
ObjectProperty<Boolean> cancel = new SimpleObjectProperty<>(null);
public AlertBox(final String title, final String message) {
cancel.setValue(null);
display(title, message);
}
private void display(final String title, final String message) {
window = new Stage(); // Create the stage.
window.initModality(Modality.APPLICATION_MODAL); // If window is up, make user handle it.
window.setTitle(title);
window.setMinHeight(350);
window.setMinWidth(250);
window.setAlwaysOnTop(true);
final VBox root = new VBox(); // Root layout.
final Label warning = new Label(message); // Message explaining what is wrong.
final HBox buttonLayout = new HBox(); // The yes and cancel button.
final Button yesButton = new Button("Yes"); // Yes button for the user.
final Button noButton = new Button("No"); // No button for the user.
yesButton.setOnAction(e -> {
cancel.set(false);
close();
});
noButton.setOnAction(e -> {
cancel.set(true);
close();
});
buttonLayout.getChildren().addAll(yesButton, noButton);
root.getChildren().addAll(warning, buttonLayout);
root.setAlignment(Pos.CENTER);
final Scene scene = new Scene(root);
window.setScene(scene);
window.show();
}
/**
* Closes the window
*
* @param variable
*/
private void close() {
window.close();
}
public void addCancelListener(final ChangeListener<Boolean> listener) {
cancel.addListener(listener);
}
}