重绘BorderPane(javaFx)

时间:2018-03-11 10:26:37

标签: java javafx repaint borderpane

我有一个应用程序,可以创建一个尺寸减小的矩形,例如10秒的时间流逝,但这是当我尝试缩小矩形,窗口错误(场景中没有显示)和等待直到倒计时结束以停止窃听(然后显示矩形不减少)。 我试图在互联网上找到相当于Swing重绘但不是平均的:/ this.requestLayout() - >我在互联网上发现了这个,但它不起作用。 这是我的倒计时代码:

public class Compteur {

    DemoBorderPane p ;

    public DemoBorderPane getPan() {
        if(p==null) {
            p = new DemoBorderPane();
        }
        return p;
    }

    public Compteur() {

    }

    public void lancerCompteur() throws InterruptedException {


       int leTempsEnMillisecondes=1000;

        for (int i=5;i>=0;i--) {
            try {
                Thread.sleep (leTempsEnMillisecondes);
            } 
            catch (InterruptedException e) {
                System.out.print("erreur");
            }
            System.out.println(i);
            getPan().diminuerRect(35);
        }
    }
}

有我的Borderpane代码:

public class DemoBorderPane extends BorderPane {

    private Rectangle r;

    public Rectangle getRect() {
        if(r==null) {
            r = new Rectangle();
             r.setWidth(350);
                r.setHeight(100);
                r.setArcWidth(30);
                r.setArcHeight(30);
                r.setFill( //on remplie notre rectangle avec un dégradé
                        new LinearGradient(0f, 0f, 0f, 1f, true, CycleMethod.NO_CYCLE,
                            new Stop[] {
                                new Stop(0, Color.web("#333333")),
                                new Stop(1, Color.web("#000000"))
                            }
                        )
                    );
        }

        return r;
    }

    public void diminuerRect(int a) {
        getRect().setWidth(getRect().getWidth()-a);
        int c= (int) (getRect().getWidth()-a);
        System.out.println(c);
        this.requestLayout();
        //this.requestFocus();
    }


    public DemoBorderPane() {
        this.setBottom(getRect());

    }
}

有我的主要代码:

public class Main extends Application {
    private DemoBorderPane p;

    public DemoBorderPane getPan() {
        if(p==null) {
            p = new DemoBorderPane();
        }
        return p;
    }

    @Override
    public void start(Stage primaryStage) {
        Compteur c = new Compteur();
        try {

            //Group root = new Group();
            Scene scene = new Scene(getPan(),800,600);
            //scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
            //root.getChildren().add(getPan());
            primaryStage.setScene(scene);
            primaryStage.show();



        } catch(Exception e) {
            e.printStackTrace();
        }

        try {
            c.lancerCompteur();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {

        launch(args);


        /*Son s = null;
        try {
            s = new Son();
        } catch (LineUnavailableException | IOException | UnsupportedAudioFileException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        s.volume(0.1);
        s.jouer();
        c.lancerCompteur();
        s.arreter();*/

    }
}

谢谢;)

1 个答案:

答案 0 :(得分:0)

只要您保持JavaFX应用程序线程忙,它就无法执行布局/呈现。因此,确保在应用程序线程上运行的任何方法都很重要,例如:输入事件的Application.start或事件处理程序返回快。

然而,

lancerCompteur会阻止应用程序线程5秒,因此您看到的唯一结果是方法完成后的最终结果。

通常,您可以在不同的线程上运行这样的代码,并使用Platform.runLater来更新ui。

在这种情况下,您可以利用Timeline类,它允许您在给定的延迟后触发应用程序线程上的事件处理程序:

@Override
public void start(Stage primaryStage) {
    Scene scene = new Scene(getPan(), 800, 600);

    Timeline timeline = new Timeline(new KeyFrame(Duration.seconds(1), event -> {
        getPan().diminuerRect(35);
    }));
    timeline.setCycleCount(5);
    timeline.play();

    primaryStage.setScene(scene);
    primaryStage.show();
}
  • 您还在DemoBorderPane班级和Main班级中使用Compteur的不同实例;场景中显示的Rectangle永远不会更新。
  • 无需在requestLayout中致电diminuerRect。当Rectangle的大小被修改时,会自动发生这种情况。
  • 懒惰的初始化是没有意义的,如果您确定在对象创建期间将调用getter。从它的构造函数调用DemoBorderPane.getRect,因此将初始化移动到构造函数将允许您在不影响功能的情况下删除if检查。
相关问题