📄 t_editor.java
字号:
++window_counter;++window_no;st=Integer.toString(window_no);Frame_class f=new Frame_class("<< Text Editor 1.0>> By << Anuj Agrawal >> : New Window "+st);f.setSize(700,600);}private void newWindow(){fileName=null;tx1.setText("");tx1.requestFocus();} private boolean lentest(){s=tx1.getText();int len=s.length();if(len>0)return (true);else return(false);}private void closedFile(){if(lentest()){ saveOpenedFile(); newWindow();String ar; if(window_no>=1){ar="New Window "+st+" : ";}else ar=""; this.setTitle("<< Text Editor 1.0>> By << Anuj Agrawal >> : "+ar+"*.*"); }else newWindow();} /* public void keyPressed(KeyEvent ke){ int key=ke.getKeyCode(); switch(key){ case KeyEvent.VK_F1:openFile();break; } }*///////////////////////////////////////////////////////////////////////////////// // public void keyReleased(KeyEvent ke){}// public void keyTyped(KeyEvent ke){} // The Dialog boxes for openinig and saving file private void AddDialog(){ openDialog =new FileDialog (this,"Open File",FileDialog.LOAD); saveDialog=new FileDialog (this,"Save File",FileDialog.SAVE); } //cuts selected textand places it on the buffer public void cut(){ copystring =tx1.getSelectedText(); tx1.replaceText("",tx1.getSelectionStart(),tx1.getSelectionEnd()); tx1.requestFocus(); } //copies selected text to buffer public void copy(){ copystring=tx1.getSelectedText(); tx1.requestFocus(); } //paste text from buffer to the screen public void paste(){ if(copystring.length()>0) tx1.insertText(copystring,tx1.getSelectionStart()); tx1.requestFocus(); } private void openFile(){ String filename; openDialog.setVisible(true); filename=openDialog.getFile(); if(filename!=null){ filename=check(filename); if(read(filename)) { fileName=filename;String ar; if(window_no>=1){ar="New Window "+st+" : ";}else ar=""; this.setTitle("<< Text Editor 1.0>> By << Anuj Agrawal >> : "+ar+fileName); } } tx1.requestFocus(); } private boolean read(String filename){String line;FileInputStream in = null;DataInputStream dataIn =null;BufferedInputStream bis=null;StringBuffer buffer=new StringBuffer();try{ in =new FileInputStream(filename); bis=new BufferedInputStream(in); dataIn=new DataInputStream(bis); } catch(Throwable e){ showErrorDialog("Can't open\""+filename+"\""); return (false); } try{ while((line=dataIn.readLine())!=null){ buffer.append(line+"\n"); } in.close(); tx1.setText(buffer.toString()); }catch(IOException e){ showErrorDialog("Can't read \""+filename+"\""); return(false); } return(true); } private String check(String filename){ if(filename.endsWith(".*.*")){ filename=filename.substring(0,filename.length()-4); } return(filename); } //method saves a file you opened previously //method produces error if you did not open a file previously public void saveOpenedFile() { if(!lentest()) showErrorDialog("There is no <<Text>> to Save");else if(fileName==null) { saveAsFile(this); //showErrorDialog("You did not previously open a file.Use save as."); } else { write(fileName); tx1.requestFocus(); } } //methods save as the file you are currently editingprivate void saveAsFile(Frame_class f1){ if(!lentest()) showErrorDialog("There is no <<Text>> to Save");else { String filename; saveDialog.setVisible(true); filename=saveDialog.getFile(); if(filename!=null){ filename=check(filename); if(write(filename)) { fileName=filename; f1.setTitle("<< Text Editor 1.0>> By << Anuj Agrawal >> : "+heading+" : "+fileName+" "+st); } } tx1.requestFocus(); } } //method handles writing files to the file systemprivate boolean write (String filename){ FileOutputStream os=null; try{ os=new FileOutputStream(filename); }catch(Throwable e){ showErrorDialog("Can't write\""+filename+"\""); return(false); } try{ String s=tx1.getText(); int len=s.length(); for(int i=0;i<len;i++){ os.write(s.charAt(i)); } os.close(); } catch(IOException e){ showErrorDialog("can't write\""+filename+"\""); return(false); } return(true); } //DialogBoxes....... public void showErrorDialog(String message){ if(errorDialog!=null)errorDialog.dispose(); errorDialog=new ErrorDialog(this,message); errorDialog.setVisible(true); } public void showTEDialog(String message){ if(teDialog!=null)teDialog.dispose(); teDialog=new TEDialog(this,message); teDialog.setVisible(true); } }//close brace of Frame_class////////////////////////////////////////////////////////////////////////////////class ErrorDialog extends Dialog{Frame_class parent;String message;public ErrorDialog(Frame_class parent,String message){super(parent,"Error",true);setBackground(Color.yellow);this.parent=parent;this.message=message;Panel p;p=new Panel();p.add(new Label(message));p.setFont(new Font("System",Font.BOLD,12));add("Center",p);Dimension d;d=parent.size();reshape(200,50,420,100);setResizable(false);}public boolean handleEvent(Event event){switch(event.id){case Event.WINDOW_DESTROY:dispose();parent.tx1.requestFocus();return(true);}return(false);}}class TEDialog extends Dialog{Frame_class parent;String message;public TEDialog(Frame_class parent,String message){super(parent,"TextEditor 1.0",true);setBackground(Color.yellow);this.parent=parent;this.message=message;TextArea p;p=new TextArea();p.setText(message);p.setFont(new Font("System",Font.BOLD,12));add("Center",p);p.setEditable(false);Dimension d;d=parent.size();reshape(200,50,500,300);//setResizable(false);}public boolean handleEvent(Event event){switch(event.id){case Event.WINDOW_DESTROY:dispose();parent.tx1.requestFocus();return(true);}return(false);}}/////////////////////////////////////////////////////////////////////////////////class MyWindowAdapter extends WindowAdapter{// MyWindowAdapter class Frame_class fc1; public MyWindowAdapter(Frame_class fc1){ this.fc1=fc1; } public void windowClosing(WindowEvent we){ if(Frame_class.window_counter==0) System.exit(0); else fc1.setVisible(false); -- Frame_class.window_counter; } }//end MyWindowAdapter class///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////main() function class ///////////////////////////////////////////////////////////////////////////////// class T_Editor { //open brace of T_Editor classpublic static void main(String args[]){ //open brace of main() fun.Frame f1=new Frame_class("<< Text Editor 1.0>> By << Anuj Agrawal >>");//f1.setVisible(true);Dimension d;d=Toolkit.getDefaultToolkit().getScreenSize();f1.setSize(d.width,d.height);}//close brace of main() fun.}//close brace of T_Editor class/////////////////////////////////////////////////////////////////////////////////
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -