java - 将按钮添加到选项卡和选项卡区域 JavaFX

标签 java css button javafx java-8

我正在寻找一种将Button 添加到JavaFX Tab 的方法。

在互联网上搜索它,但找不到任何解决方案。

类似于下面屏幕截图中的按钮。

有人可以帮我吗?

enter image description here

最佳答案

要在 Tab 上添加图标 Button:

setGraphic Tab 的方法可用于添加要显示在Tab 上的任何Node。可以添加一个 Button,因为它是一个 Node

此后出现一个功能齐全的按钮,但不显示任何图标。 Button 也有 setGraphic方法相同,因此可以添加 ImageView 以在 Button 上显示 Image

要在选项卡区域的右上角控制 Button:

这些按钮可以放在 TabPane 上,而不是放在 TabPane 内。为此,您可以使用 AnchorPaneButton 锚定到右上角。

示例:

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);
    }
}

唯一剩下的就是从 Button 中删除默认背景和边框。这可以通过将以下 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;
}

结果:

                     /image/olclI.png

关于java - 将按钮添加到选项卡和选项卡区域 JavaFX,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37721760/

相关文章:

java - 从域对象中访问 Spring 单例的好方法?

java - PosixFilePermission 在 java 中导致 UnsupportedOperationException

java - 如何调整 JavaFX 3D 子场景的大小?

html - 如何使图像上的文本响应特定位置?

IOS 7 按钮背景不起作用

reactjs - 在下拉列表中设置重置按钮

java8 "java.lang.OutOfMemoryError: Metaspace"

html - 单独处理按钮 :focus 的按钮行

jquery - 如何在 minDate jquery 中排除星期日

java - 如何为 future 的动态 Activity 留出空间?