📄 edit.java
字号:
String s = null;
StringBuffer strPool = new StringBuffer();
Frame openFileFrame = new Frame("打开");
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));
TextArea ta1;
s = br.readLine();
while(s != null)
{
strPool.append(s + "\r\n");
s = br.readLine();
}
br.close();
editor.setText(strPool.toString());
flag=false;
} catch(IOException e){}
undo.discardAllEdits();
mEditUndo.setEnabled(false);
frame.setTitle("[" + fns + "]"+"-Java 编辑器");
javaview(0,Edit.doc.getLength());
}
}
void saveFile(boolean saveasfile)
{
if (saveasfile)
{
Frame saveFileFrame = new Frame("保存");
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;
else
return;
}
OutputStreamWriter osw;
if (fns==null)
{
saveFile(true);
if (fns==null)return;
}
try
{
osw = new OutputStreamWriter(new BufferedOutputStream(
new FileOutputStream(fns)));
osw.write(editor.getText());
osw.close();
flag=false;
frame.setTitle("[" + fns + "]"+"-Java 编辑器");
} catch(IOException e)
{
}
}
// KeyListener
public void keyPressed(KeyEvent e) {}
public void keyReleased(KeyEvent e)
{
if (Edit.javaviewflag && javaview){
int where = editor.getCaretPosition();
int bak=where;
int leng1=0,leng2=doc.getLength();
String str;
try
{
for(where--;where>-1;where--)
{
if(where==0)leng1=0;
str=editor.getText(where, 1);
if(javakey.indexOf(str)>-1)leng1=where;
}
for(where=bak;where == doc.getLength();where++)
{
if(where==doc.getLength())leng2=doc.getLength();
str=editor.getText(where, 1);
if(javakey.indexOf(str)>-1)leng2=where;
}
}
catch(Exception Iee){}
javaview(leng1-1,leng2+1);
editor.setCaretPosition(bak);
Edit.javaviewflag=false;
}
}
public void keyTyped(KeyEvent e) {}
// end of KeyListener
//DocumentListener
public void removeUpdate(DocumentEvent e){}
public void insertUpdate(DocumentEvent e)
{
flag=true;
mEditUndo.setEnabled(true);
Edit.javaviewflag=true;
}
public void changedUpdate(DocumentEvent e){ }
//end of DocumentListener
public void findowrd()
{
final JDialog ds = new JDialog(frame, "查询", true);
ds.getContentPane().setLayout(new FlowLayout());
ds.setResizable(false);
final JLabel dsMessage1 = new JLabel(" 找到个数: ");
final JLabel dsMessage2 = new JLabel(" 0");
final Checkbox dsLoop = new Checkbox("循环");
dsLoop.setState(findingLoop);
final Checkbox dsMatchCase = new Checkbox("区分大小写");
final TextField tfs = new TextField(15);
ds.getContentPane().add(tfs);
Button bs = new Button(" 查找 ");
bs.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
int a, b;
b = tfs.getText().length();
String str1,str2;
for(a=FindStartPos;a+b<doc.getLength();a++)
{
try
{
str1=editor.getText(a, b);
str2=tfs.getText();
if(dsMatchCase.getState())
{ if(str1.equals(str2))
{
editor.select(a, a+b);
foundCount++;break;
}
}
else
{ if(str1.equalsIgnoreCase(str2))
{
editor.select(a,a+ b);
foundCount++;break;
}
}
}
catch(Exception Ie){}
}
FindStartPos = a + b;
dsMessage2.setText(foundCount + "");
if(FindStartPos>doc.getLength())
{
if(dsLoop.getState())
{
JOptionPane.showMessageDialog(null, "文件末尾.", "查找结果", JOptionPane.INFORMATION_MESSAGE);
FindStartPos = 0;
}
else
{
JOptionPane.showMessageDialog(null, "文件末尾.", "查找结果", JOptionPane.INFORMATION_MESSAGE);
}
foundCount = 0;
}
}
});
Button bsc = new Button("取消");
bsc.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
foundCount = 0;
ds.setVisible(false);
}
});
ds.getContentPane().add(bs);
ds.getContentPane().add(bsc);
ds.getContentPane().add(dsLoop);
ds.getContentPane().add(dsMatchCase);
ds.getContentPane().add(dsMessage1);
ds.getContentPane().add(dsMessage2);
ds.setLocation(120, 120);
ds.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
FindStartPos = 0;
ds.setVisible(false);
}
});
ds.setSize(260,110);
ds.setVisible(true);
}
public void replaceword()
{
final JDialog dr = new JDialog(frame, "替换", true);
dr.getContentPane().setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 2;
gbc.gridheight =1;
gbc.fill = gbc.NONE;
gbc.anchor = gbc.CENTER;
Panel p1 = new Panel();
dr.getContentPane().add(p1);
gbc.gridx = 2;
gbc.gridy = 0;
gbc.gridwidth = 1;
gbc.gridheight =1;
Panel p2 = new Panel();
p1.setLayout(new GridLayout(5, 1));
p2.setLayout(new GridLayout(4, 1));
dr.getContentPane().add(p2);
JLabel drMessage1 = new JLabel("查找内容: ");
JLabel drMessage2 = new JLabel("替换为: ");
final Checkbox drMatchCase = new Checkbox("区分大小写");
final TextField tfro = new TextField(15);
tfro.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent tfroe)
{
findInReplace(tfro, drMatchCase);
}
});
final TextField tfrn = new TextField(15);
tfrn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent tfrne)
{
findInReplace(tfro, drMatchCase);
}
});
p1.add(drMessage1);
p1.add(tfro);
p1.add(drMessage2);
p1.add(tfrn);
p1.add(drMatchCase);
Button brf = new Button("查找");
brf.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent brfe)
{
findInReplace(tfro, drMatchCase);
}
});
Button brr = new Button("替换");
brr.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent brre)
{
if(tfrn.getText().length() == 0 && editor.getSelectedText() != null)
editor.replaceSelection("");
if(tfrn.getText().length() > 0 && editor.getSelectedText() != null)
editor.replaceSelection(tfrn.getText());
findInReplace(tfro, drMatchCase);
}
});
Button brra = new Button("全部替换");
brra.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent brrae)
{
while(FindStartPos<doc.getLength())
{
findInReplace(tfro, drMatchCase);
if(tfrn.getText().length() == 0 && editor.getSelectedText() != null)
editor.replaceSelection("");
if(tfrn.getText().length() > 0 && editor.getSelectedText() != null)
editor.replaceSelection(tfrn.getText());
}
}
});
Button brc = new Button("取消");
brc.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent brce)
{
dr.setVisible(false);
}
});
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)
{
dr.setVisible(false);
FindStartPos = 0;
}
});
dr.setSize(230, 138);
dr.setVisible(true);
}
void findInReplace(TextField tfro, Checkbox drMatchCase)
{
int a, b;
b = tfro.getText().length();
String str1,str2;
for(a=FindStartPos;a+b<doc.getLength();a++)
{
try
{
str1=editor.getText(a, b);
str2=tfro.getText();
if(drMatchCase.getState())
{ if(str1.equals(str2))
{
editor.select(a, a+b);
foundCount++;break;
}
}
else
{ if(str1.equalsIgnoreCase(str2))
{
editor.select(a,a+ b);
foundCount++;break;
}
}
}
catch(Exception Ie){}
}
FindStartPos = a + b;
if(FindStartPos>doc.getLength())
{
JOptionPane.showMessageDialog(null, "文件末尾.", "查找结果", JOptionPane.INFORMATION_MESSAGE);
foundCount = 0;
}
}
public void mousePressed(MouseEvent mep){}
public void mouseReleased(MouseEvent mer){}
public void mouseEntered(MouseEvent mee){}
public void mouseExited(MouseEvent mex){}
public void mouseDragged(MouseEvent med){}
public void mouseClicked(MouseEvent mec)
{
if(mec.getModifiers()==mec.BUTTON3_DOWN_MASK)
popmenu.show(editor,mec.getX(),mec.getY());
}
}
class Autoup extends Thread
{
OutputStreamWriter osw;
boolean saved;
public Autoup()
{
saved=false;
}
public void run()
{
while(true)
{
if (!saved && Edit.fns!=null)
{
try
{
osw = new OutputStreamWriter(new BufferedOutputStream(
new FileOutputStream(Edit.fns+".bak")));
saved=true;
int pos = 0, t = 0;
String str = Edit.editor.getText();
osw.write(str, 0, str.length());
osw.close();
} catch(IOException e)
{
}
}
try
{
sleep(1000*60*5);//自动更新时间毫秒(5分钟)
}
catch(InterruptedException e)
{
}
if (Edit.autoup && Edit.flag)
{
try
{
if (Edit.fns==null)
osw = new OutputStreamWriter(new BufferedOutputStream(
new FileOutputStream("未命名.java")));
else
osw = new OutputStreamWriter(new BufferedOutputStream(
new FileOutputStream(Edit.fns)));
int pos = 0, t = 0;
String str = Edit.editor.getText();
osw.write(str, 0, str.length());
osw.close();
} catch(IOException e)
{
}
}
}
}
}
class PopupListener extends MouseAdapter {
JPopupMenu popup;
PopupListener(JPopupMenu popupMenu) {
popup = popupMenu;
}
public void mousePressed(MouseEvent e) {
maybeShowPopup(e);
}
public void mouseReleased(MouseEvent e) {
maybeShowPopup(e);
}
private void maybeShowPopup(MouseEvent e) {
if (e.isPopupTrigger()) {
popup.show(e.getComponent(),
e.getX(), e.getY());
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -