加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 创建一个“命令”控制台

发布时间:2020-12-14 05:09:14 所属栏目:Java 来源:网络整理
导读:我有一个不寻常的问题:如何使用Swing创建“命令控制台”? 我想要的是用户键入命令的控制台,按enter键,并显示命令的输出.我不想允许用户更改“提示”和旧的输出.我在想像Windows CMD.EXE这样的东西. 我看了一个this的问题,但是它并没有回答我的问题. 解决方
我有一个不寻常的问题:如何使用Swing创建“命令控制台”?

我想要的是用户键入命令的控制台,按enter键,并显示命令的输出.我不想允许用户更改“提示”和旧的输出.我在想像Windows CMD.EXE这样的东西.

我看了一个this的问题,但是它并没有回答我的问题.

解决方法

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

>闪烁的光标
>命令历史
>剪切/复制/粘贴,包括使用CTRL箭头键选择
>命令完成
> Unicode字符输入
>彩色文本输出
> …它都包裹在滚动窗格中.

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键.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读