java控制台应用小程序_Java - 类似控制台的Web小程序

我做了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;

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值