JavaFX布局-TabPane
- 常用属性
- padding
- side
- tabClosingPolicy
- tabDragPolicy
- 实现方式
- Java实现
- fxml实现
- 组织一组tab的容器,可以设置关闭,拖拽等
- 每个tab内容可以设置不同容器数据
常用属性
padding
内边距,可以单独设置上、下、左、右的内边距
tabPane.setPadding(new Insets(10, 10, 10, 10));
side
设置tabs的位置,LEFT
tabPane.setSide(Side.LEFT);public enum Side {TOP,BOTTOM,LEFT,RIGHT;
}
tabClosingPolicy
tab关闭策略
tabPane.setTabClosingPolicy(TabPane.TabClosingPolicy.UNAVAILABLE);public enum TabClosingPolicy {/*** 仅仅当前选中的tab可以关闭*/SELECTED_TAB,/*** 所有tab都可关闭*/ALL_TABS,/*** 所有tab都不可关闭.*/UNAVAILABLE}
tabDragPolicy
拖拽策略
tabPane.setTabDragPolicy(TabPane.TabDragPolicy.FIXED);public enum TabDragPolicy {/*** 固定不允许拖拽*/FIXED,/*** 允许拖拽*/REORDER}
实现方式
Java实现
public static TabPane demo1() {TabPane tabPane = new TabPane();// 内边距tabPane.setPadding(new Insets(10, 10, 10, 10));// 位置tabPane.setSide(Side.LEFT);// 关闭策略tabPane.setTabClosingPolicy(TabPane.TabClosingPolicy.UNAVAILABLE);// 拖拽策略tabPane.setTabDragPolicy(TabPane.TabDragPolicy.FIXED);tabPane.setRotateGraphic(false);tabPane.setCenterShape(false);Tab tab1 = new Tab("Tab 1");FlowPane flowPane1 = new FlowPane();flowPane1.setOrientation(Orientation.HORIZONTAL);for (int i = 1; i <= 5; i++) {flowPane1.getChildren().add(new Button("Button " + i));}tab1.setContent(flowPane1);Tab tab2 = new Tab("Tab 2");FlowPane flowPane2 = new FlowPane();flowPane2.setOrientation(Orientation.VERTICAL);for (int i = 6; i <= 10; i++) {flowPane2.getChildren().add(new Button("Button " + i));}tab2.setContent(flowPane2);tabPane.getTabs().addAll(tab1, tab2);return tabPane;}
fxml实现
<StackPane prefHeight="400" prefWidth="600" xmlns="http://javafx.com/javafx/17.0.2-ea"xmlns:fx="http://javafx.com/fxml/1"><children><TabPane prefHeight="200.0" prefWidth="200.0" tabClosingPolicy="ALL_TABS" side="TOP" tabDragPolicy="REORDER"rotateGraphic="true" centerShape="true"><tabs><Tab text="Tab 1"><content><FlowPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0"orientation="VERTICAL"><Button text="Button 1"/><Button text="Button 2"/><Button text="Button 3"/><Button text="Button 4"/><Button text="Button 5"/></FlowPane></content></Tab><Tab text="Tab 2"><content><FlowPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0"orientation="HORIZONTAL"><Button text="Button 6"/><Button text="Button 7"/><Button text="Button 8"/><Button text="Button 9"/><Button text="Button 10"/></FlowPane></content></Tab></tabs></TabPane></children>
</StackPane>