Javafx 文本区域禁用滚动条

Javafx Text area disable scroll bar

目前,我正在为一种编程语言制作 lw 解释器。 http://www.muppetlabs.com/~breadbox/bf/ 无论如何,我正在尝试实现一个文本区域,但我希望隐藏滚动条。我查看了多个教程来执行此操作,但其中 none 似乎有效。这是我的 fxml window

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

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

<SplitPane dividerPositions="0.6231155778894473" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" orientation="VERTICAL" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="interpreter.Window">
  <items>
    <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0">
         <children>
            <HBox layoutX="188.0" layoutY="81.0" prefHeight="262.0" prefWidth="599.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="-1.0" AnchorPane.topAnchor="0.0">
               <children>
                  <Pane prefHeight="200.0" prefWidth="200.0">
                     <children>
                        <Button fx:id="executor" layoutX="64.0" layoutY="64.0" mnemonicParsing="false" onAction="#onExecute" text="Execute" />
                        <TextField fx:id="input" layoutX="15.0" layoutY="105.0" promptText="input" />
                        <ComboBox fx:id="byteSize" layoutX="23.0" layoutY="153.0" prefWidth="150.0" promptText="Byte Size" />
                        <TextField fx:id="bytes" alignment="CENTER_RIGHT" layoutX="25.0" layoutY="205.0" prefHeight="27.0" prefWidth="91.0" promptText="Bytes" text="30000" />
                        <Text layoutX="39.0" layoutY="41.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Control Center" textAlignment="CENTER">
                           <font>
                              <Font size="17.0" />
                           </font>
                        </Text>
                        <Text layoutX="122.0" layoutY="222.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Bytes" />
                        <Line endX="100.0" layoutX="98.0" layoutY="261.0" startX="-100.0" />
                     </children>
                  </Pane>



                    <TextArea fx:id="errorLog" disable="true" editable="false" prefHeight="262.0" prefWidth="38.0" style="-fx-text-fill: #ff0000" />
                     //this is the one where I want the scroll bars hidden



                  <TextArea fx:id="code" prefHeight="262.0" prefWidth="361.0" promptText="Code" HBox.hgrow="ALWAYS" />
               </children>
            </HBox>
         </children>
      </AnchorPane>
      <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0">
         <children>
             <TextArea fx:id="output" disable="true" editable="false" layoutX="76.0" layoutY="-60.0" prefHeight="166.0" prefWidth="598.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
         </children>
      </AnchorPane>
  </items>
</SplitPane>

我指定了要隐藏哪一个的滚动条。有人对此有任何见解吗?谢谢

这可以通过在内部 ScrollPane 上设置水平+垂直滚动条策略来完成。我目前不确定这是否可以通过 FXML 或 Java API 完成,但是可以通过 CSS 实现。

CSS guide on ScrollPane

#errorLog .scroll-pane {
    -fx-hbar-policy: never;
    -fx-vbar-policy: never;
}

这假定了 TextArea 的 CSS 子结构,但这在 official CSS gudie for TextArea

中是明确定义的
scroll-pane — ScrollPane
    content — Region

如果您尚未使用 CSS,则需要定义一个 CSS 文件并将其添加到场景或 FXML 中。有关如何执行此操作的信息,请参见 Styling UI Controls with CSS

有很多方法可以满足您的需求。

通过在 CSS 中使用 Css : 我假设 errorTextAreaid

Fxml :

<TextArea id="error" fx:id="errorLog" disable="true" editable="false" prefHeight="262.0" prefWidth="38.0" style="-fx-text-fill: #ff0000" />

CSS :

#error .scroll-pane {
    -fx-hbar-policy: never;
    -fx-vbar-policy: never;
}

#error .scroll-pane{
    -fx-padding:0

}

这些样式给你相同的结果。

通过使用 FXML : 我添加了 wrapText="true"

  <TextArea id="error" fx:id="errorLog" disable="true" editable="false" prefHeight="262.0" prefWidth="38.0" style="-fx-text-fill: #ff0000" wrapText="true"/>

为了防止在整个项目中重复指向 css 的链接,我扩展了 TextArea 以创建一个非常简单的 'vertical-scroll' 只有 TextArea 对象。我将 css 附加到对象本身。即使不需要,垂直滚动条也是可见的,因为我喜欢这样。要仅在需要时显示,您可以在对 vbar-policy 的 css 调用中将 'always' 更改为 'as-needed'。

我还添加了一些填充以使文本垂直居中,尽管这显然需要根据不同的字体大小进行更改


package sample.controls;

import javafx.scene.control.TextArea;

public class VertOnlyTextArea extends TextArea {

    public VertOnlyTextArea(){
        setWrapText(true);
        getStylesheets().add("/css/vertOnlyTextArea.css");
    }
}

在css目录中...vertOnlyTextArea.css


.text-area .scroll-pane {
    -fx-hbar-policy: never;
    -fx-vbar-policy: always;
}
.text-area .content{
    -fx-padding: 8 0 0 8;
}

希望这对您有所帮助