簡體   English   中英

如何在沒有FXML的情況下在JavaFX中正確切換場景/更改場景的根節點?

[英]How to properly switch scenes/change root node of scene in JavaFX without FXML?

我正在學習javafx,我已經完成了基礎知識,所以現在我想做一個更復雜的項目。 我在網上閱讀了很多關於這個主題的指南,但是我無法在任何地方找到如何在每個類代表1個場景時切換場景或更改根節點。
為了更好地理解這里是我的項目的簡化版本:
假設我有3個A,B和C類:

public class A{
    private Stage stage;
    A(Stage stage){
         this.stage = stage;
    }
    public void showGui(){
        // This will make all elements, put them on scene and then set that scene to stage
    }
    public void callB(){
        B b = new B(Stage);
        b.showGui();
    }
} 
public class B{
    private Stage stage;
    B(Stage stage){
         this.stage = stage;
    }
    public void showGui(){
        // This will make all elements, put them on scene and then set that scene to stage
    }
    public void callC(){
        C c = new C(Stage);
        c.showGui();
    }
} 
public class C{
  // This is completely same as other two, it calls A so those 3 form some sort of circle.  
}

public void start(Stage primaryStage)中的程序開始時,我創建A的對象並將其傳遞給主要階段,然后在每個類中更改它,並且它工作正常。 但我對此幾乎沒有疑問:
這是一種正確的做法嗎?
有沒有其他方法可以在保持課程的同時完成它,或者我應該在主課程中完成所有操作?
傳遞Scene然后更改根節點更好嗎?

對不起,如果我問得太多,但我讀了很多關於它仍然找不到任何可以幫助我的東西,所以這是我的最后一個解決方案。

您可以在這里進行的設計改進很少:

  1. 您的課程根本不需要了解Stage和其他課程,請參閱隱藏概念 - 他們知道您的課程越不復雜。 場景的根節點就足夠了。 您甚至可以覆蓋節點以避免額外的代碼。

  2. 你的課看起來非常相似。 您可能想要引入父抽象類並將所有切換邏輯委托給單個方法(因此,如果邏輯更改,則不需要更改所有類)

見下一個例子:

public class FxThreeNodes extends Application {

    private abstract class CycledView extends StackPane { // Choose whatever is most appropriate class
        CycledView(CycledView next) {
            this.next = next;

            createGUI();
        }

        abstract void createGUI();

        protected void callNext() {
            getScene().setRoot(next);
        }

        private CycledView next;
    }

    // Here you implement diffent GUIs for A, B and C
    private class A extends CycledView { 

        public A(CycledView next) {
            super(next);
        }

        void createGUI() {
            getChildren().add(new Button("I'm A") {
                @Override
                public void fire() {
                    callNext();
                }

            });
        }

    }

    private class B extends CycledView { 

        public B(CycledView next) {
            super(next);
        }

        void createGUI() {
            getChildren().add(new Button("This is B") {
                @Override
                public void fire() {
                    callNext();
                }

            });
        }

    }

    private class C extends CycledView { 

        public C(CycledView next) {
            super(next);
        }

        void createGUI() {
            getChildren().add(new Button("Greeting from C") {
                @Override
                public void fire() {
                    callNext();
                }

            });
        }

    }


    @Override
    public void start(Stage primaryStage) {

        CycledView c = new C(null);
        CycledView b = new B(c);
        CycledView a = new A(b);
        c.next = a;

        Scene scene = new Scene(a, 300, 250);

        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

}

暫無
暫無

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

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