代码之家  ›  专栏  ›  技术社区  ›  Java Noob

Javafx文本区域禁用滚动条

  •  3
  • Java Noob  · 技术社区  · 7 年前

    目前,我正在为一种编程语言制作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>
    

    我已经指定了要隐藏滚动条的滚动条。有人对此有什么见解吗?谢谢

    3 回复  |  直到 7 年前
        1
  •  4
  •   Adam    7 年前

    这可以通过在内部滚动窗格上设置水平+垂直滚动条策略来实现。我目前不确定这是否可以通过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
  •  0
  •   Ala Eddine Menai    7 年前

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

    通过使用Css: 在这个CSS中,我想 error id 属于 TextArea

    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
  •  0
  •   Alastair Cooper    5 年前

    为了防止在整个项目中重复指向css的链接,我扩展了TextArea以创建一个非常简单的“垂直滚动”纯TextArea对象。我将css附加到对象本身。垂直滚动条即使在不必要的时候也是可见的,因为我喜欢这样。要仅在需要时显示,可以在对vbar策略的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;
    }
    
    

    希望这有帮助