我有几个小应用程序,它们使用标准控制台检索用户输入,并显示System.in和System.out的消息。
现在我想实现一些从这些应用程序调用的基于Swing的类,它显示了一个具有2个文本区域的框架,一个用于输入(因此与System.in关联),另一个(不可编辑)用于显示消息(因此与System.out关联),(实际上,创建一个简单的基于swing的gui并从事件调度器线程启动它并不那么复杂,将其全部导出为jar并将其作为库包含在原始项目中也是如此)。到目前为止,我遇到的唯一问题是将标准的System.in和System.out交换为与2JTextArea
相关联的一些自定义问题。事实上,我在网上检查了一些解决方案,最后得到了以下几行代码:
我使用2个PipedInputStream和一个PrintWriter:
private final PipedInputStream inPipe = new PipedInputStream();
private final PipedInputStream outPipe = new PipedInputStream();
private PrintWriter inWriter;
然后我交换流
System.setIn(inPipe);
System.setOut(new PrintStream(new PipedOutputStream(outPipe), true));
inWriter = new PrintWriter(new PipedOutputStream(inPipe), true);
为了从outPipe检索数据,我使用了一个SwingWorker
,它的doInBackgroud
方法引出了一个Scanner
,从outPipe读取行并发布它们,以便将这些行字符串附加到不可编辑的JTextArea中。同时,keyListener
检查VK_ENTER
点击,以便从用作提示的JTextField中获取文本,一旦发生这种情况,文本将使用System.out本身显示,并且它有效地出现在以前的JTextArea中,因此上述SwingWorker工作,然后我在inWriter(与System.in相关的管道关联的PrintStream
对象)中写入了同一行文本,因此该行应该可以从原始应用程序中存在的Reader对象中读取。
不幸的是,这是代码中唯一不起作用的部分。事实上,一旦我启动新的gui控制台,然后更改流,原始应用程序将只显示它在System.out上打印的文本,但当它想读取用户写的文本时,例如,抛出BufferedReader或Scanner对象,什么都不会发生,就好像入流是空的一样。
我认为这是由于SwingWorker doInBackground方法中的Scanner,因为当它读取outPipe上的下一行时,它也会清理流本身。有什么办法解决这个问题吗?我知道我可以编写新的方法来处理输入和输出,但我希望保持这种非侵入性的方法,因此在不编辑原始代码的情况下,在原始应用程序主方法中创建gui控制台对象。提前谢谢。
更新1
这是控制台类,所有操作都在这里完成
public class Console extends JFrame implements KeyListener
{
private JTextField prompt;
private JTextArea log;
private final PipedInputStream inPipe = new PipedInputStream();
private final PipedInputStream outPipe = new PipedInputStream();
private PrintWriter inWriter;
public Console(String title)
{
super(title);
System.setIn(inPipe);
try
{
System.setOut(new PrintStream(new PipedOutputStream(outPipe), true));
inWriter = new PrintWriter(new PipedOutputStream(inPipe), true);
}
catch(IOException e)
{
System.out.println("Error: " + e);
return;
}
JPanel p = new JPanel();
p.setLayout(null);
log = new JTextArea();
log.setEditable(false);
log.setBounds(10, 10, 345, 250);
p.add(log);
prompt = new JTextField();
prompt.setBounds(10, 270, 356, 80);
prompt.addKeyListener(this);
p.add(prompt);
getContentPane().add(p);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setSize(392, 400);
setLocationRelativeTo(null);
(new SwingWorker<Void, String>()
{
protected Void doInBackground() throws Exception
{
Scanner s = new Scanner(outPipe);
while (s.hasNextLine())
{
String line = s.nextLine();
publish(line);
}
return null;
}
@Override
protected void process(java.util.List<String> chunks)
{
for (String line : chunks)
{
if (line.length() < 1)
continue;
log.append(line.trim() + "n");
}
}
}).execute();
}
public void execute()
{
String text = prompt.getText();
prompt.setText("");
System.out.println(text);
inWriter.print(text.trim().replaceAll("rn", ""));
}
@Override
public void keyPressed(KeyEvent e)
{
if (e.getKeyCode() == KeyEvent.VK_ENTER)
execute();
}
@Override
public void keyReleased(KeyEvent e)
{
if (e.getKeyCode() == KeyEvent.VK_ENTER)
execute();
}
@Override
public void keyTyped(KeyEvent e)
{
if (e.getKeyCode() == KeyEvent.VK_ENTER)
execute();
}
// this is the method called from the original application
public static void setConsole(final String title)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
new Console(title);
//System.out.println("somewhat");
}
});
}
}
我自己找到了解决方案,实际上第一篇文章中发布的所有代码几乎都是正确的,问题是从原始程序中的System.读取,因为它是用Scanner
对象完成的(但我也用BufferedReader测试了它),而且它似乎与字符串终止有关,因为如果我使用简单的System.in.read()
调用进行读取,我得到了正确的数据,当然是逐字符的。由于Scanner
方法和read
方法都是I/O阻塞,我认为这与字符串的终止有关。因为当我在InputStream上编写它时,我从任何LF和CF字符中清除了该字符串,正如您在上面的代码中看到的那样,所以当调用println
时,只应附加一个\r\n。但是,逐个字符地阅读结果,我看到结尾有\r\n\r\n,所以我想这应该是与字符串结尾有关的东西,即使我无法找到正确的处理方法。我会通过更新向您提供信息,但如果您有建议和/或反馈,我们显然会欢迎您。