5

嘿,我一直在使用 Java 在 Windows 控制台中开发一个应用程序,并希望将它放在其所有控制台图形荣耀中。

是否有一个简单的 Web 小程序 API 可以用来移植我的应用程序?

我只是使用基本的 System.out 和 System.in 功能,但我很高兴重建我的 I/O 包装器。

我认为这些方面的东西对于任何想要将他们的工作放到网上的 Java 初学者来说都是一笔巨大的财富。

4

5 回答 5

4

当然,只需制作一个小程序,在其上放置一个带有 JFrame 的小型摇摆 UI,该 JFrame 具有两个组件 - 一个用于写入输出,一个用于输入输入。在页面中嵌入小程序。

于 2008-09-26T07:52:10.993 回答
4

我按照拉斯的建议做了,并写了我自己的。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.awt.Font;

public class Applet extends JFrame {
    static final long serialVersionUID = 1;

    /** Text area for console output. */
    protected JTextArea textArea;

    /** Text box for user input. */
    protected JTextField textBox;

    /** "GO" button, in case they don't know to hit enter. */
    protected JButton goButton;

    protected PrintStream printStream;
    protected BufferedReader bufferedReader;

    /**
     * This function is called when they hit ENTER or click GO.
     */
    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            goButton.setEnabled(false);
            SwingUtilities.invokeLater(
                new Thread() {
                    public void run() {
                        String userInput = textBox.getText();
                        printStream.println("> "+userInput);
                        Input.inString = userInput;
                        textBox.setText("");
                        goButton.setEnabled(true);
                    }
                }   
            );
        }
    };

    public void println(final String string) {
        SwingUtilities.invokeLater(
            new Thread() {
                public void run() {
                    printStream.println(string);
                }
            }   
        );
    }

    public void printmsg(final String string) {
        SwingUtilities.invokeLater(
            new Thread() {
                public void run() {
                    printStream.print(string);
                }
            }   
        );
    }

    public Applet() throws IOException {
        super("My Applet Title");

        Container contentPane = getContentPane();

        textArea = new JTextArea(30, 60);
        JScrollPane jScrollPane = new JScrollPane(textArea);
        final JScrollBar jScrollBar = jScrollPane.getVerticalScrollBar();
        contentPane.add(BorderLayout.NORTH, jScrollPane);
        textArea.setFocusable(false);
        textArea.setAutoscrolls(true);
        textArea.setFont(new Font("Comic Sans MS", Font.TRUETYPE_FONT, 14));

        // TODO This might be overkill
        new Thread() {
            public void run() {
                while(true) {
                    jScrollBar.setValue(jScrollBar.getMaximum());
                    try{
                        Thread.sleep(100);
                    } catch (Exception e) {}
                }
            }
        }.start();

        JPanel panel;
        contentPane.add(BorderLayout.CENTER, panel = new JPanel());

        panel.add(textBox = new JTextField(55));
        textBox.addActionListener(actionListener);

        panel.add(goButton = new JButton("GO"));
        goButton.addActionListener(actionListener);

        pack();

        // End of GUI stuff

        PipedInputStream inputStream;
        PipedOutputStream outputStream;

        inputStream = new PipedInputStream();
        outputStream = new PipedOutputStream(inputStream);

        bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "ISO8859_1"));
        printStream = new PrintStream(outputStream);

        new Thread() {
            public void run() {
                try {
                    String line;
                    while ((line = bufferedReader.readLine()) != null) {
                        textArea.append(line+"\n");
                    }
                } catch (IOException ioException) {
                    textArea.append("ERROR");
                }
            }
        }.start();
    }
}

下面的代码在一个单独的类“Input”中,它有一个静态的“inString”字符串。

    public static String getString() {
        inString = "";

        // Wait for input
        while (inString == "") {
            try{
                Thread.sleep(100);
            } catch (Exception e) {}
        }

        return inString;
    }

在项目的整个生命周期中,我可能会更多地维护这段代码,但在这一点上 - 它可以工作:)

于 2008-10-09T12:35:29.547 回答
3

作为一个光荣且非常有用的类似 cnsole 的 web 应用程序的首要示例,请参阅goosh,即 Google Shell。我无法想象没有它浏览网络了。

诚然,没有源代码,但您可能会通过使用 Firebug 左右来获得一点它的魔力。

使用 TextArea 可能是一种容易出错的方法。请记住,您需要对此 TextArea 进行输入和输出,因此您必须跟踪光标位置。我建议,如果你真的采用这种方法,你可以抽象出一个纯 TextArea(可能是继承?)并使用一个组件,例如,aprompt()来显示提示并启用输入,并且 a 也遵循通常的 shell 抽象具有stdin(一个 InputStream,从提示中读取,但可以绑定到,比如说文件左右),stdout并且可能还有stderrOutputStreams,绑定到 TextArea 的文本。

这不是一件容易的事,而且我不知道有任何图书馆可以做到这一点。

于 2008-09-27T21:01:10.737 回答
0

我记得几年前看到了 telnet 客户端小程序实现(当人们使用 telnet 时)。也许你可以把它们挖出来并修改它们。

于 2008-09-26T07:45:24.740 回答
0

System.out 和 System.in 是静态的,因此是邪恶的。您需要通过您的程序用非静态变量(“从上面的参数化”)替换它们。从小程序中,您不能使用 System.setOut/setErr/setIn。

然后你就差不多整理好了。一个小程序。添加一个 TextArea(或等效项)。将输出附加到文本区域。将击键写入输入。任务完成。

于 2008-09-26T12:45:03.637 回答