📄 outputwindow.java
字号:
/*
* Created on 2004-5-25
*/
package yuchifang.javaIDE.editors;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import javax.swing.JEditorPane;
import javax.swing.text.BadLocationException;
import yuchifang.javaIDE.interfaces.IExecCaller;
/**
* @author yuchifang
*/
public class OutputWindow extends JEditorPane implements IExecCaller
{
private int lastOutputPos; //上一次做输出的位置
private OutputStream output; //给应用程序提供的输入
public OutputWindow()
{
setBackground(Color.WHITE);
setForeground(Color.BLUE);
setFont(new Font("宋体", Font.PLAIN, 12));
addKeyListener(
new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
if (e.isActionKey()) return;
switch (e.getKeyCode())
{
case KeyEvent.VK_ENTER:
{
inputTriggered();
break;
}
case KeyEvent.VK_BACK_SPACE:
case KeyEvent.VK_DELETE:
{
if (cannotModify())
e.consume();
break;
}
case KeyEvent.VK_X:
{
if ((e.getModifiers() & KeyEvent.CTRL_MASK) != 0)
{
if (cannotModify())
e.consume();
}
}
default :
if (cannotModify())
e.consume();
}//switch
}//keyPressed
}//new KeyAdapter
);//add listener
}
/**
* 判断编辑操作是否能够进行
* 当所进行的操作不会影响到上一次输出或者输入结果时返回true
* @return true表示编辑操作可以进行
*/
protected boolean cannotModify()
{
return (getCaretPosition() <= lastOutputPos ||
getSelectionStart() < lastOutputPos);
}
/**
* 当OutputWindow里面有Enter键按下时发生,表示用户输入了数据
*/
protected void inputTriggered()
{
int length = getDocument().getLength() - lastOutputPos;
int pos = lastOutputPos;
lastOutputPos = getDocument().getLength() + 1;//加上还没有记入文档的回车符
String input = null;
try
{
input = getDocument().getText(pos, length);
} catch (BadLocationException e)
{
e.printStackTrace();
}
if (output != null)
{
try
{
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(output));
out.write(input); //##如果是Compiler时敲回车会否出错?
out.close();
} catch (IOException e1)
{
e1.printStackTrace();
}
}
}
public void printResults(InputStream is)
{
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line = null;
try
{
while ((line = br.readLine()) != null)
{
print(line + "\r\n");
}
} catch (IOException e)
{
e.printStackTrace();
}
}
public void clearResults()
{
setText("");
}
/**
* @param string
*/
synchronized public void print(String string)
{
int pos = getDocument().getLength(); //不能用getText(),那不准确
setCaretPosition(pos);
replaceSelection(string);
lastOutputPos = getDocument().getLength();
}
public void setOutput(OutputStream os)
{
output = os;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -