创建一个“命令"安慰

Create a quot;Commandquot; Console(创建一个“命令安慰)
本文介绍了创建一个“命令"安慰的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

I have a bit of an unusual question: How can I create a "Command Console" using Swing?

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

What I want to have is a console where the users type in commands, press enter, and the output from the command is displayed under. I don't want to allow the user to change the "prompt" and older output. I am thinking of something like Windows CMD.EXE.

我查看了 this 问题,但没有回答我的问题.

I had a look at this question, however it doesn't answer my question.

推荐答案

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

BeanShell provides a JConsole, a command line input console with the following features:

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

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

The BeanShell JARs are available from http://www.beanshell.org/download.html and the source is available via SVN from svn co http://ikayzo.org/svn/beanshell

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

For more info on JConsole see http://www.beanshell.org/manual/jconsole.html

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

Here is an example of using BeanShell's JConsole in your application:

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.

NB: JConsole returns ";" if you hit Enter by itself.

这篇关于创建一个“命令"安慰的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)
How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)
Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)
Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)
How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)
How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)