将图标放入 tabpane javaFX 的右上角

Place icon into top right of tabpane javaFX

我正在使用 JavaFX 8 并有一个选项卡窗格:

我想知道是否有任何方法可以将图标放置在选项卡窗格的右上角,例如这个

我的目标是在选项卡窗格的右上角有一个彩色指示器,我可以在绿色之间切换

并根据需要设置为红色。有什么方法可以在选项卡窗格的右上角添加彩色圆圈或类似的东西吗?

您可以使用以下节点结构来实现您想要的。

-AnchorPane
    -TabPane
    -StackPane

想法

选项卡窗格将完全占据锚窗格。堆栈窗格将锚定到锚定窗格的右上角,并且位于选项卡窗格的顶部。堆栈窗格的大小将被调整,因此它在右上角显示为一个图标。最后,圆圈将被添加到堆栈窗格中。


FXML

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane prefHeight="400.0" prefWidth="600.0" 
            xmlns="http://javafx.com/javafx/11.0.2" 
            xmlns:fx="http://javafx.com/fxml/1"
            fx:controller="com.example.tabpane.TabPaneController">

    <TabPane prefHeight="200.0" prefWidth="200.0" tabClosingPolicy="UNAVAILABLE" 
             AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" 
             AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">

             <!--tabs here-->

    </TabPane>

    <StackPane fx:id="icon" prefHeight="30.0" prefWidth="30.0" 
               AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0"/>
</AnchorPane>

控制器

package com.example.tabpane;

import javafx.fxml.FXML;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;

public class TabPaneController
{
    @FXML
    private StackPane icon;

    @FXML
    public void initialize()
    {
        Circle circle = new Circle(7, Color.RED);
        icon.getChildren().add(circle);
    }
}

输出


这甚至在调整应用程序大小时直到应用程序window太小时都有效。如果这是实现您想要的目标的最佳方式,我不知道。如果您遇到任何问题,请发表评论。