【问题标题】:How to get JavaFX nodes (textarea, textfield) to resize correctly when user drags to resize the stage window?当用户拖动以调整舞台窗口大小时,如何让 JavaFX 节点(文本区域、文本字段)正确调整大小?
【发布时间】:2013-11-30 19:53:14
【问题描述】:

当用户拖动以调整舞台窗口大小时,如何让 JavaFX 节点(textarea、textfield)正确调整大小?

我有一段代码可以创建一个带有两个节点(TextArea、TextField)的舞台 VBox。但是,当用户拖动调整窗口大小时,这些组件并没有按比例拖动。请看图片:

这是我的代码,关于如何实施修复以使文本字段始终位于底部并且文本区域扩展以填充空白区域的任何建议?谢谢!

Stage stage = new Stage();  
VBox root = new VBox();
textArea = new TextArea();
textField = new TextField();
root.getChildren().addAll(textArea, textField);
textArea.setStyle("-fx-background-color: DARKGRAY;"
    + "-fx-text-fill: BLACK;"
    + "-fx-font-size: 14pt;");
textArea.setPrefSize(400, 316);
textArea.setEditable(false);
textArea.setWrapText(true);
textField.setStyle("-fx-background-color: DARKGRAY;"
    + "-fx-text-fill: BLACK;"
    + "-fx-font-size: 14pt;");

【问题讨论】:

    标签: java javafx


    【解决方案1】:

    使用VBox,组件将在垂直方向上占用足够的空间以适应。之后,增加Stage 的大小并没有什么不同。

    使用BorderPane。如果你使用过 Swing,这就像BorderLayout。这将允许您将组件放置在Stage 的边界和中心,并且这些组件即使在调整大小后也将保留在它们所在的位置。

    SSCCE:

    package stack;
    
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.control.TextArea;
    import javafx.scene.control.TextField;
    import javafx.scene.layout.BorderPane;
    import javafx.stage.Stage;
    
    public class TextfieldAdjust extends Application {
    
        Scene scene;
        TextArea area;
        TextField field;
        BorderPane border;
    
        @Override
        public void start(Stage stage) throws Exception {
            border = new BorderPane();
            scene = new Scene(border);
    
            area = new TextArea();
            field = new TextField();
    
            area.setStyle("-fx-background-color: DARKGRAY;"
                    + "-fx-text-fill: BLACK;"
                    + "-fx-font-size: 14pt;");
            field.setStyle("-fx-background-color: WHEAT;"
                    + "-fx-text-fill: BLACK;"
                    + "-fx-font-size: 14pt;");
    
            border.setCenter(area);
            border.setBottom(field);
    
            stage.setScene(scene);
            stage.sizeToScene();
            stage.show();
        }
        public static void main(String[] args) {
            Application.launch("stack.TextFieldAdjust");
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-29
      • 2015-06-01
      • 2011-02-28
      • 1970-01-01
      • 1970-01-01
      • 2013-03-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多