如何使JavaFX节点(textarea,textfield)在用户拖动以调整舞台窗口大小时正确调整大小?
我有一段代码,它创建了一个带有两个节点(TextArea,TextField)的stage TextArea。但是,当用户拖动以调整窗口大小时,这些组件不会按比例拖动。请看图片:
这是我的代码,有关于如何实现修复的建议,这样textfield总是在底部,textarea展开来填充空白吗?谢谢!
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;");
发布于 2013-12-01 04:55:46
使用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");
}
}
https://stackoverflow.com/questions/20304933
复制相似问题