我在TabPane中有3個選項卡,每個選項卡都有一個帶有不同文本和不同長度的文本區域。 我想根據每個標籤中的長度來自動調整文本區域的大小。 我不明白我該怎麼辦?使用場景生成器? css?javaFX方法? 感謝您的提前...在javaFX中將文本區域自動調整爲tabpane
0
A
回答
4
我認爲您要求文本區域根據顯示的文本增長或縮小?
如果是這樣,看看這個代碼可以幫助:
import java.util.concurrent.Callable;
import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class AutosizingTextArea extends Application {
@Override
public void start(Stage primaryStage) {
TextArea textArea = new TextArea();
textArea.setMinHeight(24);
textArea.setWrapText(true);
VBox root = new VBox(textArea);
Scene scene = new Scene(root, 600, 400);
primaryStage.setScene(scene);
primaryStage.show();
// This code can only be executed after the window is shown:
// Perform a lookup for an element with a css class of "text"
// This will give the Node that actually renders the text inside the
// TextArea
Node text = textArea.lookup(".text");
// Bind the preferred height of the text area to the actual height of the text
// This will make the text area the height of the text, plus some padding
// of 20 pixels, as long as that height is between the text area's minHeight
// and maxHeight. The minHeight we set to 24 pixels, the max height will be
// the height of its parent (usually).
textArea.prefHeightProperty().bind(Bindings.createDoubleBinding(new Callable<Double>(){
@Override
public Double call() throws Exception {
return text.getBoundsInLocal().getHeight();
}
}, text.boundsInLocalProperty()).add(20));
}
public static void main(String[] args) {
launch(args);
}
}
如果你想使這個可重複使用的,那麼你可以考慮繼承TextArea
。 (一般來說,我不喜歡繼承控制類。)這裏最棘手的部分是執行代碼,一旦將它添加到實況場景圖形中(這對於查找工作而言是必需的),便可以擴展TextArea
。一種方法可以做到這一點(這是一個黑客,imho)是使用AnimationTimer
做查找,一旦查找成功,您可以停止查找。我嘲笑了這個here。
相關問題
- 1. 將FlowPane自動調整爲TabPane
- 2. 自動文本區域調整大小
- 3. 星火文本區域或富文本自動調整大小
- 4. wysihtml5文本區域調整
- 5. 如何刪除javaFx中TabPane周圍的區域?
- 6. 如何根據區域大小動態調整javafx中的svg
- 7. 在JavaFX中使用TabPane
- 8. 將Tab添加到TabPane JavaFX
- 9. Javafx拖放TabPane
- 10. Selenium WebDriver調整文本區域大小
- 11. 調整大小jHtml文本區域
- 12. JavaFx文本大小調整
- 13. 通過api調用自動調整文本區域的值(純javascript)
- 14. JavaFX - 動態關閉TabPane中的Tab鍵
- 15. 在qml中自動調整文本
- 16. 調整大小文本區域以及股利 - 但作爲股利調整爲
- 17. 在自定義UIView裏面自動調整UITableView的區域
- 18. JavaFX的2維TabPane
- 19. 如何自動將文本加載到文本區域
- 20. JavaFX的文本區域 - colums /行
- 21. 重置文本區域長度JavaFX
- 22. 充分利用JavaFX的文本區域
- 23. Javafx文本區域用戶輸入
- 24. javafx將多邊形轉換爲區域
- 25. 停止在Safari中調整大小的文本區域
- 26. 在html中調整文本的選擇區域
- 27. JavaFX 8 TabPane - Tab鍵不斷滾動出TabPane標題的視口
- 28. 如何動態調整spotfire文本區域的大小
- 29. 將文本區寬度調整爲最長的文本行
- 30. Silverlight - 自動調整文本框的大小以填滿空白區域
這裏是一個示例: https://stackoverflow.com/questions/44141172/javafx-expandable-textfield-sample – kamirru