gpt4 book ai didi

java - 创建一个 "Command"控制台

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:53:58 24 4
gpt4 key购买 nike

我有一个不寻常的问题:如何使用 Swing 创建“命令控制台”?

我想要的是一个控制台,用户可以在其中输入命令,按回车键,命令的输出显示在下面。我不想让用户更改“提示”和较旧的输出。我在想类似 Windows CMD.EXE 的东西。

我看过this问题,但是它没有回答我的问题。

最佳答案

BeanShell 提供了一个 JConsole,一个具有以下功能的命令行输入控制台:

  • 闪烁的光标
  • 命令历史
  • 剪切/复制/粘贴,包括使用 CTRL+箭头键进行选择
  • 命令完成
  • Unicode字符输入
  • 彩色文本输出
  • ...所有这些都包含在一个滚动 Pane 中。

BeanShell JAR 可从 http://www.beanshell.org/download.html 获得源代码可通过 SVN 从 svn co http://ikayzo.org/svn/beanshell

获得

有关 JConsole 的更多信息,请参阅 http://www.beanshell.org/manual/jconsole.html

这是在您的应用程序中使用 BeanShell 的 JConsole 的示例:

import java.awt.Color;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;

import javax.swing.JFrame;

import bsh.util.GUIConsoleInterface;
import bsh.util.JConsole;

/**
* Example of using the BeanShell project's JConsole in
* your own application.
*
* JConsole is a command line input console that has support
* for command history, cut/copy/paste, a blinking cursor,
* command completion, Unicode character input, coloured text
* output and comes wrapped in a scroll pane.
*
* For more info, see http://www.beanshell.org/manual/jconsole.html
*
* @author tukushan
*/
public class JConsoleExample {

public static void main(String[] args) {

//define a frame and add a console to it
JFrame frame = new JFrame("JConsole example");

JConsole console = new JConsole();

frame.getContentPane().add(console);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600,400);

frame.setVisible(true);

inputLoop(console, "JCE (type 'quit' to exit): ");

System.exit(0);
}

/**
* Print prompt and echos commands entered via the JConsole
*
* @param console a GUIConsoleInterface which in addition to
* basic input and output also provides coloured text
* output and name completion
* @param prompt text to display before each input line
*/
private static void inputLoop(GUIConsoleInterface console, String prompt) {
Reader input = console.getIn();
BufferedReader bufInput = new BufferedReader(input);

String newline = System.getProperty("line.separator");

console.print(prompt, Color.BLUE);

String line;
try {
while ((line = bufInput.readLine()) != null) {
console.print("You typed: " + line + newline, Color.ORANGE);

// try to sync up the console
//System.out.flush();
//System.err.flush();
//Thread.yield(); // this helps a little

if (line.equals("quit")) break;
console.print(prompt, Color.BLUE);
}
bufInput.close();
} catch (IOException e) {
e.printStackTrace();
}

}
}

注意:JConsole 返回“;”如果您自己按 Enter。

关于java - 创建一个 "Command"控制台,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1255373/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com