📄 myeditor.java
字号:
String strTemp = ta.getText();
PrintableTextArea tempArea = ta;
fr.remove(ta);
//if(menuAuto.getState())
//ta = new PrintableTextArea(strTemp,20,50,JTextArea.SCROLLBARS_BOTH);
// else ta = new PrintableTextArea(strtemp,20,50,JTextArea.SCROLLBARS_VERTICAL_ONLY);
// fr.add(ta);ta.repaint();
}
else if(ae.getSource() == menuViewFont) {
MenuFont mf = new MenuFont(this, true);
ta.setFont(mf.myLayout(ta.getFont()));
}
else if(ae.getSource() == menuViewColor) {
MenuColor mc = new MenuColor(this, true);
Color[] fbgc = new Color[2];
fbgc = mc.myLayout(ta.getForeground(), ta.getBackground());
ta.setForeground(fbgc[0]);
ta.setBackground(fbgc[1]);
ta.setCaretColor(fbgc[0]);
}
else if(ae.getSource() == menuViewClassic) {
if(menuViewClassic.getState()) {
defaultForeground = ta.getForeground();
defaultBackground = ta.getBackground();
defaultFont = ta.getFont();
defaultCaretColor = ta.getCaretColor();
ta.setForeground(new Color(0, 255, 0));
ta.setBackground(new Color(25, 25, 25));
ta.setFont(new Font("Serif", Font.BOLD, 16));
ta.setCaretColor(new Color(0, 255, 0));
}
else {
ta.setForeground(defaultForeground);
ta.setBackground(defaultBackground);
ta.setFont(defaultFont);
ta.setCaretColor(defaultCaretColor);
}
}
else if(ae.getSource() == menuViewStatus) {
if(menuViewStatus.getState())
{
showStatus();
}
}
else if(ae.getSource() == menuHelpAbout) {
JDialog dl = new JDialog(this, "关于 记事本", true);
dl.getContentPane().setLayout(new GridLayout(6,5,0,35));
Button bOk = new Button("确定");
bOk.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent bOke) {
dispose();
}
});
Label ver = new Label(" 迷你");
ver.setForeground(Color.blue);
Font font=new Font("Helvetica",Font.BOLD,20);
Font font1=new Font("Helvetica",Font.PLAIN,13);
ver.setFont(font);
Label null1 = new Label("版本5.1");
null1.setFont(font1);
Label null2 = new Label("版权所有(C) 2003 WorkGroup.");
null2.setFont(font1);
Label null3 = new Label();
Label null4 = new Label("记事本");
null4.setForeground(Color.blue);
null4.setFont(font1);
null4.setFont(font);
Label null5 = new Label("本作品作者:");
null5.setFont(font1);
Label null6 = new Label("吴峰");
null6.setFont(font1);
Label null7 = new Label("学号:");
null7.setFont(font1);
Label null8 = new Label("01171218");
null8.setFont(font1);
Label null9 = new Label();
Label null10 = new Label();
dl.getContentPane().add(ver);
dl.getContentPane().add(null4);
dl.getContentPane().add(null1);
dl.getContentPane().add(null3);
dl.getContentPane().add(null2);
dl.getContentPane().add(null9);
dl.getContentPane().add(null5);
dl.getContentPane().add(null6);
dl.getContentPane().add(null7);
dl.getContentPane().add(null8);
dl.getContentPane().add(null10);
dl.getContentPane().add(bOk);
bOk.addActionListener(this);
dl.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent dle) {
dispose();
}
});
dl.setLocation(220, 120);
dl.setResizable(false);
dl.setSize(400, 380);
dl.setVisible(true);
}
}//end of ActionListener
//DocumentListener
public void removeUpdate(DocumentEvent e) {
String s;
s = statusFile.getText();
if(!s.endsWith("*") & beginTextListener & !isNewFile) {
statusFile.setText("*");
}
menuEditUndo.setEnabled(true);
}
public void insertUpdate(DocumentEvent e) {
String s;
s = statusFile.getText();
if(!s.endsWith("*") & beginTextListener & !isNewFile) {
statusFile.setText("*");
}
menuEditUndo.setEnabled(true);
}
public void changedUpdate(DocumentEvent e) {
String s;
s = statusFile.getText();
if(!s.endsWith("*") & beginTextListener & !isNewFile) {
statusFile.setText("*");
}
menuEditUndo.setEnabled(true);
}//end of DocumentListener
// Caretlistener
public void caretUpdate(CaretEvent e) {
if(menuViewStatus.getState())
showStatus();
}// end of Caretlistener
// KeyListener
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == '\10') {
BACKSPACE = true;
}
}
public void keyReleased(KeyEvent e) {
if(e.getKeyCode() == 155) {
if(INSERTMODE)
INSERTMODE = false;
else
INSERTMODE = true;
}
if(menuViewStatus.getState())
showStatus();
}
public void keyTyped(KeyEvent e) {
beginTextListener = true;
isNewFile = false;
if(!BACKSPACE) {
if(!INSERTMODE) {
int pos = ta.getCaretPosition();
char c = ta.getText().charAt(pos);
if(c == '\12') {
}
else {
ta.replaceRange("", pos, pos + 1);
}
}
}
BACKSPACE = false;
}// end of KeyListener
void showStatus() {
int rows, cols, to;
rows = cols = 0;
to = ta.getCaretPosition();
String str = ta.getText();
cols = to - str.substring(0, to).lastIndexOf(10);
try {
rows = ta.getLineOfOffset(to) + 1;
} catch(BadLocationException ble) {
}
statusRow.setText("行数: " + rows);
statusCol.setText("列数: " + cols);
}
int wordLocation(String str, int pos, boolean isToRight) {
char c;
if(isToRight) {
try {
c = str.charAt(pos);
while(true) {
if(c == '\12')
return pos;
else if(c < 48)
return pos;
else if(c > 57 & c < 65)
return pos;
else if(c > 90 & c < 97)
return pos;
else if(c > 122)
return pos;
pos++;
c = str.charAt(pos);
}
} catch(Exception e) {
return pos--;
}
}
else {
try {
pos--;
c = str.charAt(pos);
while(true) {
if(c == '\12')
return pos++;
else if(c < 48)
return pos++;
else if(c > 57 & c < 65)
return pos++;
else if(c > 90 & c < 97)
return pos++;
else if(c > 122)
return pos++;
pos--;
c = str.charAt(pos);
}
}catch(Exception e) {
return pos++;
}
}
}
void setNewFile() {
ta.replaceRange("", 0, ta.getText().length());
fns = null;
undo.discardAllEdits();
menuEditUndo.setEnabled(false);
fr.setTitle("新建 文本文档.txt-记事本");
}
boolean saveNewFile() {
OutputStreamWriter osw;
Frame saveFileFrame = new Frame("Save file");
FileDialog fileDialog = new FileDialog(saveFileFrame);
fileDialog.setMode(FileDialog.SAVE);
fileDialog.setFile("*.txt;*.java");
fileDialog.show();
String file = fileDialog.getFile();
String directory = fileDialog.getDirectory();
if(file != null) {
fns = directory + file;
try {
osw = new OutputStreamWriter(
new BufferedOutputStream(
new FileOutputStream(fns)));
int pos = 0, t = 0;
String str = ta.getText();
while(true){
pos = str.indexOf('\12', pos);
if(pos == -1) break;
str = str.substring(0, pos) + '\15' + str.substring(pos);
pos = pos + 2;
}
osw.write(str, 0, str.length());
osw.close();
fr.setTitle("MiniEditor - [" + fns + "]");
} catch(IOException e) {
return false;
}
return true;
}
else {
return false;
}
}
boolean saveFile() {
OutputStreamWriter osw;
try {
osw = new OutputStreamWriter(
new BufferedOutputStream(
new FileOutputStream(fns)));
int pos = 0, t = 0;
String str = ta.getText();
while(true){
pos = str.indexOf('\12', pos);
if(pos == -1) break;
str = str.substring(0, pos) + '\15' + str.substring(pos);
pos = pos + 2;
}
osw.write(str, 0, str.length());
osw.close();
fr.setTitle("MiniEditor - [" + fns + "]");
} catch(IOException e) {
return false;
}
return true;
}
boolean openFile() {
String s = null;
StringBuffer strPool = new StringBuffer();
Frame openFileFrame = new Frame("Open file");
FileDialog fileDialog = new FileDialog(openFileFrame);
fileDialog.setMode(FileDialog.LOAD);
fileDialog.setFile("*.txt;*.java");
fileDialog.show();
String file = fileDialog.getFile();
String directory = fileDialog.getDirectory();
if(file != null) {
fns = directory + file;
BufferedReader br;
try {
br = new BufferedReader(new FileReader(fns));
s = br.readLine();
while(s != null) {
strPool.append(s + "\12");
s = br.readLine();
}
br.close();
ta.setText(strPool.toString());
} catch(IOException e) {
return false;
}
isNewFile = true;
undo.discardAllEdits();
menuEditUndo.setEnabled(false);
fr.setTitle("MiniEditor - [" + fns + "]");
return true;
}
else {
return false;
}
}
boolean openFileViaArgs(String fns) {
String s = null;
StringBuffer strPool = new StringBuffer();
BufferedReader br;
try {
br = new BufferedReader(new FileReader(fns));
s = br.readLine();
while(s != null) {
strPool.append(s + "\12");
s = br.readLine();
}
br.close();
ta.setText(strPool.toString());
isNewFile = true;
undo.discardAllEdits();
menuEditUndo.setEnabled(false);
fr.setTitle("MiniEditor - [" + fns + "]");
return true;
} catch(IOException e) {
return false;
}
}
void findInReplace(TextField tfro, Checkbox drMatchCase) {
int a = 0, b = 0;
String str1, str2, str3, str4, strA, strB;
str1 = ta.getText();
str2 = str1.toLowerCase();
str3 = tfro.getText();
str4 = str3.toLowerCase();
if(drMatchCase.getState()) {
strA = str1;
strB = str3;
}
else {
strA = str2;
strB = str4;
}
a = strA.indexOf(strB, FindStartPos);
if(a > -1) {
ta.setCaretPosition(a);
b = tfro.getText().length();
ta.select(a, a + b);
FindStartPos = a + b;
foundCount++;
}
else {
JOptionPane.showMessageDialog(null, "End of file.", "结果", JOptionPane.INFORMATION_MESSAGE);
foundCount = 0;
}
}
class UndoHandler implements UndoableEditListener {
public void undoableEditHappened(UndoableEditEvent uee) {
undo.addEdit(uee.getEdit());
}
}
}//end of class MiniEditor
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -