用于控制台应用程序的 Java gotoxy(x,y)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1001335/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java gotoxy(x,y) for console applications
提问by setzamora
I'm writing a simple console application (80x24) in Java, is there a gotoxy(x,y) equivalent?
我正在用 Java 编写一个简单的控制台应用程序 (80x24),是否有一个 gotoxy(x,y) 等价物?
采纳答案by Tom Jefferys
If by gotoxy(x,y), you want to reposition your cursor somewhere specific on the console, you can usually use VT100 control codes to do this. See http://www.termsys.demon.co.uk/vtansi.htm.
如果通过 gotoxy(x,y),您想将光标重新定位在控制台上的某个特定位置,通常可以使用 VT100 控制代码来执行此操作。请参阅http://www.termsys.demon.co.uk/vtansi.htm。
Do something like
做类似的事情
char escCode = 0x1B;
int row = 10; int column = 10;
System.out.print(String.format("%c[%d;%df",escCode,row,column));
Which should move the cursor to position 10,10 on the console.
这应该将光标移动到控制台上的位置 10,10。
回答by Bill the Lizard
回答by John Weldon
Not without pulling in a console curses
style library...
不是没有拉入控制台curses
样式库......
You can try javacursesand see if that helps you.
您可以尝试javacurses,看看是否对您有帮助。
回答by CodeZombie
I found lanternato be a very good library. It does not dependend on any native library but runs 100% in pure Java.
我发现lanterna是一个非常好的库。它不依赖于任何本地库,而是 100% 在纯 Java 中运行。
It offers a Screen
class which allows text output based on a coordinate system. For OS with a graphical environment it uses a Swing based terminal emulator. Unfortunately, you are not able to force terminal mode on Windows, so if you really need the terminal, use one of the solutions in the other answers.
它提供了一个Screen
允许基于坐标系的文本输出的类。对于具有图形环境的操作系统,它使用基于 Swing 的终端仿真器。不幸的是,您无法在 Windows 上强制使用终端模式,因此如果您确实需要终端,请使用其他答案中的解决方案之一。