java JavaFX文本区域包含带字符串的空行。split()?
我想弄明白这一点已经有一段时间了,但还不能完全实现。本质上,我有一个JavaFX TextArea,我想用最后一行输入(即按下ENTER键后插入符号上方的行)构造一个名为CommandWrapper的新对象。每当我在输入命令后按ENTER键时,它都能完美地工作,但由于某些原因,我的字符串。如果我没有输入命令,split()函数将不会得到空行,如下面的GIF所示:
以下是有关该问题的代码:
package com.mswordhf.jnet.java.contollers;
import java.net.URL;
import java.util.Arrays;
import java.util.List;
import java.util.ResourceBundle;
import com.mswordhf.jnet.java.models.JnetModel;
import com.mswordhf.jnet.java.modules.CommandWrapper;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TextArea;
import javafx.scene.input.KeyCode;
public class CmdController implements Initializable {
private JnetModel model;
private int clientIndex;
@FXML private TextArea commandTextArea;
public CmdController(JnetModel model, int clientIndex) {
this.model = model;
this.clientIndex = clientIndex;
}
@Override
public void initialize(URL url, ResourceBundle rb) {
commandTextArea.setOnKeyPressed(keyEvent -> {
if(keyEvent.getCode() == KeyCode.ENTER) {
List<String> lines = Arrays.asList(commandTextArea.getText().split("\\n"));
String command = lines.get(lines.size() - 1);
System.out.println(command);
if(command == "\n") {
System.out.println("Worked");
}else {
CommandWrapper wrapper = new CommandWrapper(command);
model.getClients().get(clientIndex).getHandle().sendModule(wrapper);
if(!model.getCmdOutput.isRunning()) {
model.getCmdOutput.reset();
model.getCmdOutput.start();
}
}
}
});
model.getCmdOutput.setOnSucceeded(event -> {
for(String line : model.getCmdOutput.getValue()) {
commandTextArea.appendText(line + "\n");
}
model.clearList();
});
}
}
# 1 楼答案
我真的不确定为什么,但使用:
完全按照预期工作