JavaFX等待值设置完毕
我正在努力解决以下问题: 我的javafx应用程序中有一个类似于弹出窗口的东西,它应该会阻止应用程序,直到用户做出一些输入。它不是一个单独的阶段,我可以调用showAndWait()然后返回阶段的输入。弹出窗口被实现为一个窗格,放置在其他组件上。现在我做了这样的事情:JavaFX等待值设置完毕,java,user-interface,javafx,Java,User Interface,Javafx,我正在努力解决以下问题: 我的javafx应用程序中有一个类似于弹出窗口的东西,它应该会阻止应用程序,直到用户做出一些输入。它不是一个单独的阶段,我可以调用showAndWait()然后返回阶段的输入。弹出窗口被实现为一个窗格,放置在其他组件上。现在我做了这样的事情: PopupPane pp = new PopupPane() stackPane.add(new PopupPane()); //show pane //... waiting until user terminates popu
PopupPane pp = new PopupPane()
stackPane.add(new PopupPane()); //show pane
//... waiting until user terminates popup
return pp.getInput(); //returns input when user terminates popup
因此,我希望pp.getInput()等待,直到用户按下OK/CANCEL/APPLY/。。。按钮在我的弹出窗口。在这种情况下,如何实现showAndWait()之类的功能?一种可能的方法是使用一个额外的窗格,在显示“弹出窗口”时可以禁用该窗格 例如,假设
场景的根布局窗格是堆栈窗格
。您可以将界面的所有其余部分包装在另一个窗格中,您将根据需要禁用或启用该窗格
当需要显示“弹出窗口”时,将其添加到您的StackPane
并禁用“内容”窗格。当弹出窗口关闭时,只需将其从堆栈窗格中删除
,然后重新启用“内容”窗格
下面是一个快速且公认不具吸引力的概念示例:
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class SimulatedPopupExample extends Application {
private static StackPane root;
private static BorderPane content;
public static void main(String[] args) {
launch(args);
}
private static void showPopup() {
// Disable the main layout pain
content.setDisable(true);
VBox popup = new VBox();
popup.setPadding(new Insets(10));
popup.setAlignment(Pos.CENTER);
popup.setStyle("-fx-border-color: black; -fx-background-color: -fx-base;");
popup.setMaxSize(200, 200);
popup.getChildren().add(
new Button("Close Popup") {{
setOnAction(event -> {
// Re-enable the pane
content.setDisable(false);
// Remove popup from root layout
root.getChildren().remove(popup);
});
}}
);
// Add popup to root layout
root.getChildren().add(popup);
}
@Override
public void start(Stage primaryStage) {
// Simple Interface
root = new StackPane();
content = new BorderPane(
new Button("Show \"Popup\"") {{
setOnAction(e -> showPopup());
}},
new Button("Top Button"),
new Button("Right Button"),
new Button("Bottom Button"),
new Button("Left Button")
);
root.getChildren().add(content);
// Show the stage
primaryStage.setScene(new Scene(root));
primaryStage.setWidth(500);
primaryStage.setHeight(400);
primaryStage.setTitle("SimulatedPopupExample Sample");
primaryStage.show();
}
}
结果:
一种可能的方法是在显示“弹出窗口”时使用一个可禁用的附加窗格
例如,假设场景的根布局窗格是堆栈窗格
。您可以将界面的所有其余部分包装在另一个窗格中,您将根据需要禁用或启用该窗格
当需要显示“弹出窗口”时,将其添加到您的StackPane
并禁用“内容”窗格。当弹出窗口关闭时,只需将其从堆栈窗格中删除
,然后重新启用“内容”窗格
下面是一个快速且公认不具吸引力的概念示例:
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class SimulatedPopupExample extends Application {
private static StackPane root;
private static BorderPane content;
public static void main(String[] args) {
launch(args);
}
private static void showPopup() {
// Disable the main layout pain
content.setDisable(true);
VBox popup = new VBox();
popup.setPadding(new Insets(10));
popup.setAlignment(Pos.CENTER);
popup.setStyle("-fx-border-color: black; -fx-background-color: -fx-base;");
popup.setMaxSize(200, 200);
popup.getChildren().add(
new Button("Close Popup") {{
setOnAction(event -> {
// Re-enable the pane
content.setDisable(false);
// Remove popup from root layout
root.getChildren().remove(popup);
});
}}
);
// Add popup to root layout
root.getChildren().add(popup);
}
@Override
public void start(Stage primaryStage) {
// Simple Interface
root = new StackPane();
content = new BorderPane(
new Button("Show \"Popup\"") {{
setOnAction(e -> showPopup());
}},
new Button("Top Button"),
new Button("Right Button"),
new Button("Bottom Button"),
new Button("Left Button")
);
root.getChildren().add(content);
// Show the stage
primaryStage.setScene(new Scene(root));
primaryStage.setWidth(500);
primaryStage.setHeight(400);
primaryStage.setTitle("SimulatedPopupExample Sample");
primaryStage.show();
}
}
结果:
这是一个自定义对话框,只有在用户在文本字段中输入数据后才能关闭或接受该对话框
private void showInputTextDialog(){
Label lblAmt = new Label("Enter Amount");
Button btnOK = new Button("OK");
TextField txtAmt = new TextField();
AnchorPane customDialog = new AnchorPane();
customDialog.setStyle("-fx-border-color:red;-fx-border-width:10px; -fx-background-color: lightblue;");
customDialog.getChildren().addAll(lblAmt,btnOK,txtAmt);
lblAmt.setLayoutX(30);
lblAmt.setLayoutY(30);
txtAmt.setLayoutX(164);
txtAmt.setLayoutY(25);
txtAmt.setMaxWidth(116);
btnOK.setLayoutX(190);
btnOK.setLayoutY(100);
btnOK.setStyle("-fx-font-size: 18px;-fx-font-weight: bold;");
lblAmt.setStyle("-fx-font-size: 18px;-fx-font-weight: bold;");
txtAmt.setStyle("-fx-font-size: 18px;-fx-font-weight: bold;");
Scene secondScene = new Scene(customDialog, 300, 180);
EventHandler<ActionEvent> filter = event -> {
if(txtAmt.getText().isEmpty()) {
event.consume();
}
};
// New window (Stage)
Stage newWindow = new Stage();
//newWindow.initStyle(StageStyle.UNDECORATED);
newWindow.initModality(Modality.APPLICATION_MODAL);
newWindow.setResizable(false);
//newWindow.setTitle("Custom Dialog");
newWindow.setScene(secondScene);
btnOK.addEventHandler(ActionEvent.ACTION,filter);
btnOK.setOnAction(evt -> {
String str = txtAmt.getText();
System.out.println("@@@@@@@@@@@@@@@@ str "+str);
if(txtAmt.getText().trim().equals("")) {
evt.consume();
txtAmt.clear();
txtAmt.requestFocus();
}else{
txAMT = Double.valueOf(str);
newWindow.close();
}
});
newWindow.setOnCloseRequest(event -> {
if(txtAmt.getText().isEmpty()) {
event.consume();
}
});
txtAmt.requestFocus();
newWindow.showAndWait();
}
private void showInputTextDialog(){
标签lblAmt=新标签(“输入金额”);
按钮btnOK=新按钮(“确定”);
TextField txtmat=新建TextField();
AnchorPane customDialog=新的AnchorPane();
customDialog.setStyle(“-fx边框颜色:红色;-fx边框宽度:10px;-fx背景颜色:浅蓝色;”);
customDialog.getChildren().addAll(lblAmt、btnOK、txtmat);
lblAmt.setLayoutX(30);
lblAmt.setLayoutY(30);
txtAmt.setLayoutX(164);
txtAmt.setLayoutY(25);
txtAmt.setMaxWidth(116);
btnOK.setLayoutX(190);
btnOK.setLayoutY(100);
btnOK.setStyle(“-fx字体大小:18px;-fx字体大小:粗体;”);
lblAmt.setStyle(“-fx字体大小:18px;-fx字体大小:粗体;”);
txtAmt.setStyle(“-fx字体大小:18px;-fx字体大小:粗体;”);
场景secondScene=新场景(customDialog,300,180);
EventHandler筛选器=事件->{
if(txtmat.getText().isEmpty()){
event.consume();
}
};
//新窗口(舞台)
Stage newWindow=newstage();
//newWindow.initStyle(StageStyle.Undecoraded);
newWindow.initmodal(MODAL.APPLICATION_MODAL);
newWindow.setResizeable(false);
//setTitle(“自定义对话框”);
newWindow.setScene(第二场景);
btnOK.addEventHandler(ActionEvent.ACTION,filter);
btnOK.setOnAction(evt->{
字符串str=txtmat.getText();
System.out.println(“@@@@@@@@@@@@@@@@@@@@@@@@@@str”+str);
if(txtmat.getText().trim().equals(“”){
evt.consume();
txtmat.clear();
txtmat.requestFocus();
}否则{
txAMT=双值(str);
newWindow.close();
}
});
newWindow.setOnCloseRequest(事件->{
if(txtmat.getText().isEmpty()){
event.consume();
}
});
txtmat.requestFocus();
newWindow.showAndWait();
}
这是一个自定义对话框,只有在用户在文本字段中输入数据后才能关闭或接受该对话框
private void showInputTextDialog(){
Label lblAmt = new Label("Enter Amount");
Button btnOK = new Button("OK");
TextField txtAmt = new TextField();
AnchorPane customDialog = new AnchorPane();
customDialog.setStyle("-fx-border-color:red;-fx-border-width:10px; -fx-background-color: lightblue;");
customDialog.getChildren().addAll(lblAmt,btnOK,txtAmt);
lblAmt.setLayoutX(30);
lblAmt.setLayoutY(30);
txtAmt.setLayoutX(164);
txtAmt.setLayoutY(25);
txtAmt.setMaxWidth(116);
btnOK.setLayoutX(190);
btnOK.setLayoutY(100);
btnOK.setStyle("-fx-font-size: 18px;-fx-font-weight: bold;");
lblAmt.setStyle("-fx-font-size: 18px;-fx-font-weight: bold;");
txtAmt.setStyle("-fx-font-size: 18px;-fx-font-weight: bold;");
Scene secondScene = new Scene(customDialog, 300, 180);
EventHandler<ActionEvent> filter = event -> {
if(txtAmt.getText().isEmpty()) {
event.consume();
}
};
// New window (Stage)
Stage newWindow = new Stage();
//newWindow.initStyle(StageStyle.UNDECORATED);
newWindow.initModality(Modality.APPLICATION_MODAL);
newWindow.setResizable(false);
//newWindow.setTitle("Custom Dialog");
newWindow.setScene(secondScene);
btnOK.addEventHandler(ActionEvent.ACTION,filter);
btnOK.setOnAction(evt -> {
String str = txtAmt.getText();
System.out.println("@@@@@@@@@@@@@@@@ str "+str);
if(txtAmt.getText().trim().equals("")) {
evt.consume();
txtAmt.clear();
txtAmt.requestFocus();
}else{
txAMT = Double.valueOf(str);
newWindow.close();
}
});
newWindow.setOnCloseRequest(event -> {
if(txtAmt.getText().isEmpty()) {
event.consume();
}
});
txtAmt.requestFocus();
newWindow.showAndWait();
}
private void showInputTextDialog(){
标签lblAmt=新标签(“输入金额”);
按钮btnOK=新按钮(“确定”);
TextField txtmat=新建TextField();
AnchorPane customDialog=新的AnchorPane();
customDialog.setStyle(“-fx边框颜色:红色;-fx边框宽度:10px;-fx背景颜色:浅蓝色;”);
customDialog.getChildren().addAll(lblAmt、btnOK、txtmat);
lblAmt.setLayoutX(30);
lblAmt.setLayoutY(30);
txtAmt.setLayoutX(164);
txtAmt.setLayoutY(25);
txtAmt.setMaxWidth(116);
btnOK.setLayoutX(190);
btnOK.setLayoutY(100);
btnOK.setStyle(“-fx字体大小:18px;-fx字体大小:粗体;”);
lblAmt.setStyle(“-fx字体大小:18px;-fx字体大小:粗体;”);
txtAmt.setStyle(“-fx字体大小:18px;-fx字体大小:粗体;”);
场景secondScene=新场景(customDialog,300,180);
EventHandler筛选器=事件->{
if(txtmat.getText().isEmpty()){
event.consume();
}
};
//新窗口(舞台)
Stage newWindow=newstage();
//newWindow.initStyle(StageStyle.Undecoraded);
newWindow.initmodal(MODAL.APPLICATION_MODAL);
newWindow.setResizeable(false);
//setTitle(“自定义对话框”);
newWindow.setScene(第二场景);
btnOK.addEventHandler(ActionEvent.ACTION,filter);
btnOK.setOnAction(evt->{
字符串str=txtmat.getText();
系统