Javafx:导航到页面
我正在尝试参考 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需创建一个您想要查看窗格的区域并更改它们:
您可能想要更新您的 AllPagesPage 类:
Just create an area where you want to see your panes and change them:
You may want to update your AllPagesPage class: