我做了一个简单的java应用程序。基本上,当用户单击通知按钮时,我希望在文本区域内显示一条消息。现在该消息显示在文本区域下方,我不知道如何更改它。这是到目前为止我的代码。
public void start(Stage primaryStage) {
GridPane root = new GridPane();
root.setAlignment(Pos.CENTER);
root.setHgap(10);
root.setVgap(10);
Label label = new Label();
Button btn = new Button();
btn.setText("Notify");
btn.setOnAction((ActionEvent event) -> {
label.setText("You have been notified");
});
Button btn2 = new Button();
btn2.setText("Clear");
btn2.setOnAction((ActionEvent event) -> {
label.setText("");
});
TextField textField = new TextField();
root.add(btn,1,0);
root.add(label,0,1);
root.add(btn2,1,1);
root.add(textField,0,0);
Scene scene = new Scene(root, 400, 250);
primaryStage.setTitle("Notifier");
primaryStage.setScene(scene);
primaryStage.show();
}
最佳答案
您正在设置标签中的文本。在文本字段中设置:
btn.setOnAction((ActionEvent event) -> {
textField.setText("You have been notified");
});
关于java - 如何在 Java 中将文本打印到文本区域而不是文本区域下方,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32484831/