Java-コンソールのようなWebアプレット[終了]
質問
ねえ、私はJavaを使用してWindowsコンソールでアプリケーションを開発しており、すべてのconsole-graphics-gloryでオンラインにしたいです。
アプリを移植するために使用できるシンプルなWebアプレットAPIはありますか?
基本的なSystem.outおよびSystem.in機能を使用していますが、I / Oラッパーを再構築できます。
これらのラインに沿った何かは、作業をオンラインにしたいJava開発者にとって大きな財産になると思います。
解決
確かに、アプレットを作成し、2つのコンポーネントを持つJFrameで小さなスイングUIを配置します。1つは出力の書き込み用、もう1つは入力の入力用です。アプレットをページに埋め込みます。
他のヒント
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」を持つ別のクラス「Input」にありました。文字列。
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
(プロンプトから読み取るがバインドできるInputStream to、たとえばファイルなど)と stdout
および場合によっては stderr
、OutputStreams、TextAreaのテキストにバインドされています。
これは簡単な作業ではなく、それを行うためのライブラリも知りません。
Telnetクライアントアプレットの実装を数年前に見たことを覚えています(人々がtelnetを使用したとき)。たぶんあなたはそれらを掘り出して修正することができます。
System.outおよびSystem.inは静的であるため、悪です。それらを非静的に置き換えるプログラムを実行する必要があります(「上記のパラメーター化」)。アプレットからは、System.setOut / setErr / setInを使用できません。
その後、あなたはほとんどソートされています。アプレット。 TextArea(または同等のもの)を追加します。テキスト領域に出力を追加します。キーストロークを入力に書き込みます。ジョブ完了。