DVa*_*rga 20
要在Buttons上标记Tabs:
该setGraphic方法Tab可用于添加任何Node要显示的内容Tab.A Button可以添加,因为它是Node.
在此之后,存在一个功能齐全的按钮,但它不显示任何图标.Button也有setGraphic相同的方法,因此ImageView可以添加一个显示在Image上面的方法Button.
要Button在选项卡区域的右上角进行控制:
这些按钮可以放置在TabPane,而不是内部的TabPane.为此,您可以使用an AnchorPane来将Buttons 锚定到右上角.
例:
public class ButtonedTabPane extends Application {
@Override
public void start(Stage primaryStage) {
BorderPane root = new BorderPane();
TabPane tabPane = new TabPane();
tabPane.setTabClosingPolicy(TabPane.TabClosingPolicy.UNAVAILABLE);
// HBox of control buttons
HBox hbox = new HBox();
hbox.getChildren().addAll(createTabButton("min.png"), createTabButton("max.png"));
// Anchor the controls
AnchorPane anchor = new AnchorPane();
anchor.getChildren().addAll(tabPane, hbox);
AnchorPane.setTopAnchor(hbox, 3.0);
AnchorPane.setRightAnchor(hbox, 5.0);
AnchorPane.setTopAnchor(tabPane, 1.0);
AnchorPane.setRightAnchor(tabPane, 1.0);
AnchorPane.setLeftAnchor(tabPane, 1.0);
AnchorPane.setBottomAnchor(tabPane, 1.0);
// Create some tabs
Tab tab = new Tab("Files");
tab.setGraphic(createTabButton("files.png"));
((Button) tab.getGraphic()).setOnAction(e -> System.out.println("I'll show the list of files!"));
tabPane.getTabs().add(tab);
tab = new Tab("Network");
tab.setGraphic(createTabButton("network.png"));
((Button) tab.getGraphic()).setOnAction(e -> System.out.println("I'll show the network!"));
tabPane.getTabs().add(tab);
root.setCenter(anchor);
Scene scene = new Scene(root, 400, 400);
scene.getStylesheets().add(getClass().getResource("style.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
}
private Button createTabButton(String iconName) {
Button button = new Button();
ImageView imageView = new ImageView(new Image(getClass().getResource(iconName).toExternalForm(),
16, 16, false, true));
button.setGraphic(imageView);
button.getStyleClass().add("tab-button");
return button;
}
public static void main(String[] args) {
launch(args);
}
}
Run Code Online (Sandbox Code Playgroud)
唯一剩下的就是从Buttons中删除默认背景和边框.这可以通过将以下CSS选择器插入CSS样式表来完成.
style.css文件
.tab-button {
-fx-border-width: 0;
-fx-background-radius: 0;
-fx-background-color: transparent;
-fx-content-display: graphic-only;
}
.tab-button:hover {
-fx-background-color: white;
}
Run Code Online (Sandbox Code Playgroud)
结果:

| 归档时间: |
|
| 查看次数: |
5961 次 |
| 最近记录: |