簡體   English   中英

重繪BorderPane(javaFx)

[英]Repaint BorderPane (javaFx)

我有一個可以創建尺寸減小的矩形的應用程序,例如經過10秒的時間,但是這里是當我嘗試縮小矩形時,出現窗口錯誤(場景中未顯示任何內容)並等待倒計時完成以停止錯誤檢查(然后顯示未縮小的矩形)。 我試圖在Internet上找到與Swing中的重繪等效的功能,但不是平均水平:/ this.requestLayout()->我在Internet上找到了此功能,但是它不起作用。 這是我的倒計時代碼:

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();*/

    }
}

謝謝 ;)

只要您使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();
}

  • 您還可以在Main類和Compteur類中使用DemoBorderPane不同實例; 場景中顯示的Rectangle永遠不會進行更新。
  • 無需在diminuerRect調用requestLayout 修改Rectangle的大小時,這會自動發生。
  • 如果您確定在對象創建期間將調用getter,則延遲初始化是沒有意義的。 DemoBorderPane.getRect是從其構造函數調用的,因此將初始化移至構造函數將使您擺脫if檢查,而不會影響功能。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM