设置java.util.Scanner的占位符文本

时间:2014-04-06 02:44:08

标签: java eclipse java.util.scanner

作为一项任务的一部分,我需要用Java构建一个系统shell模拟器。其中一个功能是使用向上箭头键(↑)来指示运行命令的历史记录。我需要把占位符'控制台系统输入中的文本。

我了解如何存储运行命令的历史记录。我不确定的部分是如何获得'占位符'扫描仪输入中的文字。

示例(用户的输入为绿色):

enter image description here

当用户点击向上箭头键时,他们可以得到这个(假设先前已经cal运行):

enter image description here

可以使用Scanner系统类完成吗?如果没有,这可以用别的东西来完成吗?

1 个答案:

答案 0 :(得分:0)

基本上每次用户输入命令时,您都必须将该命令(很可能是String)存储到某种List中。

因此...

ArrayList<String> commands = new ArrayList<String>();
int displacementFromLastCommand = 0;

每次扫描仪读取一行......

String newCommand = scanner.nextLine();
commands.add(newCommand);
displacementFromLastCommand = 0;

然后在你的keyEvent上......

public void onKeyReleased(KeyEvent event) {
    if(event.getKeyCode() == KeyEvent.VK_UP) {
        displacementFromLastCommand ++;
        // set the current text to commands.get(commands.size() - dis);
    }
}
相关问题