📄 minieditor.java
字号:
ta.select(a, a + b);
FindStartPos = a + b;
foundCount++;
}
else {
JOptionPane.showMessageDialog(null, "End of file.", "Result",1);
foundCount = 0;
}
if(tfrn.getText().length() == 0 && ta.getSelectedText() != null)
ta.replaceSelection("");
if(tfrn.getText().length() > 0 && ta.getSelectedText() != null)
ta.replaceSelection(tfrn.getText());
}
}
});
Button brc = new Button("Cancel");
brc.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent brce) {
dispose();
}
});
p2.add(brf);
p2.add(brr);
p2.add(brra);
p2.add(brc);
dr.setResizable(false);
dr.setLocation(120, 120);
dr.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
FindStartPos = 0;
}
});
dr.setSize(220,138);
dr.setVisible(true);
}
else if(ae.getSource() == menuEditGoTo) {
JDialog dg = new JDialog(this, "Line Number", true);
dg.getContentPane().setLayout(new FlowLayout());
final JTextField dgtf = new JTextField(4);
Button dgOk = new Button("Go To");
dgOk.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent brce) {
int totalLine = ta.getLineCount();
int[] lineNumber = new int[totalLine + 1];
String s = ta.getText();
int pos = 0, t = 0;
while(true) {
// pos = s.indexOf(?\12?, pos);
pos=s.indexOf('\12',pos);
if(pos == -1)
break;
lineNumber[t++] = pos++;
}
int gt = 1;
try {
gt = Integer.parseInt(dgtf.getText());
} catch(NumberFormatException efe) {
JOptionPane.showMessageDialog(null, "Please enter an integer.", "Input",1);
}
if(gt < 2 || gt >= totalLine) {
if(gt < 2)
ta.setCaretPosition(0);
else
ta.setCaretPosition(s.length());
}
else
ta.setCaretPosition(lineNumber[gt-2] + 1);
dispose();
}
});
Button dgCancel = new Button("Cancel");
dgCancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent brce) {
dispose();
}
});
dg.getContentPane().add(dgtf);
dg.getContentPane().add(dgOk);
dg.getContentPane().add(dgCancel);
dg.setResizable(false);
dg.setLocation(120, 120);
dg.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
}
});
dg.setSize(180,60);
dg.setVisible(true);
}
else if(ae.getSource() == menuEditSelectAll) {
ta.selectAll();
}
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()) {
ta.setForeground(new Color(0, 255, 0));
ta.setBackground(new Color(45, 0, 45));
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() == menuViewWordWrap) {
if(menuViewWordWrap.getState()) {
ta.setLineWrap(true);
}
else {
ta.setLineWrap(false);
}
}
else if(ae.getSource() == menuViewDoubleSpace) {
int pos = 0;
String str = ta.getText();
while(true){
pos = str.indexOf('\15',pos);
if(pos == -1) break;
// str = str.substring(0, pos) + '\15'+'\12' + str.substring(pos);
str=str.substring(0,pos)+'\15'+'\12'+str.substring(pos);
pos = pos + 3;
}
ta.setText(str);
}
else if(ae.getSource() == menuHelpAbout) {
dl = new JDialog(this,"About MiniEditor", true);
dl.getContentPane().setLayout(new GridLayout(3,3));
dl.setBackground(new Color(212,208,200));
Button bOk = new Button("OK");
bOk.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent bOke) {
dispose();
}
});
Label ver = new Label("Version .9");
Label null1 = new Label();
Label null2 = new Label();
Label null3 = new Label();
Label null4 = new Label();
Label null5 = new Label();
Label null6 = new Label();
Label null7 = new Label();
dl.getContentPane().add(null1);
dl.getContentPane().add(ver);
dl.getContentPane().add(null2);
dl.getContentPane().add(null3);
dl.getContentPane().add(bOk);
dl.getContentPane().add(null4);
dl.getContentPane().add(null5);
dl.getContentPane().add(null6);
dl.getContentPane().add(null7);
bOk.addActionListener(this);
dl.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
}
});
dl.setLocation(120, 120);
dl.setResizable(false);
dl.setSize(200,80);
dl.setVisible(true);
}
}//end of ActionListener
// DocumentListener
public void removeUpdate(DocumentEvent e) {
String s;
s = fileStatus.getText();
if(!s.endsWith("*") & beginTextListener & !isNewFile) {
fileStatus.setText("*");
}
menuEditUndo.setEnabled(true);
}
public void insertUpdate(DocumentEvent e) {
String s;
s = fileStatus.getText();
if(!s.endsWith("*") & beginTextListener & !isNewFile) {
fileStatus.setText("*");
}
menuEditUndo.setEnabled(true);
}
public void changedUpdate(DocumentEvent e) {
String s;
s = fileStatus.getText();
if(!s.endsWith("*") & beginTextListener & !isNewFile) {
fileStatus.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;
}
// if(menuViewStatus.getState())
// showStatus();
}
public void keyReleased(KeyEvent e) {
if(e.getKeyCode() == 155) { //ESCAPE = 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 if(c =='\15') {
}
else {
ta.replaceRange("", pos, pos + 1);
}
}
}
BACKSPACE = false;
}// end of KeyListener
void showStatus() {
int rows, cols, to, fileSize;
rows = cols = 0;
to = ta.getCaretPosition();
fileSize = 0;
String str = ta.getText();
cols = to - str.substring(0, to).lastIndexOf(10);
fileSize = str.length();
String mode;
if(INSERTMODE) {
mode = "INSERT";
}
else {
mode = "OVERLAY";
}
try {
rows = ta.getLineOfOffset(to) + 1;
} catch(BadLocationException ble) {
}
statusRow.setText("row: " + rows);
statusCol.setText("col: " + cols);
statusMode.setText("mode: " + mode);
statusSize.setText("size: " + fileSize);
// fileStatus.setText("file status: ");
}
int wordLocation(String str, int pos, boolean isToRight) {
char c;
if(isToRight) {
c = str.charAt(pos);
while(true) {
if(c < 48) break;
else if(c > 57 & c < 65) break;
else if(c > 90 & c < 97) break;
else if(c > 122) break;
pos++;
c = str.charAt(pos);
}
return pos--;
}
else {
pos--;
c = str.charAt(pos);
while(true) {
if(c < 48) break;
else if(c > 57 & c < 65) break;
else if(c > 90 & c < 97) break;
else if(c > 122) break;
pos--;
c = str.charAt(pos);
}
return pos++;
}
}
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 + -