[英]Is it possible to return a variable in javaFX from a close method? Is there a more efficient way to do this?
我想運行一種方法,當選擇了答案或手動關閉了窗口后將關閉彈出窗口,然后返回選擇的值(布爾值)。 不幸的是,我不知道如何檢索數據,因為我最初調用的是顯示查詢的方法,而不是close方法。
我嘗試了許多不同的方法,但是當我嘗試使用close方法時,無法檢索從其返回的數據。 否則,數據將在用戶能夠對其進行突變之前返回。 均未達到我的目標
這是我創建盒子的方法。 我目前只是關閉窗口,而不是通過其他關閉方法關閉。
public class ConfirmBox
{
static boolean answer;
public static boolean display(String title, String question) {
Stage window = new Stage();
//Block events to other windows
window.initModality(Modality.APPLICATION_MODAL);
window.setTitle(title);
window.setMinWidth(250);
//LabelQ
Label label = new Label();
label.setText(question);
//YesBox
Button yesB= new Button("Yes");
yesB.getStyleClass().add("button-green");
yesB.setOnAction(e -> {
answer=true;
window.close();
});
//NoBox
Button noB= new Button("No");
noB.getStyleClass().add("button-red");
yesB.setOnAction(e -> {
answer=false;
window.close();
});
window.setOnCloseRequest(e ->{
e.consume();
window.close();
});
VBox layout = new VBox(10);
layout.getChildren().addAll(label, yesB, noB);
layout.setAlignment(Pos.CENTER);
//Display window and wait for it to be closed before returning
Scene scene = new Scene(layout, 800, 200);
scene.getStylesheets().add("styles.css");
window.setScene(scene);
window.showAndWait();
return answer;
}
}
這是我到目前為止運行的所有方法。
public class Main extends Application
{
Stage window;
String gender;
String name;
public static void main (String[] args){
launch (args);
}
public void start(Stage primaryStage) throws Exception{
window= primaryStage;
window.setTitle("Pokemon Gray");
GridPane grid= new GridPane();
grid.setPadding(new Insets(10, 10, 10, 10));
grid.setVgap(8);
grid.setHgap(10);
// Name Label
Label nameLabel= new Label("UserName");
GridPane.setConstraints(nameLabel,0, 0);
//Name input
TextField nameInput = new TextField();
GridPane.setConstraints(nameInput,1, 0);
//GenderLabel
Label genderLabel= new Label("Gender");
GridPane.setConstraints(genderLabel,0, 1);
//Gender Boy
Button boySelect= new Button("Boy");
boySelect.getStyleClass().add("button-blue");
GridPane.setConstraints(boySelect, 1, 1);
boySelect.setOnAction(e-> gender="Male");
//Gender Girl
Button girlSelect= new Button("Girl");
girlSelect.getStyleClass().add("button-red");
GridPane.setConstraints(girlSelect, 2, 1);
girlSelect.setOnAction(e-> gender="Female");
//Login
Button saveName= new Button("Save Name");
GridPane.setConstraints(saveName, 1, 2);
saveName.setOnAction( e -> {
boolean valid =verifyText(nameInput.getText());
if ((valid)&&((gender == "Male")||(gender=="Female"))){
String qStr= "Are you sure your name is "+nameInput.getText()+" and you are a "+gender+"?";
boolean confirmed= ConfirmBox.display("Confirmation", qStr );
if (confirmed == true){
name= nameInput.getText();
}
}
else{
ErrorBox.display("Error", "You must enter a name less than 20 characters long and a gender");
}
});
grid.getChildren().addAll(nameLabel, nameInput, genderLabel, boySelect, girlSelect, saveName);
Scene scene= new Scene(grid, 1000, 1000);
scene.getStylesheets().add("styles.css");
window.setScene(scene);
window.show();
}
public boolean verifyText(String text){
boolean valid;
if ((text.length() > 0) && (text.length() <= 20)){
valid= true;
}
else{
valid=false;
}
return valid;
}
}
我希望能夠運行一個單獨的方法來關閉它,並將ConfirmBox中的答案布爾值返回給Main方法。 但是當前它返回null,因為它立即返回。
編輯:應該注意,我目前沒有關閉方法。 我刪除了上一個,因為它引起了很多問題。
我認為您無法做到這一點,您需要執行另一個操作,例如:
If(button.getResult==yes){"charge yesscreen")else{"charge no screen"}
差異屏幕的控制器不能與其他屏幕交互,您只能將差異屏幕從調用到控制器,但是您不能通過他傳遞值,請嘗試執行類似的操作我以這種心態使用javafx,所有屏幕都是獨立的並且您僅調用不同的屏幕。 您可以執行以下操作:
class first screen{
is a girl?
is a charizar?
is a x)
if(isgirl && is a x...){
call screen x
}else if(ismen&& is a z..){
call screen z
}
{
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.