2016-02-27 100 views
0

我一直在创建一些具有多个窗格的GUI,并且用户需要能够在这些窗格之间进行切换。因此,我一直在寻找内置的控件,这将允许我处理这个问题。我相信两个明显的选择是TabPane和手风琴。我也遇到了分页控制。有关JavaFX分页控件的使用和行为的查询

我的第一个问题:在文档中,分页控件通常被描述为一种通过分成较小部分的内容进行导航的方式。我使用它的方式更类似于简单地将不相关内容的窗格分隔到不同的页面上,就像使用TabPane一样。为此目的使用分页控制有什么问题吗?有什么优点/缺点VS这种方法和TabPane?

我已经通过设置页面出厂如下与分页控制来实现它:

this.setPageCount(2); 
this.setPageFactory((Integer index) -> { 
      if (index == 0) { 
       return myFirstPane; 
      } else { 
       return mySecondPane; 
      } 
     }); 

那里有两页,每一个都有自己的窗格中显示。在这种情况下,'this'代表扩展分页的类。我的第二个问题:有多少范围可以自定义分页控件,例如:而不是它为页面显示1,2,3等,我可以为每个页面选择一个自定义消息,例如, “第一个窗格”,“第二个窗格”等?

另外,有没有一种方法来定位分页控件的不同,因为默认情况下它看起来坐在我与之关联的窗格下面,但如果我想让它出现在上面呢?

回答

0

分页控件并不是为了满足您的特定需求而定制的,所以您最好创建自己的组件。已经做了你需要看起来会是什么临客一个快速和肮脏的导航组件:

package sample; 

import javafx.application.Application; 
import javafx.beans.binding.Bindings; 
import javafx.geometry.Pos; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.control.Label; 
import javafx.scene.layout.BorderPane; 
import javafx.scene.layout.HBox; 
import javafx.scene.layout.StackPane; 
import javafx.stage.Stage; 

public class Main extends Application { 

    @Override 
    public void start(Stage stage) throws Exception { 
     // Define the different panes 
     StackPane pane1 = new StackPane(new Label("Pane 1")); 
     StackPane pane2 = new StackPane(new Label("Pane 2")); 

     // This is the control that holds the active pane and the navigation 
     BorderPane root = new BorderPane(); 

     // Create buttons to select active pane 
     Button button1 = new Button("Pane 1"); 
     button1.setOnAction(e -> root.setCenter(pane1)); 
     button1.disableProperty().bind(Bindings.createBooleanBinding(() -> root.getCenter() == pane1, root.centerProperty())); 

     Button button2 = new Button("Pane 2"); 
     button2.setOnAction(e -> root.setCenter(pane2)); 
     button2.disableProperty().bind(Bindings.createBooleanBinding(() -> root.getCenter() == pane2, root.centerProperty())); 

     // Add navigation to the bottom 
     HBox menu = new HBox(button1, button2); 
     menu.setAlignment(Pos.CENTER); 
     root.setBottom(menu); 

     // Enable pane1 by default 
     root.setCenter(pane1); 

     stage.setScene(new Scene(root, 400, 300)); 
     stage.show(); 
    } 

} 

希望这是帮助:)

+0

我忘了提 - 'TabPane'似乎有你想要的确切功能,所以不要使用自定义组件,除非您有理由例如使其看起来完全不同。 –

+0

是否有任何实际问题,虽然使用分页控件的pageFactory,因为我有 - 我没有看到我做了什么特别错误的写作:this.setPageFactory((Integer index) - > { if(index == 0){ return myFirstPane; } else { return mySecondPane; } }); – Tranquility

+0

不,这很好,但正如您所经历的,您无法真正改变分页控件的外观和文本。我通过使用node.lookup并更改不同事件上的文本来完成了几次,但这很容易出错,并且可能会发生变化,因为我们正在使用内部API。还有几个听众会再次覆盖你的改变,所以这是一个烦人的路线。 –