3


EDIT: This feature only occurs when I invoke the clearScreen method of ConsoleReader! Any other changes don't have an effect. Is this then a bug in JLine2?


JLine2:

Why, when I run this, do I get two console prompts directly following each other (----> ---->)? Is it because two consoles are being created? I do not understand how.
What am I failing to see here?

import java.io.IOException;
import jline.console.ConsoleReader;

class TextUi implements Ui {
    private static final String prompt1 = "---->  ";
    public void homeScreen() {
        try {
            ConsoleReader con = new ConsoleReader();
            con.setPrompt(prompt1);

            con.clearScreen();
            System.out.println("Press any key to continue...");
            con.readCharacter();
            con.clearScreen();

            System.out.println("Here is a prompt. Do something and press enter to continue...");
            String line = con.readLine();
            con.clearScreen();

            System.out.println("You typed: ");
            System.out.println(line);
            System.out.println("Press any key to exit. ");
            con.readCharacter();
            con.clearScreen();
        } catch (IOException e) {
            e.printStackTrace();

        }   
    }   
    public void exitSplash() {
        System.out.println("Thank You. Goodbye.");
        System.out.println("");
    }   
    public void creditsScreen() {
    }   
    public static void main (String argv[]) {
            TextUi ui = new TextUi();
            ui.homeScreen();
            ui.exitSplash();
    }   
}
4

1 回答 1

0

这不是错误,您只需要con.flush()在每次调用后调用con.clearScreen()

clearScreen方法不会flush()自动调用(在某些情况下它可能会在不刷新的情况下工作)但是该readLine方法会,因此只有在您调用con.readLine(). 这导致最后System.out.println(在 之前readLine)被清除,即使它是在之后调用con.clearScreen()的。

您在try块内的代码应更改为:

ConsoleReader con = new ConsoleReader();
con.setPrompt(prompt1);

con.clearScreen();
con.flush();
System.out.println("Press any key to continue...");
con.readCharacter();
con.clearScreen();
con.flush();

System.out.println("Here is a prompt. Do something and press enter to continue...");
String line = con.readLine();
con.clearScreen();
con.flush();

System.out.println("You typed: ");
System.out.println(line);
System.out.println("Press any key to exit. ");
con.readCharacter();
con.clearScreen();
con.flush();
于 2015-12-25T13:25:33.643 回答