java 如何从 JavaFX 的选项卡中删除关闭按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31531059/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 18:45:34  来源:igfitidea点击:

How to remove close button from tabs in JavaFX

javajavafx

提问by persistent_poltergeist

I have created some tabs in a TabPane. Each time I make a tab it has got a close(x) button on its right side. I don't want the tabs to be removed from the TtabPaneso I have used:

我在TabPane. 每次我制作一个标签时,它的右侧都有一个关闭(x)按钮。我不希望从标签中删除标签,TtabPane所以我使用了:

 TabPane tabPane = new TabPane();
 Tab tab = new Tab("new tab");
 tab.setContents(new Label("Please help"));
 tabPane.getTabs().add(tab);
 tab.setOnCloseRequest(e -> e.consume());

so that it won't be removed. Is there some way not to display this close button on tab.

以免被删除。有什么方法可以不在选项卡上显示这个关闭按钮。

Any help is appreciated.

任何帮助表示赞赏。

回答by NDY

You can set the TabClosingPolicyon a TabPane

您可以设置TabClosingPolicy一个TabPane

myTabPane.setTabClosingPolicy(TabClosingPolicy.UNAVAILABLE);

There are the following possibilities:

有以下几种可能:

  • TabClosingPolicy.ALL_TABS // all tabs can be closed
  • TabClosingPolicy.SELECTED_TAB // only the selected tab can be closed
  • TabClosingPolicy.UNAVAILABLE // you cant close
  • TabClos​​ingPolicy.ALL_TABS // 所有选项卡都可以关闭
  • TabClos​​ingPolicy.SELECTED_TAB // 只能关闭选中的选项卡
  • TabClos​​ingPolicy.UNAVAILABLE // 你不能关闭

If you are adding classes to myTabPane.getTabs()there is also the possibility to set the class to not be closeable (because it needs to extend from Tab):

如果您要添加类,myTabPane.getTabs()还可以将类设置为不可关闭(因为它需要从 扩展Tab):

setClosable(false);

If you define it in the class which extends from TabI guess the policy you set will be useless and is overridden.

如果您在继承自的类中定义它,Tab我猜您设置的策略将毫无用处并被覆盖。

Link to the oracle doc: JavaFX 8 TabPane.TabClosingPolicy

链接到 oracle 文档: JavaFX 8 TabPane.TabClos​​ingPolicy

回答by Pochmurnik

You can also define this using FXML by this code:

您还可以通过以下代码使用 FXML 定义它:

<TabPane tabClosingPolicy="UNAVAILABLE">