【发布时间】:2010-09-13 10:39:54
【问题描述】:
嘿,我一直在用 Java 在 windows 控制台中开发一个应用程序,并且想把它放到它所有的控制台图形荣耀中。
是否有一个简单的网络小程序 API 可以用来移植我的应用程序?
我只是使用基本的 System.out 和 System.in 功能,但我很高兴重建我的 I/O 包装器。
我认为,对于任何想要将他们的工作放到网上的 Java 初学者来说,这些东西将是一笔巨大的财富。
【问题讨论】:
嘿,我一直在用 Java 在 windows 控制台中开发一个应用程序,并且想把它放到它所有的控制台图形荣耀中。
是否有一个简单的网络小程序 API 可以用来移植我的应用程序?
我只是使用基本的 System.out 和 System.in 功能,但我很高兴重建我的 I/O 包装器。
我认为,对于任何想要将他们的工作放到网上的 Java 初学者来说,这些东西将是一笔巨大的财富。
【问题讨论】:
我记得大约几年前(当人们使用 telnet 时)看到了 telnet 客户端小程序实现。也许你可以把它们挖出来修改一下。
【讨论】:
当然,只需制作一个小程序,在其上放置一个带有 JFrame 的小型摇摆 UI,该 JFrame 具有两个组件 - 一个用于写入输出,一个用于输入输入。在页面中嵌入小程序。
【讨论】:
System.out 和 System.in 是静态的,因此是邪恶的。您需要通过您的程序用非静态变量(“从上面的参数化”)替换它们。从小程序中,您不能使用 System.setOut/setErr/setIn。
那么你的排序就差不多了。一个小程序。添加一个 TextArea(或等效项)。将输出附加到文本区域。将击键写入输入。任务完成。
【讨论】:
作为一个出色且非常有用的类似 cnsole 的 web 应用程序的首要示例,请参阅 goosh,Google Shell。我无法想象没有它浏览网络了。
当然,没有源代码,但您可能会通过使用 Firebug 左右来发挥它的魔力。
使用 TextArea 可能是一种容易出错的方法。请记住,您需要对此 TextArea 进行输入和输出,因此您必须跟踪光标位置。我建议,如果你真的采用这种方法,你可以抽象出一个普通的 TextArea (继承,也许?)并使用一个组件,例如a prompt() 显示提示并启用输入,并且 a 还遵循具有 stdin (一个 InputStream,从提示中读取,但可以绑定到,比如说文件左右)和 stdout可能还有stderr,OutputStreams,绑定到TextArea的文本。
这不是一件容易的事,而且我不知道有什么库可以做到这一点。
【讨论】:
我按照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();
}
}
下面的代码在一个单独的类“Input”中,它有一个静态的“inString”字符串。
public static String getString() {
inString = "";
// Wait for input
while (inString == "") {
try{
Thread.sleep(100);
} catch (Exception e) {}
}
return inString;
}
在项目的整个生命周期中,我可能会更多地维护这段代码,但在这一点上 - 它可以工作:)
【讨论】: