我正在尝试在我的书中创建一些C ++代码的Java端口,本书使用一些操作系统特定的东西在控制台窗口中为文本着色。
我决定,因为没有简单的方法让它在java中跨平台工作,我可以创建一个带有模拟控制台的文本区域的窗口。打印和着色文本很简单,但我无法弄清楚如何使控制台的输入流部分工作。
我希望Console.getIn()方法返回的对象与System.in完全一样。我当前的代码sorta可以工作,但如果用作扫描器的输入,它将挂起。我在下面列出了我的实现,如果您能告诉我的代码有什么问题,或者有更好的方法,请告诉我。
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.MutableAttributeSet;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
import javax.swing.text.StyledDocument;
/**
*
* @author William Matrix Peckham
*/
public class Console extends JTextPane {
DocOutputStream out;
PrintStream pout;
DocInputStream in;
JFrame frame;
StyledDocument doc;
public Console() {
super();
setPreferredSize(new Dimension(500, 500));
doc = this.getStyledDocument();
out = new DocOutputStream(doc,this);
pout=new PrintStream(out);
in = new DocInputStream();
this.addKeyListener(in);
setFGColor(Color.black);
setBGColor(Color.white);
frame = new JFrame("Console");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new JScrollPane(this));
frame.pack();
frame.setVisible(true);
}
public InputStream getIn(){
return in;
}
public PrintStream getOut(){
return pout;
}
public void setFGColor(Color c){
StyleConstants.setForeground(out.cur, c);
}
public void setBGColor(Color c){
StyleConstants.setBackground(out.cur, c);
}
private static class DocOutputStream extends OutputStream {
StyledDocument doc;
MutableAttributeSet cur;
JTextPane pane;
public DocOutputStream(StyledDocument doc, JTextPane pane) {
this.doc = doc;
this.pane=pane;
cur=new SimpleAttributeSet();
}
@Override
public void write(int b) throws IOException {
try {
doc.insertString(doc.getLength(), (char)b+"", cur);
pane.setCaretPosition(doc.getLength());
} catch (BadLocationException ex) {
Logger.getLogger(Console.class.getName()).
log(Level.SEVERE, null, ex);
}
}
}
private static class DocInputStream extends InputStream implements KeyListener {
ArrayBlockingQueue<Integer> queue;
public DocInputStream(){
queue=new ArrayBlockingQueue<Integer>(1024);
}
@Override
public int read() throws IOException {
Integer i=null;
try {
i = queue.take();
} catch (InterruptedException ex) {
Logger.getLogger(Console.class.getName()).
log(Level.SEVERE, null, ex);
}
if(i!=null)
return i;
return -1;
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
int c = e.getKeyCode();
try {
queue.put(c);
} catch (InterruptedException ex) {
Logger.getLogger(Console.class.getName()).
log(Level.SEVERE, null, ex);
}
}
}
}
编辑:请注意,在read()中我尝试使用poll()而不是take(),这会阻止它完全阻塞,但我认为它可能会阻止Scanner永远阻止它,这是真的,但它也阻止了它得到任何真正的输入。
答案 0 :(得分:2)
也许Message Console可以满足您的要求。您可以使用JTextArea或JTextPane作为控制台组件。
答案 1 :(得分:2)
我明白了。问题是Scanner正在调用InputStream.read(char [],int,int),它被实现为读取整个流或整个大小的缓冲区。扫描程序试图填充8000+字节的缓冲区,默认的read(...)实现仅在缓冲区已满或读取-1(EOF)后才停止调用read()。
这导致扫描仪永远阻塞,因为大多数控制台输入永远不会那么长。解决方案是覆盖缓冲读取。我需要的版本将阻塞第一个字节,并且如果没有更多字符则返回,我认为BufferedInputStream已经通过调用流上的available()实现了这一点,所以我尝试在覆盖可用之后将我的类包装在一个中不行。我的新实现使用available()和EOF作为停止案例。
这就是现在的实现:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.MutableAttributeSet;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
import javax.swing.text.StyledDocument;
/**
*
* @author William Matrix Peckham
*/
public class Console extends JTextPane {
DocOutputStream out;
PrintStream pout;
DocInputStream in;
JFrame frame;
StyledDocument doc;
public Console() {
super();
setPreferredSize(new Dimension(500, 500));
doc = this.getStyledDocument();
out = new DocOutputStream(doc,this);
pout=new PrintStream(out);
in = new DocInputStream();
this.addKeyListener(in);
setFGColor(Color.black);
setBGColor(Color.white);
frame = new JFrame("Console");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new JScrollPane(this));
frame.pack();
frame.setVisible(true);
}
public InputStream getIn(){
return in;
}
public PrintStream getOut(){
return pout;
}
public void setFGColor(Color c){
StyleConstants.setForeground(out.cur, c);
}
public void setBGColor(Color c){
StyleConstants.setBackground(out.cur, c);
}
private static class DocOutputStream extends OutputStream {
StyledDocument doc;
MutableAttributeSet cur;
JTextPane pane;
public DocOutputStream(StyledDocument doc, JTextPane pane) {
this.doc = doc;
this.pane=pane;
cur=new SimpleAttributeSet();
}
@Override
public void write(int b) throws IOException {
try {
doc.insertString(doc.getLength(), (char)b+"", cur);
pane.setCaretPosition(doc.getLength());
} catch (BadLocationException ex) {
Logger.getLogger(Console.class.getName()).
log(Level.SEVERE, null, ex);
}
}
}
private static class DocInputStream extends InputStream implements KeyListener {
ArrayBlockingQueue<Integer> queue;
public DocInputStream(){
queue=new ArrayBlockingQueue<Integer>(1024);
}
@Override
public int read() throws IOException {
Integer i=null;
try {
i = queue.take();
} catch (InterruptedException ex) {
Logger.getLogger(Console.class.getName()).
log(Level.SEVERE, null, ex);
}
if(i!=null)
return i;
return -1;
}
@Override
public int read(byte[] b, int off, int len) throws IOException {
if (b == null) {
throw new NullPointerException();
} else if (off < 0 || len < 0 || len > b.length - off) {
throw new IndexOutOfBoundsException();
} else if (len == 0) {
return 0;
}
int c = read();
if (c == -1) {
return -1;
}
b[off] = (byte)c;
int i = 1;
try {
for (; i < len && available() > 0 ; i++) {
c = read();
if (c == -1) {
break;
}
b[off + i] = (byte)c;
}
} catch (IOException ee) {
}
return i;
}
@Override
public int available(){
return queue.size();
}
@Override
public void keyTyped(KeyEvent e) {
int c = e.getKeyChar();
try {
queue.put(c);
} catch (InterruptedException ex) {
Logger.getLogger(Console.class.getName()).
log(Level.SEVERE, null, ex);
}
}
@Override
public void keyPressed(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
}
}
}