如何在Javafx的Pane中心显示ProgressIndicator [英] How to show ProgressIndicator in center of Pane in Javafx

查看:189
本文介绍了如何在Javafx的Pane中心显示ProgressIndicator的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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



下图说明了我想要的内容。





代码

  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;

公共类Main扩展Application {

@Override
public void start(Stage arg0)抛出异常{
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);
按钮提交=新按钮(提交);
submit.setOnAction(new EventHandler< ActionEvent>(){

public void handle(ActionEvent event){
ProgressIndicator pi = new ProgressIndicator();
//在这里添加,但它在按钮下方添加
//这里怎么做
bx.getChildren()。add(pi);
//进一步处理
}
});
bx.getChildren()。addAll(userName,email,submit);
场景c =新场景(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);
}
}


解决方案

你需要使用






  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;

公共类Main扩展Application {

@Override
public void start(Stage arg0)抛出异常{
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);
按钮提交=新按钮(提交);
submit.setOnAction(new EventHandler< ActionEvent>(){

public void handle(ActionEvent event){
ProgressIndicator pi = new ProgressIndicator();
VBox box = new VBox(pi);
box.setAlignment(Pos.CENTER);
// Gray Background
bx.setDisable(true);
root.getChildren()。add (方框);
}
});
bx.getChildren()。addAll(userName,email,submit);
root.getChildren()。add(bx);
场景c =新场景(根);
arg0.setScene(c);
arg0.setMinWidth(500);
arg0.setMinHeight(500);
arg0.show();
}

public static void main(String [] args){
launch(args);
}
}


I have a Pane with some controls and a button. When I click on the button, I want show ProgressIndicator in center of pane without removing any controls.

When I am adding a ProgressIndicator to pane during onAction of button, it adds it below the button. I want it to overlay on the pane.

The picture below explains what I want.

Code

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);
  }
}

解决方案

You need to use a StackPane as your root layout instead of using a VBox. StackPane allows you to stack nodes on top of each other (z-order).

On the button's action you can create a new ProgressIndicator and add it to your StackPane. I have introduced another VBox as the parent to the indicator, because I did not want the indicator to capture all the available space. You can disable the already present VBox to get the greying effect on button's action after the process is done you can enable the VBox again.


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);
    }
}

这篇关于如何在Javafx的Pane中心显示ProgressIndicator的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆