【问题标题】:JavaFX: Stretching Tabs to fit parent TabPaneJavaFX:拉伸选项卡以适合父 TabPane
【发布时间】:2018-01-18 18:48:45
【问题描述】:

我正在使用以下代码使 TabPane 的组合选项卡宽度适合 TabPane 的宽度。

private void initTabPane() {

    // Populate Tabs
    List<Tab> tabs = _tabPane.getTabs();
    tabs.add(new Tab("Metadator", _metadatorView));
    tabs.add(new Tab("Translator", _translatorView));
    tabs.add(new Tab("ESPN"));

    // Stretch to fit width 
    _tabPane.tabMinWidthProperty().bind(_tabPane.widthProperty()
                                                .divide(_tabPane.getTabs().size())                                                   );
}

我一直在尝试删除标签达到相对于 TabPane 宽度的特定宽度时显示的 tab-down-button 视图。您可以在下图的右上角看到:

我尝试使用其类 .control-buttons-tab, .container, .tab-down-button, .arrow 的 padding/margin/bg-color 属性,但没有任何效果。

有没有什么办法可以去掉它,或者把它偏移到很远才不会干扰最后一个标签?

【问题讨论】:

  • 你不能在伸展范围内考虑它以适应宽度绑定吗?
  • @Sedrick 我可以,但我试图让最后一个选项卡触摸窗口的右侧。
  • 我对你的问题投了赞成票,希望它能吸引更多有经验的程序员参与讨论。

标签: java javafx javafx-8 javafx-2


【解决方案1】:

这是在正确的轨道上,但是您必须对其进行一些更改。您基本上需要将Tabs' 宽度设置为一定长度。然后你需要将TabPane's 的宽度设置为所有Tabs 加在一起的长度。最后,添加一个小缓冲区,这样下拉菜单就不会出现。 -> 见代码中的+55

import java.util.List;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class Main extends Application
{

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

    @Override
    public void start(Stage primaryStage)
    {
        primaryStage.setTitle("Tabs");
        Group root = new Group();

        TabPane tabPane = new TabPane();

        BorderPane borderPane = new BorderPane();
        List<Tab> tabs = tabPane.getTabs();
        tabs.add(new Tab("Metadator"));
        tabs.add(new Tab("Translator"));
        tabs.add(new Tab("ESPN"));

        tabPane.tabMinWidthProperty().set(100);//set the tabPane's tabs min and max widths to be the same.
        tabPane.tabMaxWidthProperty().set(100);//set the tabPane's tabs min and max widths to be the same.
        System.out.println(tabPane.tabMinWidthProperty().get());
        tabPane.setMinWidth((100 * tabPane.getTabs().size()) + 55);//set the tabPane's minWidth and maybe max width to the tabs combined width + a padding value
        tabPane.setPrefWidth((100 * tabPane.getTabs().size()) + 55);//set the tabPane's minWidth and maybe max width to the tabs combined width + a padding value
        borderPane.setCenter(tabPane);
        root.getChildren().add(borderPane);

        Scene scene = new Scene(root, (100 * tabPane.getTabs().size()) + 55, 250, Color.WHITE);//You might need to set the scene's with also
        primaryStage.setScene(scene);
        primaryStage.show();
    }

}

【讨论】:

  • 感谢您抽出宝贵的时间 sedrick,但真正的问题是我不想要缓冲区。希望有一种方法可以将顶部紧按钮的逻辑委托给上下文菜单,然后摆脱它。
  • 你可能需要检查它何时出现,然后看看你是否可以隐藏它。
猜你喜欢
  • 1970-01-01
  • 2017-01-28
  • 2013-01-02
  • 2016-09-14
  • 1970-01-01
  • 2011-10-17
  • 1970-01-01
  • 2014-07-16
  • 2017-07-04
相关资源
最近更新 更多