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

我可以使用简单的网络小程序API来移植我的应用程序吗?

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

我认为,对于任何想要将他们的工作放在网上的Java开发人员而言,这些方面的内容将是一笔巨大的财富。

有帮助吗?

解决方案

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

其他提示

我做了 Lars 建议并写了我自己的。

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();
    }
}

以下代码位于单独的类“输入”中,该类具有静态“inString”。字符串。

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

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

        return inString;
    }

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

作为一个光荣且非常有用的类似cnsole的webapp的首要示例,请参阅Google Shell的 goosh 。我无法想象在没有它的情况下浏览网络。

当然,没有源代码,但是你可以通过使用Firebug来获得它的魔力。

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

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

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

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

然后你几乎整理好了。小程序。添加TextArea(或等效的)。将输出附加到文本区域。将键击写入输入。完成工作。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top