私はしばらくの間これを理解しようとしていましたが、それを完全に機能させることができません。基本的に、JavaFX TextAreaがあり、入力の最後の行(つまり、ENTERキーが押された後のキャレットの上の行)を使用してCommandWrapperという名前の新しいオブジェクトを作成したいと思います。コマンドを入力した後にEnterキーを押すと、問題なく動作しますが、以下のGIFに示されているコマンドアッシュを入力しないと、何らかの理由でString.split()関数が空の行を取得しません。
この問題に関するコードは次のとおりです。
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();
});
}
}
理由はよくわかりませんが、次のものを使用しています。
commandTextArea.setOnKeyReleased(keyevent -> {
//code...
}
完全に意図したとおりに機能します。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加