【问题标题】:Javafx Text area disable scroll barJavafx 文本区域禁用滚动条
【发布时间】:2018-05-26 22:23:05
【问题描述】:

目前,我正在为一种编程语言制作一个 lw 解释器。 http://www.muppetlabs.com/~breadbox/bf/ 无论如何,我正在尝试实现一个文本区域,但我希望隐藏滚动条。我已经查看了多个教程来做到这一点,但它们似乎都不起作用。这是我的 fxml 窗口的代码

<?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>

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

【问题讨论】:

    标签: java javafx scroll textarea javafx-8


    【解决方案1】:

    这可以通过在内部 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 中找到有关如何执行此操作的信息。

    【讨论】:

    【解决方案2】:

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

    通过在这个 CSS 中使用 Css : 我想 erroridTextArea

    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"/>
    

    【讨论】:

      【解决方案3】:

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

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

      
      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;
      }
      
      

      希望对你有帮助

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-08-31
        • 1970-01-01
        • 1970-01-01
        • 2018-06-21
        • 2011-09-05
        相关资源
        最近更新 更多