【问题标题】:New Line in TextField文本字段中的新行
【发布时间】:2018-04-02 18:51:06
【问题描述】:

我试图在我的 TextField 中添加一些预定义的文本,但我不知道如何在单词之间添加新行。我试过\n,但它不起作用。

代码:

    package view;

import javafx.scene.text.Text;

public class Overview extends VBox{

    private TextField info;

    public Overview() {

        //Adds a little border around the app so the text field doesn't go to the edges
        this.setPadding(new Insets(30, 30, 30, 30));

        //----- Initialising text field --------
        info = new TextField("Name:\n" + "PNumber:\n"+ "Email:\n"+ "Date:\n"+ "Course:\n"+ ""+ "Selected Modules:\n"+ "==============");
        info.setAlignment(Pos.TOP_LEFT);
        info.setMinHeight(850);
        info.setEditable(false);


        //------ Adding the text to the Text Field --------


        //-------- Putting it into VBox so that it stretches with the screen ----------
        VBox fit = new VBox(info);




        this.getChildren().add(fit);

    }

}

有谁知道如何在每个单词之间添加换行符? 我希望单词之间有一个分隔,然后它会在以后自动填写信息。

【问题讨论】:

  • 使用TextArea 而不是TextField
  • @Rishaldevsingh 已经解决了,谢谢:D
  • 很高兴,请接受答案:)

标签: java javafx javafx-8


【解决方案1】:

TextField 仅适用于单行,它不会像您希望代码实现的那样呈现多行。所以不要使用TextField,而是使用TextArea

【讨论】:

    【解决方案2】:

    尝试使用文本区域而不是文本字段。您可以在 textarea 中使用多行,但 textfield 是单行控件

    【讨论】:

      猜你喜欢
      • 2011-01-10
      • 2021-04-13
      • 2015-05-17
      • 2021-03-18
      • 2013-07-31
      • 1970-01-01
      • 2016-09-05
      • 1970-01-01
      • 2023-04-10
      相关资源
      最近更新 更多