11

我想使用类似于带有 JavaFX 的 Firefox 配置面板的图标创建选项卡面板:

在此处输入图像描述

有没有我可以用来查看如何实现的示例?

4

3 回答 3

17

选项卡与 JavaFX 中的许多其他元素一样,有一个名为 的方法setGraphic(Node value),您可以在其中放置任何 JavaFX 节点。例子:

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;

public class TabPaneTest extends Application {
  public static void main(String[] args) {
    Application.launch(args);
  }
  @Override
  public void start(Stage primaryStage) {
    primaryStage.setTitle("Tabs");
    Group root = new Group();
    Scene scene = new Scene(root, 400, 250, Color.WHITE);
    TabPane tabPane = new TabPane();
    BorderPane borderPane = new BorderPane();
    for (int i = 0; i < 5; i++) {
      Tab tab = new Tab();
      tab.setGraphic(new Circle(0, 0, 10));
      HBox hbox = new HBox();
      hbox.getChildren().add(new Label("Tab" + i));
      hbox.setAlignment(Pos.CENTER);
      tab.setContent(hbox);
      tabPane.getTabs().add(tab);
    }
    // bind to take available space
    borderPane.prefHeightProperty().bind(scene.heightProperty());
    borderPane.prefWidthProperty().bind(scene.widthProperty());

    borderPane.setCenter(tabPane);
    root.getChildren().add(borderPane);
    primaryStage.setScene(scene);
    primaryStage.show();
  }
}

结果:

TabPane 中的选项卡

于 2013-06-10T07:42:56.967 回答
4

我知道它是一个旧线程,但我没有在任何地方找到直接答案。所以我想把它发布一些,这对一些搜索它会有帮助。

这就是我为获得像 Firefox 首选项屏幕这样的选项卡所做的工作。

使用 setGraphics 将图像添加到选项卡,并将以下代码添加到应用程序 css 文件中。我的图像尺寸是 48x48。所以我选择了70的身高。

.tab-label {
    -fx-content-display: top;
}
.tab-pane {
    -fx-tab-min-height: 70;
    -fx-tab-max-height: 70;
}
于 2014-02-19T14:27:31.543 回答
0

如何直接从图片网址添加图片:

          Tab tab = new Tab();
          tab.setGraphic(buildImage("patch/to/image");

    // Helper method to create image from image patch
    private static ImageView buildImage(String imgPatch) {
            Image i = new Image(imgPatch);
            ImageView imageView = new ImageView();
            //You can set width and height
            imageView.setFitHeight(16);
            imageView.setFitWidth(16);
            imageView.setImage(i);
            return imageView;
        }
于 2016-02-16T15:18:42.320 回答