【问题标题】:How to show ProgressIndicator in center of Pane in Javafx如何在 Javafx 的窗格中心显示 ProgressIndicator
【发布时间】:2015-09-26 06:34:52
【问题描述】:

我有一个带有一些控件和一个按钮的窗格。当我点击按钮时,我想在窗格中心显示 ProgressIndicator 而不删除任何控件。

当我在按钮的onAction 期间将 ProgressIndicator 添加到窗格时,它会将其添加到按钮下方。我希望它覆盖在窗格上。

下图说明了我想要什么。

代码

package fx;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ProgressIndicator;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class Main extends Application {

@Override
public void start(Stage arg0) throws Exception {
    final VBox bx = new VBox();
    bx.setAlignment(Pos.CENTER);
    TextField userName = new TextField("User Name");
    userName.setMaxWidth(200);
    TextField email = new TextField("Email");
    email.setMaxWidth(200);
    Button submit = new Button("Submit");
    submit.setOnAction(new EventHandler<ActionEvent>() {

        public void handle(ActionEvent event) {
            ProgressIndicator pi = new ProgressIndicator();
            //adding here but it is adding at below of button
            //how to do here
            bx.getChildren().add(pi);
            //Further process
          }
    });
    bx.getChildren().addAll(userName, email, submit);
    Scene c = new Scene(bx);
    arg0.setScene(c);
    arg0.setMinWidth(500);
    arg0.setMinHeight(500);
    arg0.show();
  }

  public static void main(String[] args) {
     Main h = new Main();
     h.launch(args);
  }
}

【问题讨论】:

    标签: javafx javafx-8


    【解决方案1】:

    您需要使用StackPane 作为您的根布局,而不是使用 VBox。 StackPane 允许您将节点堆叠在一起(z 顺序)。

    在按钮的操作上,您可以创建一个新的 ProgressIndicator 并将其添加到您的 StackPane。我已经引入了另一个 VBox 作为指标的父级,因为我不希望指标捕获所有可用空间。你可以disable已经存在的VBox来获得greying对按钮动作的影响,处理完成后你可以再次启用VBox。


    import javafx.application.Application;
    import javafx.event.ActionEvent;
    import javafx.event.EventHandler;
    import javafx.geometry.Pos;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.control.ProgressIndicator;
    import javafx.scene.control.TextField;
    import javafx.scene.layout.StackPane;
    import javafx.scene.layout.VBox;
    import javafx.stage.Stage;
    
    public class Main extends Application {
    
        @Override
        public void start(Stage arg0) throws Exception {
            StackPane root = new StackPane();
            VBox bx = new VBox();
            bx.setAlignment(Pos.CENTER);
            TextField userName = new TextField("User Name");
            userName.setMaxWidth(200);
            TextField email = new TextField("Email");
            email.setMaxWidth(200);
            Button submit = new Button("Submit");
            submit.setOnAction(new EventHandler<ActionEvent>() {
    
                public void handle(ActionEvent event) {
                    ProgressIndicator pi = new ProgressIndicator();
                    VBox box = new VBox(pi);
                    box.setAlignment(Pos.CENTER);
                    // Grey Background
                    bx.setDisable(true);
                    root.getChildren().add(box);
                }
            });
            bx.getChildren().addAll(userName, email, submit);
            root.getChildren().add(bx);
            Scene c = new Scene(root);
            arg0.setScene(c);
            arg0.setMinWidth(500);
            arg0.setMinHeight(500);
            arg0.show();
        }
    
        public static void main(String[] args) {
            launch(args);
        }
    }
    

    【讨论】:

    • 这很好用,但是显示灰色的叠加层我必须在我的布局中添加 vbox,因为 bx.setDisable(true);不足以显示灰色我还使用 Text label = new Text("Loading... Please wait"); 添加了加载文本label.setBoundsType(TextBoundsType.VISUAL); label.setFill(Color.BLACK);
    • 当您想要在按钮单击上进行一些其他工作时,这将不起作用。例如,ProcessIndicator 应在登录按钮单击时显示并在用户验证后隐藏。因为它只有在当前线程完成后才打开。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-06
    • 2018-03-03
    • 2015-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多