Javafx滚动条位置

问题描述 投票:0回答:1

我在ScrollPane中有一个TextArea。我已经设置了TextArea nodeOrientation =“RIGHT_TO_LEFT”。当我这样做时,滚动条会出现在窗口的左侧而不是右侧,我认为这是默认的。如何强制滚动条到窗口的右侧?

我已经尝试将我的AnchorPane和ScrollPane的nodeOrientation设置为从左到右,但这没有帮助

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

<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="200.0" prefWidth="400.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.mycompany.mycalc.MyCalcHistoryController">
   <children>
      <ScrollPane fx:id="scrollPane" fitToHeight="true" fitToWidth="true" prefHeight="242.0" prefWidth="400.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
         <content>
            <TextArea fx:id="txtHistory" editable="false" nodeOrientation="RIGHT_TO_LEFT" />
         </content>
      </ScrollPane>
   </children>
</AnchorPane>
javafx scrollpane
1个回答
0
投票

我使用BorderPane而不是AnchorPane并且没有使用fxml文件。这是我的代码,代码是屏幕截图。如您所见,仅为TextArea设置节点方向不会改变ScrollPane上滚动条的位置。

import javafx.application.Application;
import javafx.geometry.NodeOrientation;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextArea;
import javafx.scene.control.ScrollPane.ScrollBarPolicy;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class FxTest00 extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        TextArea txtAr = new TextArea("myxomatosis");
        txtAr.setNodeOrientation(NodeOrientation.RIGHT_TO_LEFT);
        ScrollPane scrollPane = new ScrollPane(txtAr);
        scrollPane.setVbarPolicy(ScrollBarPolicy.ALWAYS);
        BorderPane root = new BorderPane();
        root.setCenter(scrollPane);
        Scene scene = new Scene(root);
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

FxTst00

编辑(在Ray_White对我的回答的初步评论之后)

您所指的滚动条(在您的评论中)是TextArea的一部分,与ScrollPane父级无关。在下面的代码中,TextArea直接添加到BorderPane,即下面的代码不包含ScrollPane

我发现删除TextArea垂直滚动条的方式是通过TextArea风格。我创建了一个名为scrolbar.css的CSS文件。这是它的内容:

.text-area .scroll-pane {
    -fx-vbar-policy: never;
}

这里是修改后的代码,包括你建议将文本添加到TextArea以强制滚动。

import javafx.application.Application;
import javafx.geometry.NodeOrientation;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class FxTest00 extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        TextArea txtAr = new TextArea("myxomatosis");
        txtAr.appendText("\n");
        txtAr.appendText("One");
        txtAr.appendText("\n");
        txtAr.appendText("Two");
        txtAr.appendText("\n");
        txtAr.appendText("Three");
        txtAr.appendText("\n");
        txtAr.appendText("Four");
        txtAr.appendText("\n");
        txtAr.appendText("Five");
        txtAr.appendText("\n");
        txtAr.appendText("Six");
        txtAr.appendText("\n");
        txtAr.appendText("Seven");
        txtAr.appendText("\n");
        txtAr.appendText("Eight");
        txtAr.appendText("\n");
        txtAr.appendText("Nine");
        txtAr.appendText("\n");
        txtAr.appendText("Ten");
        txtAr.appendText("\n");
        txtAr.appendText("Eleven");
        txtAr.setNodeOrientation(NodeOrientation.RIGHT_TO_LEFT);
        BorderPane root = new BorderPane();
        root.setCenter(txtAr);
        Scene scene = new Scene(root);
        scene.getStylesheets().add(getClass().getResource("scrolbar.css").toString());
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

这是修改后的GUI的屏幕截图。如您所见,没有滚动条。

No scrollbar

© www.soinside.com 2019 - 2024. All rights reserved.