首页 > 代码库 > Java记事本及网页源代码浏览器
Java记事本及网页源代码浏览器
1 /******************************************* 2 * 3 * 文本编辑器及网页源代码浏览器 4 * 5 * Author: Jintao 6 * E-mail:zjt1118@gmail.com 7 * Blog :www.zhaojintao.cn 8 * 9 ********************************************/ 10 import java.awt.BorderLayout; 11 import java.awt.Container; 12 import java.awt.FileDialog; 13 import java.awt.Frame; 14 import java.awt.Panel; 15 import java.awt.event.ActionEvent; 16 import java.awt.event.ActionListener; 17 import java.awt.event.InputEvent; 18 import java.awt.event.KeyEvent; 19 import java.io.BufferedReader; 20 import java.io.DataOutputStream; 21 import java.io.FileInputStream; 22 import java.io.FileOutputStream; 23 import java.io.IOException; 24 import java.io.InputStream; 25 import java.io.InputStreamReader; 26 import java.io.Reader; 27 import java.io.UnsupportedEncodingException; 28 import java.net.MalformedURLException; 29 import java.net.URL; 30 import java.nio.CharBuffer; 31 import java.sql.Savepoint; 32 33 import javax.swing.JButton; 34 import javax.swing.JFrame; 35 import javax.swing.JMenu; 36 import javax.swing.JMenuBar; 37 import javax.swing.JMenuItem; 38 import javax.swing.JPanel; 39 import javax.swing.JScrollPane; 40 import javax.swing.JTextArea; 41 import javax.swing.JTextField; 42 import javax.swing.KeyStroke; 43 44 45 public class Note extends JFrame{ 46 JMenuBar menub=new JMenuBar(); 47 JTextArea text=new JTextArea(); 48 JTextField urlText=new JTextField(); 49 JMenu files=new JMenu("文件"); 50 JMenu net=new JMenu("网络"); 51 //JMenu help=new JMenu("帮助"); 52 JMenuItem newfile=new JMenuItem("新建"); 53 JMenuItem open=new JMenuItem("打开"); 54 JMenuItem save=new JMenuItem("保存"); 55 JMenuItem saveas=new JMenuItem("另存为"); 56 JMenuItem about=new JMenuItem("关于记事本"); 57 JMenuItem url=new JMenuItem("转到"); 58 String name; 59 JFrame th=this; 60 String openedPath=null; 61 boolean opened=false; 62 boolean reworked=false; 63 64 //初始化窗体 65 Note(String name){ 66 super(name); 67 this.name=name; 68 setSize(600, 400); 69 } 70 //初始化 71 void init() 72 { 73 newfile.setMnemonic(‘N‘); 74 open.setMnemonic(‘O‘); 75 save.setMnemonic(‘S‘); 76 saveas.setMnemonic(‘A‘); 77 url.setMnemonic(‘\r‘); 78 //添加助记符 79 80 newfile.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,InputEvent.CTRL_MASK)); 81 open.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,InputEvent.CTRL_MASK)); 82 save.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK)); 83 saveas.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,InputEvent.CTRL_MASK)); 84 url.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,InputEvent.CTRL_MASK)); 85 //为控件添加快捷键 86 87 88 files.add(newfile); 89 files.add(open); 90 files.add(save); 91 files.add(saveas); 92 //help.add(about); 93 net.add(url); 94 menub.add(files); 95 menub.add(net); 96 setJMenuBar(menub); 97 98 Container p=getContentPane(); 99 p.setLayout(new BorderLayout());100 p.add(urlText,BorderLayout.NORTH);101 p.add(new JScrollPane(text),BorderLayout.CENTER);102 103 Listen listen=new Listen();104 newfile.addActionListener(listen);105 open.addActionListener(listen);106 save.addActionListener(listen);107 saveas.addActionListener(listen);108 url.addActionListener(listen);109 urlText.addActionListener(listen);110 111 }112 class Listen implements ActionListener113 {114 115 public void actionPerformed(ActionEvent e){116 // TODO Auto-generated method stub117 Object source=e.getSource();118 if (source==newfile) {119 text.setText("");120 th.setTitle(name);121 openedPath=null;122 opened=false;123 }else if (source==url||source==urlText){124 System.out.println("url");125 String ut= "http://"+urlText.getText();126 try {127 URL url=new URL(ut);128 InputStream is= url.openStream();129 InputStreamReader isr = new InputStreamReader(is,"utf-8");130 BufferedReader br=new BufferedReader(isr);131 String data=http://www.mamicode.com/br.readLine(); 132 text.setText("");133 while(data!=null)134 { 135 String t=text.getText();136 t+="\r\n";137 text.setText(t+data);138 data=http://www.mamicode.com/br.readLine();139 } 140 text.setCaretPosition(0);141 //System.out.println(content);142 } catch (MalformedURLException e1) {143 // TODO Auto-generated catch block144 e1.printStackTrace();145 } catch (IOException e1) {146 // TODO Auto-generated catch block147 e1.printStackTrace();148 }149 }else if (source==open) {150 FileDialog openFile=new FileDialog(th, "打开文件..", FileDialog.LOAD);151 openFile.setVisible(true);152 String filePath=openFile.getDirectory()+openFile.getFile();153 try {154 InputStreamReader inr =new InputStreamReader(new FileInputStream(filePath),"utf-8");155 FileInputStream fis=new FileInputStream(filePath);156 char[] content=new char[fis.available()];157 inr.read(content);158 //fis.read(content);159 text.setText(new String(content));160 text.setCaretPosition(0);161 if(openFile.getTitle()!=null)162 {163 th.setTitle(openFile.getFile()+name);164 openedPath=filePath;165 opened=true;166 }167 fis.close();168 inr.close();169 } catch (Exception e2) {170 // TODO: handle exception171 e2.printStackTrace();172 }173 174 175 }else if (source==save || source==saveas) 176 {177 String savePath=openedPath;178 if (savePath==null||source==saveas) {179 FileDialog saveFile=new FileDialog(th,"保存文件...",FileDialog.SAVE);180 saveFile.setVisible(true);181 savePath=saveFile.getDirectory()+saveFile.getFile();182 System.out.println(saveFile.getTitle());183 }184 try {185 DataOutputStream dos = new DataOutputStream(new FileOutputStream(savePath));186 //FileOutputStream fos=new FileOutputStream(savePath);187 //fos.write(text.getText().getBytes());188 //fos.close();189 dos.write(text.getText().getBytes("utf-8"));190 dos.close();191 } catch (Exception e2) {192 // TODO: handle exception193 e2.printStackTrace();194 }195 if(source==save)196 {197 openedPath=savePath;198 }199 }200 201 }202 203 }204 /**205 * @param args206 */207 public static void main(String[] args) {208 // TODO Auto-generated method stub209 String name="--涛哥牌记事本--V0.01--";210 Note note=new Note(name);211 note.init();212 note.setVisible(true);213 }214 215 }
Java记事本及网页源代码浏览器
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。