在 Java 中获得文本用户界面(类似 ncurses)功能的最佳方法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1321308/
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
What's the best way to get text user-interfaces (ncurses-like) functionality in Java?
提问by Anthony
I need to implement a console application (possibly in Java) with ncurses-like functionality (such as navigating a menu and redrawing the whole screen).
我需要使用类似 ncurses 的功能(例如导航菜单和重绘整个屏幕)来实现一个控制台应用程序(可能是在 Java 中)。
The only solutions that I can find to do this so far are CHARVA ("A Java Windowing Toolkit for Text Terminals"), tuipeer ("A Text User Interface for the Java AWT") and a really old Dr. Dobb's article ("A Text UI for the Java AWT ").
到目前为止,我能找到的唯一解决方案是 CHARVA(“A Java Windowing Toolkit for Text Terminals”)、tuipeer(“A Text User Interface for the Java AWT”)和一篇非常古老的 Dr. Dobb 的文章(“A Java AWT 的文本 UI")。
So far, CHARVA is the best thing that I can find but I don't like the idea of it using JNI to wrap curses.
到目前为止,CHARVA 是我能找到的最好的东西,但我不喜欢它使用 JNI 来包装诅咒的想法。
Is there any standard way, say with AWT/Swing, to do do this? What other alternatives are there?
有没有标准的方法,比如 AWT/Swing,来做到这一点?还有哪些其他选择?
采纳答案by Waldemar Wosiński
Since 2010 there is Lanterna :
自 2010 年以来,有 Lanterna :
Lanterna is a Java library allowing you to write easy semi-graphical user interfaces in a text-only environment, very similar to the C library curses but with more functionality. Lanterna is supporting xterm compatible terminals and terminal emulators such as konsole, gnome-terminal, putty, xterm and many more. One of the main benefits of lanterna is that it's not dependent on any native library but runs 100% in pure Java.
Lanterna 是一个 Java 库,允许您在纯文本环境中编写简单的半图形用户界面,与 C 库 curses 非常相似,但具有更多功能。Lanterna 支持 xterm 兼容终端和终端仿真器,例如 konsole、gnome-terminal、putty、xterm 等等。lanterna 的主要好处之一是它不依赖于任何本地库,而是 100% 以纯 Java 运行。
More here: https://github.com/mabe02/lanterna
回答by Anthony
I believe Jcurses is a native java implementation of the curses API, I recall it have a few quirks, but it should be able to do what you want:
我相信 Jcurses 是 Curses API 的原生 Java 实现,我记得它有一些怪癖,但它应该能够做你想做的事:
回答by Pierre
Try java curses(sorry it uses JNI). I also tried to implement a short version of this library just to learn JNI , see http://plindenbaum.blogspot.com/2008/01/java-native-interface-jni-notebook.html. One could also imagine a specialized JPanel displaying a matrix of characters:
尝试java 诅咒(抱歉它使用 JNI)。我还尝试实现这个库的一个简短版本来学习 JNI,请参阅http://plindenbaum.blogspot.com/2008/01/java-native-interface-jni-notebook.html。还可以想象一个专门的 JPanel 显示字符矩阵:
public class TPanel extends JPanel
{
private Vector<Vector<YourCharAndStyle>> rows;
protected void paintComponent(Graphics g)
{
//paint the characters
(...)
}
}
回答by D'Nabre
The short answer is deal with a Java wrapper around curses.
简短的回答是处理围绕curses 的Java 包装器。
The long answer:
长答案:
Terminals vary a lot, which is why terminfo/termcap libraries exist and why they are a mess to deal with (The maintainers of those projects are saints btw). They abstract away all the really basic terminal variations to something sane. Curses make those in nice efficient libraries to use.
终端变化很大,这就是 terminfo/termcap 库存在的原因以及它们处理起来一团糟的原因(顺便说一句,这些项目的维护者是圣人)。他们将所有真正基本的终端变化抽象为理智的东西。诅咒使那些在很好的高效库中使用。
If you want a pure Java solution, you'll need both those libraries or their equivalent in Java. I'm sure someone will point you to that if it exists, but I as far as I know it doesn't exist.
如果您想要一个纯 Java 解决方案,您将需要这些库或它们在 Java 中的等价物。我敢肯定有人会指出如果它存在,但据我所知它不存在。
回答by Alexey Sviridov
I'm using JavaTUI (http://sourceforge.net/projects/javatui/files/) in my several console java projects. It's best what i can find but it so far from perfect. I'm think there is not a good TUI implemetation in java world.
我在我的几个控制台 java 项目中使用 JavaTUI ( http://sourceforge.net/projects/javatui/files/)。这是我能找到的最好的,但它远非完美。我认为在 Java 世界中没有一个好的 TUI 实现。
回答by Eldelshell
I think it would be better to abstract your Java code from the TUI and use ncurses against several separated parts of your application or using arguments, in a web-services style. For example, code your TUI and when the user calls an action, use ncurses to call your code passing some parameters
我认为最好从 TUI 中抽象出您的 Java 代码,并对应用程序的几个独立部分使用 ncurses 或以 Web 服务风格使用参数。例如,对你的 TUI 进行编码,当用户调用一个动作时,使用 ncurses 来调用你的代码并传递一些参数
java -Daction=doSomething MyApp
This way you can code your app using a GUI also in case you need to.
通过这种方式,您也可以在需要时使用 GUI 对您的应用程序进行编码。
回答by stinkymatt
You may want to go vote for this issue here: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6351276
您可能想在这里为这个问题投票:http: //bugs.sun.com/bugdatabase/view_bug.do?bug_id=6351276
Essentially there is no good way to get ncurses-like functionality without JNI until this issue is addressed.
从本质上讲,在解决此问题之前,没有 JNI 就没有获得类似 ncurses 的功能的好方法。
回答by Yousha Aleayoub
You can try Jexer - Java Text User Interface:
您可以尝试 Jexer - Java 文本用户界面: