如何在没有FXML的情况下在JavaFX中正确切换场景/更改场景的根节点?

时间:2017-05-03 13:12:56

标签: java javafx javafx-8

我正在学习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 个答案:

答案 0 :(得分:4)

您可以在这里进行很少的设计改进:

  1. 您的课程根本不需要了解Stage和其他课程,请参阅hiding concept - 他们知道您的课程越不复杂。场景的根节点就足够了。您甚至可以覆盖节点以避免额外的代码。

  2. 你的课看起来非常相似。您可能想要引入父抽象类并将所有切换逻辑委托给单个方法(因此,如果您的逻辑发生更改,则不需要更改所有类)

  3. 见下一个例子:

    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);
        }
    
    }
    
相关问题