将Swing Progress栏转换为JavaFx progressBar

时间:2016-04-07 08:32:00

标签: swing javafx

我的Swing进度条类如下:

public class MyProgessBar extends JDialog implements Runnable {
/**
 * @param string
 */
private JProgressBar progressBar;
private boolean cancelled=false; 

public static void main(String[] args) throws InterruptedException{
    MyProgessBar p = new MyProgessBar("Test");
    new Thread(p).start();
    for(int i=0;i<500;i++){
        p.setMsg("Its "+i+"%");
        p.setDone(i);
        Thread.sleep(200);
    }
    p.dispose();
}

public MyProgessBar(String title) {
    setTitle(title);
    progressBar = new JProgressBar();
    progressBar.setValue(0);
    progressBar.setStringPainted(true);
    progressBar.setIndeterminate(true);
    add(progressBar);
    setSize(400, 50);
    setLocationRelativeTo(null);
    setModal(true);
    SwingUtilities.invokeLater(this);
}

/**
 * @param string
 */
public void setTaskName(String string) {
    progressBar.setString(string);
    setTitle(string);
}

/**
 * @param string
 */
public void setMsg(String string) {
    progressBar.setString(string);

}

/**
 * @param rows
 */
public void setDone(int rows) {
    progressBar.setValue(rows);
}

/**
 * 
 */
public void taskFinished() {
    setVisible(false);  
    dispose();  
}

public void run() {
    setVisible(true);
    progressBar.setVisible(true);
}

protected void processWindowEvent(WindowEvent e) {
    if (e.getID() == WindowEvent.WINDOW_CLOSING) {
        this.cancelled = true;
    }
    super.processWindowEvent(e);
}

public boolean isCancelled(){
    return cancelled;
}

}

我正在使用定义的方法调用我的进度条

    MyProgess bar = new MyProgess("Test");
    bar.setMsg("Test");

我需要一种方法将上面的类转换为纯粹的javafx。我是Java和javafx的新手,非常感谢我能得到的任何帮助。

1 个答案:

答案 0 :(得分:0)

下面给出了JavaFX ProgressBar的代码。我从JavaFX文档中提到它。

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressBar;
import javafx.scene.control.ProgressIndicator;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class Main  extends Application {

final Float[] values = new Float[] {-1.0f, 0f, 0.6f, 1.0f};
final Label [] labels = new Label[values.length];
final ProgressBar[] pbs = new ProgressBar[values.length];
final ProgressIndicator[] pins = new ProgressIndicator[values.length];
final HBox hbs [] = new HBox [values.length];

    @Override
    public void start(Stage stage) {
        Group root = new Group();
        Scene scene = new Scene(root, 300, 150);
        scene.getStylesheets().add("progresssample/Style.css");
        stage.setScene(scene);
        stage.setTitle("Progress Controls");


        for (int i = 0; i < values.length; i++) {
            final Label label = labels[i] = new Label();
            label.setText("progress:" + values[i]);

            final ProgressBar pb = pbs[i] = new ProgressBar();
            pb.setProgress(values[i]);

            final ProgressIndicator pin = pins[i] = new ProgressIndicator();
            pin.setProgress(values[i]);
            final HBox hb = hbs[i] = new HBox();
            hb.setSpacing(5);
            hb.setAlignment(Pos.CENTER);
            hb.getChildren().addAll(label, pb, pin);
        }

        final VBox vb = new VBox();
        vb.setSpacing(5);
        vb.getChildren().addAll(hbs);
        scene.setRoot(vb);
        stage.show();
    }
    public static void main(String[] args) {
        launch(args);
    }
}

我希望它可以帮助你......