±àÒë²¢ÔËÐÐUseStream1.class¡£
3.ʹÓÃJFrame»òFrame´°ÌåµÈGUI±à³Ì£¬ÊµÏÖÒ»¸ö¼Çʱ¾³ÌÐò£¬ÊµÏÖÎļþµÄ´ò¿ª¡¢±à¼¡¢±£´æ£»Îı¾µÄȫѡ¡¢¸´ÖÆ¡¢¼ôÇС¢Õ³ÌùµÈ¹¦ÄÜ¡£
import java.awt.*;
import java.awt.event.*; import java.io.*;
import java.awt.datatransfer.*; class MyMenuBar extends MenuBar{ public MyMenuBar(Frame parent){ parent.setMenuBar(this); }
public void addMenus(String [] menus){ for(int i=0;i public void addMenuItems(int menuNumber,String[] items){ for(int i=0;i getMenu(menuNumber).add(new MenuItem(items[i])); else getMenu(menuNumber).addSeparator(); } } public void addActionListener(ActionListener al){ for(int i=0;i for(int j=0;j getMenu(i).getItem(j).addActionListener(al); } } class MyFile{ private FileDialog fDlg; public MyFile(Frame parent){ fDlg=new FileDialog(parent,\,FileDialog.LOAD); } private String getPath(){ return fDlg.getDirectory()+\+fDlg.getFile(); } public String getData() throws IOException{ fDlg.setTitle(\´ò¿ª\); fDlg.setMode(FileDialog.LOAD); fDlg.setVisible(true); BufferedReader br=new BufferedReader(new FileReader(getPath())); StringBuffer sb=new StringBuffer(); String aline; while((aline=br.readLine())!=null) sb.append(aline+'\\n'); br.close(); return sb.toString(); } public void setData(String data) throws IOException{ fDlg.setTitle(\±£´æ\); fDlg.setMode(FileDialog.SAVE); fDlg.setVisible(true); BufferedWriter bw=new BufferedWriter(new FileWriter(getPath())); bw.write(data); bw.close(); } } class MyClipboard{ private Clipboard cb; public MyClipboard(){ cb=Toolkit.getDefaultToolkit().getSystemClipboard(); } public void setData(String data){ cb.setContents(new StringSelection(data),null); } public String getData(){ Transferable content=cb.getContents(null); try{ return (String) content.getTransferData(DataFlavor.stringFlavor); }catch(Exception ue){} return null; } } class MyFindDialog extends Dialog implements ActionListener{ private Label lFind=new Label(\²éÕÒ×Ö·û´®\); private Label lReplace=new Label(\Ìæ»»×Ö·û´®\); private TextField tFind=new TextField(10); private TextField tReplace=new TextField(10); private Button bFind=new Button(\²éÕÒ\); private Button bReplace=new Button(\Ìæ»»\); private TextArea ta; public MyFindDialog(Frame owner,TextArea ta){ super(owner,\²éÕÒ\,false); this.ta=ta; setLayout(null); lFind.setBounds(10,30,80,20); lReplace.setBounds(10,70,80,20); tFind.setBounds(90,30,90,20); tReplace.setBounds(90,70,90,20); bFind.setBounds(190,30,80,20); bReplace.setBounds(190,70,80,20); add(lFind); add(tFind); add(bFind); add(lReplace); add(tReplace); add(bReplace); setResizable(false); bFind.addActionListener(this); bReplace.addActionListener(this); addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ MyFindDialog.this.dispose(); } }); } public void showFind(){ setTitle(\²éÕÒ\); setSize(280,60); setVisible(true); } public void showReplace(){ setTitle(\²éÕÒÌæ»»\); setSize(280,110); setVisible(true); } private void find(){ String text=ta.getText(); String str=tFind.getText(); int end=text.length(); int len=str.length(); int start=ta.getSelectionEnd(); if(start==end) start=0; for(;start<=end-len;start++){ if(text.substring(start,start+len).equals(str)){ ta.setSelectionStart(start); ta.setSelectionEnd(start+len); return; } } ta.setSelectionStart(end); ta.setSelectionEnd(end); } public Button getBFind() { return bFind; } private void replace(){ String str=tReplace.getText(); if(ta.getSelectedText().equals(tFind.getText())) ta.replaceRange(str,ta.getSelectionStart(),ta.getSelectionEnd()); else find(); } public void actionPerformed(ActionEvent e) { if(e.getSource()==bFind) find(); else if(e.getSource()==bReplace) replace(); } } public class search_9_Jishiben extends Frame implements ActionListener{ private TextArea editor=new TextArea(); //¿É±à¼µÄTextArea private MyFile mf=new MyFile(this);//MyFile¶ÔÏó private MyClipboard cb=new MyClipboard(); private MyFindDialog findDlg=new MyFindDialog(this,editor); public search_9_Jishiben(String title){ //¹¹Ô캯Êý super(title); MyMenuBar mb=new MyMenuBar(this); //Ìí¼ÓÐèÒªµÄ²Ëµ¥¼°²Ëµ¥Ïî mb.addMenus(new String[]{\Îļþ\,\±à¼\,\²éÕÒ\,\°ïÖú\}); mb.addMenuItems(0,new String[]{\н¨\,\´ò¿ª\,\±£´æ\,null,\ȫѡ\}); mb.addMenuItems(1,new String[]{\¼ôÌù\,\¸´ÖÆ\,\Õ³Ìù\,\Çå³ý\,null,\ȫѡ\}); mb.addMenuItems(2,new String[]{\²éÕÒ\,null,\²éÕÒÌæ»»\}); mb.addMenuItems(3,new String[]{\ÎҵļÇʱ¾ÐÅÏ¢\}); add(editor); //Ϊ²Ëµ¥Ïî×¢²á¶¯×÷ʱ¼ä¼àÌýÆ÷ mb.addActionListener(this); addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ search_9_Jishiben.this.dispose(); } }); //·ÖºÅ²»ÄÜÍüÁË } //¹¹Ô캯ÊýÍê public void actionPerformed(ActionEvent e){ String selected=e.getActionCommand(); //»ñÈ¡²Ëµ¥Ïî±êÌâ if(selected.equals(\н¨\)) editor.setText(\); else if(selected.equals(\´ò¿ª\)){ try{ editor.setText(mf.getData()); }catch(IOException ie){} } else if(selected.equals(\±£´æ\)){ try{ mf.setData(editor.getText()); }catch(IOException ie){} } else if(selected.equals(\Í˳ö\)){ dispose(); } else if(selected.equals(\¼ôÌù\)){ //½«Ñ¡ÖеÄ×Ö·û´®¸´ÖƵ½¼ôÌù°åÖв¢Çå³ý×Ö·û´® cb.setData(editor.getSelectedText()); editor.replaceRange(\,editor.getSelectionStart(),editor.getSelectionEnd()); } else if(selected.equals(\¸´ÖÆ\)){ cb.setData(editor.getSelectedText()); } else if(selected.equals(\Õ³Ìù\)){ String str=cb.getData(); editor.replaceRange(str,editor.getSelectionStart(),editor.getSelectionEnd()); //Õ³ÌùÔÚ¹â±êλÖà } else if(selected.equals(\Çå³ý\)){ editor.replaceRange(\,editor.getSelectionStart(),editor.getSelectionEnd()); } else if(selected.equals(\ȫѡ\)){ editor.setSelectionStart(0); editor.setSelectionEnd(editor.getText().length()); } else if(selected.equals(\²éÕÒ\)){ findDlg.showFind(); } else if(selected.equals(\²éÕÒÌæ»»\)){ findDlg.showReplace(); } } public static void main(String[] args){ search_9_Jishiben memo=new search_9_Jishiben(\¼Çʱ¾\); memo.setSize(650,450); memo.setVisible(true); } } ÔËÐнá¹û£º Îå¡¢×¢ÒâÊÂÏî ¢±ÈÏÕæÌîдʵÑ鱨¸æ ¢²×ñÊØÊµÑéÊÒ¸÷ÏîÖÆ¶È£¬·þ´ÓʵÑéÖ¸µ¼½ÌʦµÄ°²ÅÅ ¢³°´¹æ¶¨µÄʱ¼äÍê³ÉʵÑé Áù¡¢ËµÃ÷ ¢±½¨ÒéѧʱÊý4ѧʱ Æß¡¢ÊµÑé×ܽáÓëÌå»á Õâ¸öʵÑéÈÃÎÒÃÇÁ˽âÎļþµÄ¸ÅÄîºÍÎļþ¶ÔÏóµÄ´´½¨·½·¨¡¢Á˽âFileInputStreamºÍFileOutoutStreamµÄ»ù±¾¸ÅÄͨ¹ýÕâЩµÄѧϰʵ¼ùѧ»áÁË´´½¨ÎļþÊäÈëÊä³öÁ÷£¬ÕÆÎÕÁËʹÓÃÎļþÊäÈëÊä³öÁ÷¶ÁдÎļþµÄ·½·¨¡£Ò²½øÒ»²½µÄѵÁ·Á˲˵¥µÄÖÆ×÷£¬×ÓÏîµÄÌí¼Ó£¬´ÓÖеõ½ÁË¿ìÀÖ£¬Îò³öjavaÆäʵºÜ¼òµ¥£¬¶¼ÊÇÓÐÕ¿Éѵ쬾¡¹ÜËûµÄº¯ÊýºÜ¶à£¬µ«¶¼ÊÇÒ»Ò»Ïà¹ØµÄ¡£ ʵÑéÊ®¡¢Êý¾Ý¿âÓ¦ÓÃʵÑé Ò»¡¢ÊµÑéÄ¿µÄ ¢±ÕÆÎÕJavaÓ¦ÓóÌÐòÁ¬½ÓÊý¾Ý¿â ¢²ÕÆÎÕJavaСӦÓóÌÐòÁ¬½ÓÊý¾Ý¿â ¶þ¡¢Ô¤Ï°ÄÚÈÝ Êý¾Ý¿â±íµÄÓ¦Óà Èý¡¢ÊµÑéÉ豸Óë»·¾³ ×°ÓÐJAVAÓïÑÔ¹¤¾ßÈí¼þ (Eclipse )µÄ΢»úÈô¸É ËÄ¡¢ÊµÑéÄÚÈÝ ¢±Íê³ÉÒ»¸öϵͳ£ºÊµÏÖGUIµÄѧÉú³É¼¨¹ÜÀí 1£© ÔÚÊý¾Ý¿â´´½¨±í£ºÑ§Éú±í£¨Ñ§ºÅ¡¢ÐÕÃû¡¢ÐԱ𡢳öÉúÄêÔ£©¡¢¿Î³Ì±í£¨¿Î³ÌºÅ¡¢¿Î³ÌÃû£©¡¢³É¼¨±í£¨Ñ§ºÅ¡¢¿Î³Ì ºÅ£¬³É¼¨£© 2£© ʵÏÖÓû§µÇ½¹¦ÄÜ 3£© ÔÚÖ÷´°¿ÚʵÏÖѧÉúÐÅÏ¢¹ÜÀí¡¢¿Î³ÌÐÅÏ¢¹ÜÀí¡¢³É¼¨Â¼ÈëÐ޸IJéѯºÍÌá½»¹¦ÄÜ¡¢Ñ§Éú³É¼¨µÄ·ÖÎö£¨Í³¼Æ¸÷¸ö·ÖÊý ¶ÎµÄÈËÊý£¬²¢»³öͳ¼ÆÍ¼£© ³ÌÐòʵÏÖ£º //Óû§µÇ¼ import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.sql.*; public class User extends JFrame{ private JLabel use,password; private JTextField k1;//Óû§ÃûÊäÈë¿ò private JPasswordField k2;//ÃÜÂëÊäÈë¿ò private JButton b1,b2; //µÇ¼´°¿Ú public User(JFrame f){ super(\ϵͳµÇ¼\); Container c=getContentPane(); c.setLayout(new FlowLayout()); use=new JLabel(\ÕË»§\); use.setFont(new Font(\,Font.PLAIN,20)); password=new JLabel(\ÃÜÂë\); password.setFont(new Font(\,Font.PLAIN,20)); k1=new JTextField(14); k2=new JPasswordField(14); b1=new JButton(\µÇ¼\); b2=new JButton(\Í˳ö\); // ÉèÖõǼ·½·¨