将Swing进度条转换为JavaFx进度条
我的Swing进度条课程如下:将Swing进度条转换为JavaFx进度条,swing,javafx,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{ MyPr
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;
}
我需要一种将上述类转换为纯javafx的方法。我是一名Java和javafx新手,希望能得到任何帮助。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);
}
}
导入javafx.application.application;
导入javafx.geometry.Pos;
导入javafx.scene.Group;
导入javafx.scene.scene;
导入javafx.scene.control.Label;
导入javafx.scene.control.ProgressBar;
导入javafx.scene.control.ProgressIndicator;
导入javafx.scene.layout.HBox;
导入javafx.scene.layout.VBox;
导入javafx.stage.stage;
公共类主扩展应用程序{
最终浮点[]值=新浮点[]{-1.0f,0f,0.6f,1.0f};
最终标签[]标签=新标签[values.length];
最终进度条[]pbs=新进度条[values.length];
最终ProgressIndicator[]引脚=新ProgressIndicator[值.长度];
最终HBox hbs[]=新HBox[值.长度];
@凌驾
公众假期开始(阶段){
组根=新组();
场景=新场景(根,300,150);
scene.getStylesheets().add(“progresssample/Style.css”);
舞台场景;
阶段。设置标题(“进度控制”);
对于(int i=0;i
我希望它对您有所帮助。您的swing代码实际上已被破坏:它从后台线程更新UI,这违反了swing的单线程规则。在Swing中,这意味着您的程序可以在基本上任意的时间产生错误。(参见教程)在JavaFX中,许多违反等效规则的调用抛出
IllegalStateException
s。看见
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);
}
}