?? 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 + -