40

JavaFX 2 上是否有类似GroupBoxTitledBorder的东西?

感谢您的任何提示:-)

4

6 回答 6

49

没有这样的标准控件,但很容易创建自己的。这是一个示例实现:

/** Places content in a bordered pane with a title. */
class BorderedTitledPane extends StackPane {
  BorderedTitledPane(String titleString, Node content) {
    Label title = new Label(" " + titleString + " ");
    title.getStyleClass().add("bordered-titled-title");
    StackPane.setAlignment(title, Pos.TOP_CENTER);

    StackPane contentPane = new StackPane();
    content.getStyleClass().add("bordered-titled-content");
    contentPane.getChildren().add(content);

    getStyleClass().add("bordered-titled-border");
    getChildren().addAll(title, contentPane);
  }
}

以及随附的CSS:

.label {
  -fx-font: 28px Vivaldi;
}

.bordered-titled-title {
  -fx-background-color: white;
  -fx-translate-y: -16;
}

.bordered-titled-border {
  -fx-content-display: top;
  -fx-border-insets: 20 15 15 15;
  -fx-background-color: white;
  -fx-border-color: black;
  -fx-border-width: 2;
}

.bordered-titled-content {
  -fx-padding: 26 10 10 10;
}

该代码来自我为响应 Oracle JavaFX 论坛主题帖子“等效于 BorderFactory.createTitledBorder”而创建的示例

示例程序的输出如下所示。

葛底斯堡

于 2013-02-13T22:01:40.177 回答
45

TitledPanesetCollapsible(false). 它看起来比使用 CSS 样式更一致。这是结果

在此处输入图像描述

于 2016-06-18T04:22:43.537 回答
14

Jewelsea 答案的 FXML 版本:

TitledBorder(我将 BorderedTitledPane 重命名为 TitledBorder)

package com.example.controls;

import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;

public class TitledBorder extends StackPane 
{
    private Label titleLabel = new Label();
    private StackPane contentPane = new StackPane();
    private Node content;



    public void setContent(Node content)
    {
        content.getStyleClass().add("bordered-titled-content");
        contentPane.getChildren().add(content);
    }


    public Node getContent()
    {
        return content;
    }


    public void setTitle(String title)
    {
    titleLabel.setText(" " + title + " ");
    }


    public String getTitle()
    {
        return titleLabel.getText();
    }



    public TitledBorder() 
    {
        titleLabel.setText("default title");
        titleLabel.getStyleClass().add("bordered-titled-title");
        StackPane.setAlignment(titleLabel, Pos.TOP_CENTER);

        getStyleClass().add("bordered-titled-border");
        getChildren().addAll(titleLabel, contentPane);
      }

}

FXML 用法:

<?import com.example.controls.*?>

<TitledBorder title="title" >       
    <Label text="label with text" />        
</TitledBorder>   

不要忘记样式表!

将此 CSS 用于普通字体:

.bordered-titled-title {
  -fx-background-color: white;
  -fx-translate-y: -10; /* play around with this value when changing the title font to get a vertically centered title */
}

.bordered-titled-border {
  -fx-content-display: top;
  -fx-border-insets: 20 15 15 15;
  -fx-background-color: white;
  -fx-border-color: black;
  -fx-border-width: 2;
}

.bordered-titled-content {
  -fx-padding: 26 10 10 10;
}

使用这个 CSS,它现在看起来像这样:

在此处输入图像描述

更新: 标题长于内容时的问题:

在此处输入图像描述 有什么提示可以解决这个问题吗?

于 2013-02-14T11:50:06.810 回答
3

这是一个可以加载到具有类似功能的 SceneBuilder 的 FXML 文档:

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

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

<AnchorPane style="-fx-border-insets: 8 0 0 0; -fx-background-color: #FFFFFF; -fx-border-color: black;">
    <children>
        <Label alignment="TOP_LEFT" layoutX="14.0" style="-fx-padding: 0 5; -fx-background-color: inherit;" text="Title" />
        <AnchorPane prefHeight="200.0" prefWidth="200.0" AnchorPane.bottomAnchor="1.0" AnchorPane.leftAnchor="1.0" AnchorPane.rightAnchor="1.0" AnchorPane.topAnchor="10.0" />
    </children>
</AnchorPane>

如果您需要使标签文本/边框尺寸更大,您只需要编辑子 AnchorPane 的 CSS 和 topAnchor 以及父 AnchorPane 的 -fx-border-insets 的第一个参数。

于 2016-08-19T17:33:28.383 回答
0

GroupBox - 据我所知,这是通常的组布局。

TitledBorder - 看起来像 TitledPane(通常是 Accordion 的一个组件,但也可以是单独存在的控件)。

JavaFX-2 类似物看起来与您的不同(但不显着),并且像往常一样,您可以使用不同的控件外观更改方式:css、控件的皮肤替换等

于 2013-02-13T19:05:42.880 回答
0

这是一个基于 TitledPane 的 GroupBox 实现。它提供了三种方法来设置 GroupBox 的标题、内容和内容填充。

public final class GroupBox extends Parent {

    private StackPane _stackPane;
    private TitledPane _titledPane;

    public GroupBox() {
        _stackPane = new StackPane();
        _titledPane = new TitledPane();
        setContentPadding(new Insets(10));
        _titledPane.setCollapsible(false);
        _titledPane.setContent(_stackPane);
        super.getChildren().add(_titledPane);
    }

    public GroupBox(String title, Node content) {
        this();
        setText(title);
        setContent(content);
    }

    public GroupBox(String title, Node content, Insets contentPadding) {
        this(title, content);
        setContentPadding(contentPadding);
    }

    public void setText(String value) {
        _titledPane.setText(value);
    }

    public void setContent(Node node) {
        _stackPane.getChildren().add(node);
    }

    public void setContentPadding(Insets value) {
        _stackPane.setPadding(value);
    }
}
于 2017-01-12T04:37:15.380 回答