是否可以打印用户可以编辑的文本(对于控制台程序)?

发布于 2024-08-06 22:47:20 字数 175 浏览 15 评论 0原文

假设我允许用户编辑某些内容,例如地址簿中的电话号码(实际上,这正是我正在做的事情)。我可以在 println 中添加一些内容,以允许我插入一个变量以显示为完全可编辑的文本吗?我正在做的任务实际上并不需要这样做,但我认为这样做会很酷。我正在谷歌上查找,但找不到任何东西,但我又不知道我在寻找什么,也不知道我是否记住了正确的条款......

Say I allow the user to edit something, like the phone number in an Address Book (actually, that's exactly what I'm doing). Is there something that I can add to println that will allow me to insert a variable to display as fully editable text? The assignment that I'm doing this for doesn't actually call for this, but I think it would be cool to have. I'm looking on Google but can't find anything, then again I don't really know what I'm looking for and whether or not I have the correct terms in mind ...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

神爱温柔 2024-08-13 22:47:20

不,不只是使用 Java 在框架中提供的内容。编辑某些文本需要

  1. 对按键进行操作,这是不可能的,因为在 Java 中,输入被缓冲(即等待按下 Enter)
  2. 以在输出的文本中移动,这也是不可能的

这可以完成使用一些本机代码(Linux 上的 ncurse,...),使用 JNI 或 JNA,但不是那么容易。

请注意,有些项目旨在添加这些功能,因此如果您可以使用核心库之外的功能,您可以尝试一下......例如 http://code.google.com/p/java-console-api/

No, not using only what Java provides in the framework. Editing some text would require to

  1. act on key press, which is not possible as in Java the input is buffered (i.e., wait for Enter to be pressed)
  2. to move around in the text you output, which is also not possible

This could be done using some native code (ncurse on linux, ...), using JNI or JNA, but not that easily.

Note that there are some projects that aim to add those functionalities, so if you can use something outside of the core libraries, you could give them a tries... for instance http://code.google.com/p/java-console-api/

木落 2024-08-13 22:47:20

按照功能和复杂性的简单性和可移植性的顺序,有多种选项:

  1. 简单地提示信息,读取完整的(返回终止的)响应行,并允许使用正常的终端输入设施基本编辑。

  2. 使用 gnu readline 库之类的东西来允许更高级的编辑。尽管如此,您仍然不会有小部件(屏幕上特定位置的文本输入框)。这里有一个java实现: http://java-readline.sourceforge.net/

  3. 使用 ncurses 之类的东西来专门定位光标、打印文本标签、处理按键以及实现您自己的文本输入框。不好玩。

  4. 使用文本用户界面库 (TUI),如下所示:http://www.bmsi。 com/tuipeer/

There are various options for this, in order of simplicity and portability to features and complexity:

  1. Simply prompt for the information, reading a complete (return-terminated) line of response, and allow the normal terminal input facilities to be used for basic editing.

  2. Use something like the gnu readline library to allow more advanced editing. You still won't have widgets (text input boxes at specific places on screen) as such though. There's a java implementation here: http://java-readline.sourceforge.net/

  3. Use something like ncurses to specifically position the cursor, print text labels, handle keypresses, and implement your own text input box. Not fun.

  4. Use a textual user interface library (TUI), like this one: http://www.bmsi.com/tuipeer/

两仪 2024-08-13 22:47:20

如果您打开了一个看起来像控制台窗口的窗口,并且可以对按键事件做出反应,那么您可以执行您所要求的操作,但是,否则,如果您只是运行一个程序,该程序将停止执行并将控制返回给你的控制台,所以它不能做任何其他事情。

但是,如果您使用可编写脚本的 java 版本,您可以编写自己的 shell,然后您可以执行您所要求的操作,因为 shell 不会停止执行。

但是,这可能超出了你的课程范围。

If you opened a window that looks like the console window, and could react to keypress events, then you could do what you are asking, but, otherwise, if you are just running a program, the program will have ceased executing and returned control to your console, so it can't do anything else.

But, if you use a scriptable version of java you could write your own shell, and then you could do what you are asking, as the shell would not cease executing.

But, that will probably be beyond your course.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文