如何使Label的高度与JavaFX中的字体大小相同?

如何解决如何使Label的高度与JavaFX中的字体大小相同?

我将上部标签的字体大小设置为24px。 我不知道在哪里可以检查元素的实际高度(如果在“场景”构建器中可能的话),但是框的上部(在图像中指定)和下部有多余的空间。

  • 行距为0
  • 父VBox没有顶部填充。
  • 所有标签尺寸均已计算。

enter image description here

我需要标签的高度等于字体大小。 我的意思是FontSize = Gray space surrounded by blue markers height

如何到达?

我也遇到过类似的HTML / CSS问题。 在大多数情况下,不是一个简单但可行的解决方案是使用负边距以及:before:after伪元素。

现在有针对JavaFX的解决方案吗?

再现

? Repository

通过Scene Builder打开resources/static/TasksOverview.fxml

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

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.CheckBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>


<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="640.0" prefWidth="320.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <HBox alignment="CENTER_LEFT">
         <children>
            <CheckBox mnemonicParsing="false" />
            <VBox>
               <children>
                  <Label style="-fx-font-size: 24px;" text="Wake up">
                     <VBox.margin>
                        <Insets />
                     </VBox.margin>
                  </Label>
                  <Label style="-fx-font-size: 14px;" text="... and fly. I need more text to test multiline mode." wrapText="true" />
               </children>
               <HBox.margin>
                  <Insets left="12.0" />
               </HBox.margin>
            </VBox>
         </children>
         <padding>
            <Insets left="12.0" right="12.0" />
         </padding>
      </HBox>
      <HBox alignment="CENTER_LEFT" layoutX="10.0" layoutY="10.0">
         <children>
            <CheckBox mnemonicParsing="false" />
            <VBox>
               <children>
                  <Label style="-fx-font-size: 24px;" text="Wash face">
                     <VBox.margin>
                        <Insets />
                     </VBox.margin>
                  </Label>
                  <Label style="-fx-font-size: 14px;" text="... with cold water." wrapText="true" />
               </children>
               <HBox.margin>
                  <Insets left="12.0" />
               </HBox.margin>
            </VBox>
         </children>
         <padding>
            <Insets left="12.0" right="12.0" />
         </padding>
      </HBox>
   </children>
</VBox>

解决方法

标签是“智能”控件,在“皮肤”代码中包括诸如某些内置填充之类的东西,以及用于文本和图形的各种对齐方式和间距选项,并支持通过动态隐藏文本来调整大小。 / p>

如果要对文本布局的方式进行更多控制,则可以使用原始Text节点而不是Labels,然后根据需要自己进行一些其他布局。当然,您将因此失去很多内置功能(请当心……),但有时这可能正是您想要的。

例如,具有以下属性的“文本”节点:

  1. 字体大小为24。
  2. 文本的可视区域的顶部,左侧或右侧没有间距。
  3. 放置在VBox中时,文字的可视区域下方的边距为3个像素。

可以在FXML中定义如下:

<Text boundsType="VISUAL" text="Visual bounds != Logical bounds">
    <font>
        <Font size="24.0" />
    </font>
    <VBox.margin>
        <Insets bottom="3.0" />
    </VBox.margin>
</Text>

如果您在大量文本上使用此内容,则可以在样式表中设置许多值,而不是FXML(最好使用)。除了要说有一个文本未公开的-fx-bounds-type css属性之外,我这里不会描述如何做。

有时候,比起使用Text节点,通过使用负数插入(通常是我的首选选项)之类的东西来“砍”类似于Matt答案的Label可能更好。

小心,随后进行深奥的讨论,准备忽略它;-)

一个关键部分是将boundsType设置为VISUAL。默认情况下,文本的边界通常比您实际看到的要大一些,这可以简化对齐问题,因为这意味着字母“ a”和字母“ A”的边界相同。这种界限是默认界限,称为逻辑界限。 VISUAL边界使这两个字母的边界不同,因此“ a”的边界小于“ A”的边界,这使材料不会占用超出绝对需要的空间。

例如,使用带有VISUAL边界的Text而不是Label,可以消除出现在示例屏幕截图上的文本周围的两组多余空白。 Label控件的内部布局算法分配了一组空白,而LOGICAL边界分配了另一组空白。

通过应用适当的CSS样式表,可以在标签内将边界设置为VISUAL,但是,如果这样做,标签仍然不会缩小以仅适合VISUAL边界,因为它似乎被编码为基于VISUAL逻辑界限,至少那是我尝试进行的一些快速测试所显示的。因此,在标签中的文本中使用VISUAL边界似乎只会使其显示不正确(某些人甚至会将其归类为bug)。如果您在标签内设置了带有Text的图形,这可能会起作用,但是如果这样做,则标签对您没有多大帮助,也可以直接使用Text节点(除非您使用的是TitledPane之类的控件,标题中包含标签,在这种情况下,您必须使用标签。

,

只需设置一个负值插图,它将缩小标签上方的空间,例如:

<Insets top="-5.0"/>

完整的FXML

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

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.CheckBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>


<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="640.0" prefWidth="320.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1">
    <children>
        <HBox alignment="CENTER_LEFT">
            <children>
                <CheckBox mnemonicParsing="false" />
                <VBox>
                    <children>
                        <Label style="-fx-font-size: 24px;" text="Wake up">
                            <VBox.margin>
                                <Insets top="-5.0"/>
                            </VBox.margin>
                        </Label>
                        <Label style="-fx-font-size: 14px;" text="... and fly. I need more text to test multiline mode." wrapText="true" />
                    </children>
                    <HBox.margin>
                        <Insets left="12.0" />
                    </HBox.margin>
                </VBox>
            </children>
            <padding>
                <Insets left="12.0" right="12.0" />
            </padding>
        </HBox>
        <HBox alignment="CENTER_LEFT" layoutX="10.0" layoutY="10.0">
            <children>
                <CheckBox mnemonicParsing="false" />
                <VBox>
                    <children>
                        <Label style="-fx-font-size: 24px;" text="Wash face">
                            <VBox.margin>
                                <Insets />
                            </VBox.margin>
                        </Label>
                        <Label style="-fx-font-size: 14px;" text="... with cold water." wrapText="true" />
                    </children>
                    <HBox.margin>
                        <Insets left="12.0" />
                    </HBox.margin>
                </VBox>
            </children>
            <padding>
                <Insets left="12.0" right="12.0" />
            </padding>
        </HBox>
    </children>
</VBox>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-