我正在测试JavaFX ScrollPane类,并意识到它没有按照我的预期工作,我不知道为什么。我有以下代码:
public class Client3 extends Application {
int indexMsg = 0;
Button send;
GridPane root;
ScrollPane msgPane;
GridPane msgPaneContent;
FlowPane writePane;
TextField writeMsg;
Scene scene;
@Override
public void start(Stage primaryStage) {
root = new GridPane();
root.setAlignment(Pos.CENTER);
root.setVgap(10);
root.setPadding(new Insets(10, 10, 10, 10));
msgPane = new ScrollPane();
msgPane.setPrefSize(280, 280);
msgPane.setHbarPolicy(ScrollPane.ScrollBarPolicy.NEVER);
msgPaneContent = new GridPane();
msgPaneContent.setPrefWidth(270);
msgPaneContent.setVgap(10);
writePane = new FlowPane(10, 10);
writePane.setAlignment(Pos.CENTER);
writePane.setPrefWidth(280);
writeMsg = new TextField();
writeMsg.setPrefWidth(150);
writeMsg.setPromptText("Write your message");
writePane.getChildren().add(writeMsg);
GridPane.setConstraints(msgPane, 0, 0);
GridPane.setConstraints(writePane, 0, 1);
msgPane.setContent(msgPaneContent);
root.getChildren().addAll(msgPane, writePane);
writeMsg.setOnAction((ev) -> {
if (!writeMsg.getText().isEmpty()) {
TextArea msg = new TextArea(writeMsg.getText());
msg.setMaxWidth(135);
msg.setPrefRowCount(msg.getLength() / 21 + 1);
msg.setWrapText(true);
GridPane.setConstraints(msg, 0, indexMsg);
indexMsg++;
writeMsg.deleteText(0, writeMsg.getText().length());
msgPaneContent.getChildren().add(msg);
msgPane.setVvalue(1.0);
}
});
scene = new Scene(root, 300, 300);
primaryStage.setTitle("Chat App");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
基本上,我有一个GridPane
作为根,ScrollPane
和GridPane
作为其子项。 ScrollPane
有一个孩子GridPane
。 TextField
EventHandler
TextArea
会在GridPane
(ScrollPane
的子项内部生成TextArea
。每个setVvalue(1.0)
对象都是在垂直方向向下创建的。我想在每次添加新的TextArea
时始终将滚动条设置为其最大值(a = ["a","b","c"]
b = [1,2,3]
c = [4,5,6]
new_dict = {i:[j, k] for i, j, k in zip(a, b, c)}
)。事情是它似乎没有工作,因为垂直值在处理事件后从未设置为最大值,但似乎设置为处理它之前的最大值(底部的之前的TextArea已添加)。
任何解决方案?提前谢谢。