Javafx:导航到页面

发布于 2024-12-19 11:32:16 字数 1894 浏览 2 评论 0原文

我正在尝试参考 Ensemble 示例的 JavaFX2.0 应用程序。与 Ensemble 一样,我也想根据中心页面上的点击来显示不同的页面。我没有任何树、工具栏等。只想根据特定选择显示一组页面。示例:我的主页可能有6个按钮,单击btn1时我想要Page1,单击btn2时单击Page2,依此类推。在第一页上,我将再次有一些按钮和返回键以返回到上一页。在普通的 Java 中,我们可以使用 CardLayout 轻松实现这一点。将所有分页添加到卡片并显示所需的页面。

查看 Enemble 项目,我看到所有示例页面(例如“AnchorLayout、ColorButton 等”)都扩展了 Sample。 Pages 类具有 AllPagesPage、SamplesPage、DocPage 等成员,这些成员都显示在左侧的 TreeView 中。

我添加了扩展 Pane 的 Sample,创建了另一个扩展 Sample 的类 DataPane。具有所有窗格的引用的第三类:

public class AllPagesPage {
HashMap<String, Sample> pages = null; 
private static String DATAPANE = "DATAPANE";

public AllPagesPage() {
    pages = new HashMap<String, Sample>();
    addPages();
}

private void addPages() {
    pages.put(DATAPANE, (Sample)new DataPane());
}

public Sample getPage(String page) {
    if (pages.containsKey(page))
        return (Sample) pages.get(page);

    return null;

}

}

为了根据名称存储引用,我使用了 HashMap。现在在我的应用程序类中,如何将页面设置为 DataPane ?

    @Override
public void start(Stage primaryStage) {
    primaryStage.setTitle("Hello World");
    Group root = new Group();
    Scene scene = new Scene(root, 300, 250);
    /*
    Button btn = new Button();
    btn.setLayoutX(100);
    btn.setLayoutY(80);
    btn.setText("Hello World");
    btn.setOnAction(new EventHandler<ActionEvent>() {

        public void handle(ActionEvent event) {
            System.out.println("Hello World");
        }
    });
    root.getChildren().add(btn);
    * 
    */

    primaryStage.setScene(scene);
    primaryStage.show();
}

// Should be called as gotoPage(AllPagesPage.DATAPANE), 
// on this command everythign else should be removed and contents of DataPane should come up.      
public void goToPage(String page) {

}

DataPane 仅包含构造函数中 AnchorPaneSample 的代码。没有更多或没有其他功能 - 只有构造函数。

我应该如何获取舞台并设置调用 gotoPage(String page) 方法的页面???

I am trying a JavaFX2.0 application in reference to Ensemble sample. Like Ensemble, I also want to show different pages but based on clicks made on center page. I don't have any Tree, Toolbar, etc. Just want a set of pages to be displayed based on a particular selection. Example: my main page may have 6 buttons, on clicking btn1 I want Page1, Page2 on btn2 click and so on. On Page1, I will again have some buttons and a Return to return back to the previous page. In normal Java, we could achieve this easily using CardLayout. Add all paged to cards and show the desired page.

Looking at Enemble project, I saw that all the samples pages like "AnchorLayout, ColorButton, etc" they all extend Sample. And Pages class has members of AllPagesPage, SamplesPage, DocPage, etc which all are shown in TreeView on left side.

I added Sample that extends Pane, create another class DataPane that extends Sample. A 3rd class that has the reference of all Panes :

public class AllPagesPage {
HashMap<String, Sample> pages = null; 
private static String DATAPANE = "DATAPANE";

public AllPagesPage() {
    pages = new HashMap<String, Sample>();
    addPages();
}

private void addPages() {
    pages.put(DATAPANE, (Sample)new DataPane());
}

public Sample getPage(String page) {
    if (pages.containsKey(page))
        return (Sample) pages.get(page);

    return null;

}

}

To store the refernce based on name I have used HashMap. Now in my application class, how do I setup the page as DataPane ?

    @Override
public void start(Stage primaryStage) {
    primaryStage.setTitle("Hello World");
    Group root = new Group();
    Scene scene = new Scene(root, 300, 250);
    /*
    Button btn = new Button();
    btn.setLayoutX(100);
    btn.setLayoutY(80);
    btn.setText("Hello World");
    btn.setOnAction(new EventHandler<ActionEvent>() {

        public void handle(ActionEvent event) {
            System.out.println("Hello World");
        }
    });
    root.getChildren().add(btn);
    * 
    */

    primaryStage.setScene(scene);
    primaryStage.show();
}

// Should be called as gotoPage(AllPagesPage.DATAPANE), 
// on this command everythign else should be removed and contents of DataPane should come up.      
public void goToPage(String page) {

}

DataPane just contains code from AnchorPaneSample in Constructor. Nothing more or no other functions - ONLY constructor.

How should I get the stage and set pages on call to gotoPage(String page) method ???

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绿光 2024-12-26 11:32:16

只需创建一个您想要查看窗格的区域并更改它们:

public class Main extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    private Pane pagesArea;
    private AllPagesPage pages = new AllPagesPage();

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Hello World");
        VBox root = new VBox();
        Scene scene = new Scene(root, 300, 250);

        Button btn = new Button();
        btn.setText("Open DataPane");
        btn.setOnAction(new EventHandler<ActionEvent>() {

            public void handle(ActionEvent event) {
                goToPage(AllPagesPage.DATAPANE);
            }
        });
        root.getChildren().addAll(btn, pagesArea = new StackPane());
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public void goToPage(String page) {
        Pane pane = pages.getPage(page);
        if (pane != null) {
            pagesArea.getChildren().clear();
            pagesArea.getChildren().add(pane);
        }
    }
}

您可能想要更新您的 AllPagesPage 类:

  • getPage() 中的验证是多余的,HashMap 会为您完成此操作,
  • 而不是您可能希望使用枚举更好的静态字符串灵活性(它将允许迭代、简单比较、更容易重构等)

Just create an area where you want to see your panes and change them:

public class Main extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    private Pane pagesArea;
    private AllPagesPage pages = new AllPagesPage();

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Hello World");
        VBox root = new VBox();
        Scene scene = new Scene(root, 300, 250);

        Button btn = new Button();
        btn.setText("Open DataPane");
        btn.setOnAction(new EventHandler<ActionEvent>() {

            public void handle(ActionEvent event) {
                goToPage(AllPagesPage.DATAPANE);
            }
        });
        root.getChildren().addAll(btn, pagesArea = new StackPane());
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public void goToPage(String page) {
        Pane pane = pages.getPage(page);
        if (pane != null) {
            pagesArea.getChildren().clear();
            pagesArea.getChildren().add(pane);
        }
    }
}

You may want to update your AllPagesPage class:

  • validation in getPage() is redundant, HashMap will do it for you
  • instead of static Strings you may want to use enum for better flexibility (it will allow iteration, plain comparison, easier refactoring and other)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文