2015-04-05 88 views
0

我正在嘗試學習JavaFX。要做到這一點,我一直在試圖製作一個包含多行文本框支持的文本編輯器,以及語法突出顯示的可能性。JavaFX ScrollPane不填充它的父窗格

目前,我一直面臨的最大問題是,滾動窗格我一直在封裝我所有的FlowPanes中不會調整根據它在窗格的大小。我一直在研究這個問題有關現在只有半個星期,並且根本無法讓ScrollPane填充它所在的窗口。下面的代碼顯示了一個JavaFX階段,它具有可用的鍵盤輸入,而且無論如何,ScrollPane的大小始終相同。感謝所有提前!

這裏是我公司主營:

import javafx.application.Application; 
import javafx.scene.Scene; 
import javafx.stage.Stage; 

public class Launcher extends Application { 
    public static void main(String[] args) { 
     launch(args); 
    } 


    @Override 
    public void start(Stage primaryStage) throws Exception { 
     primaryStage.setScene(new Scene(new DynamicTextBox(),500,500)); 
     primaryStage.show(); 
    } 
} 

TextBox類:

import javafx.beans.value.ChangeListener; 
import javafx.beans.value.ObservableValue; 
import javafx.event.EventHandler; 
import javafx.geometry.Bounds; 
import javafx.geometry.Orientation; 
import javafx.scene.control.ScrollPane; 
import javafx.scene.input.KeyEvent; 
import javafx.scene.layout.FlowPane; 
import javafx.scene.layout.Pane; 
import javafx.scene.text.Text; 

public class DynamicTextBox extends Pane { 
    //currentLinePane is made to handle all the direct user inputs 
    //multiLinePane, while not really used yet will create a new line when     the enter key is struck. 
    private FlowPane currentLinePane, multiLinePane; 
    private ScrollPane editorScroller; 

    public DynamicTextBox() { 
     super(); 

     currentLinePane = new FlowPane(Orientation.HORIZONTAL); 
     multiLinePane = new FlowPane(Orientation.VERTICAL); 
     multiLinePane.getChildren().add(currentLinePane); 

     editorScroller = new ScrollPane(multiLinePane); 
     editorScroller.setVbarPolicy(ScrollPane.ScrollBarPolicy.AS_NEEDED); 
     editorScroller.setHbarPolicy(ScrollPane.ScrollBarPolicy.NEVER); 
     editorScroller.setOnKeyPressed(new EventHandler<KeyEvent>() { 
      @Override 
      public void handle(KeyEvent event) { 
       configureInput(event); 
      } 
     }); 

     super.getChildren().add(editorScroller); 
     editorScroller.requestFocus(); 
    } 

    private void configureInput(KeyEvent event) { 
     currentLinePane.getChildren().add(new Text(event.getText())); 
    } 
} 

回答

0

您使用

ScrollPane.ScrollBarPolicy.AS_NEEDED

其中,根據在甲骨文的文檔「,表示需要時應顯示滾動條「。相反,使用

ScrollPane.ScrollBarPolicy.ALWAYS

另外,召回這些是常數。您可以使用boundsInParent得到家長的高度:https://docs.oracle.com/javafx/2/api/javafx/scene/Node.html#boundsInParentProperty

或者,你可以使用getParent()獲取父,然後使用computeMinWidth()https://docs.oracle.com/javafx/2/api/javafx/scene/Node.html#getParent()

+0

嗯得到它的高度,以及滾動條政策我是故意和作品非常好,這就是我想要的。當我運行這個時,我沒有理由找到它爲什麼選擇這個隨機大小。我對這個轉換到javafx非常鹹,至今我已經知道swing了,雖然swing有很多怪癖,但我很確定它只是根據窗口大小調整FlowPane – 2015-04-06 01:12:16

+0

其實我可能剛剛發現了一些好東西,我的FlowPanes可能是罪魁禍首,因爲它們的默認包裝長度顯然是400 https://docs.oracle.com/javafx/2/api/javafx/scene/layout/FlowPane.html – 2015-04-06 01:16:07