java简易编辑器
1 package peng_jun; 2 3 import java.awt.*; 4 import java.awt.event.*; 5 import javax.swing.*; 6 import javax.swing.event.*; 7 import java.util.*; 8 import java.io.*; 9 import javax.swing.filechooser.*; 10 import javax.swing.filechooser.FileFilter; 11 import java.awt.datatransfer.*; 12 13 public class Text extends JFrame { 14 TextArea area;//文本域 15 String name = null;//文件名 16 String board = null; 17 private Clipboard cb;//系统粘贴板 18 19 Text() throws Exception {//构造函数 20 setTitle("记事本"); 21 Toolkit kit = Toolkit.getDefaultToolkit();//Toolkit获得本机系统的屏幕的参数 22 //Image img = kit.getImage("01.jpg"); 23 //setIconImage(img); 24 25 26 JMenuBar jmb = new JMenuBar();//创建菜单条 27 add(jmb); 28 setJMenuBar(jmb); 29 area = new TextArea(); 30 add(area); 31 32 JMenu file = new JMenu("文件");//定义“文件”菜单项 33 JMenuItem rebuild = new JMenuItem("新建"); 34 rebuild.setAccelerator(KeyStroke.getKeyStroke("ctrl N")); 35 JMenuItem save = new JMenuItem("保存"); 36 save.setAccelerator(KeyStroke.getKeyStroke("ctrl S")); 37 JMenuItem open = new JMenuItem("打开"); 38 open.setAccelerator(KeyStroke.getKeyStroke("ctrl O")); 39 JMenuItem anotherSave = new JMenuItem("另存为"); 40 file.add(rebuild); 41 file.add(save); 42 file.add(open); 43 file.add(anotherSave); 44 // JScrollPane scroldPane=new JScrollPane(area); //设置滚动轴 45 // add(scroldPane); 46 JMenu edit = new JMenu("编辑");//定义“编辑”菜单项 47 JMenuItem cut = new JMenuItem("剪切"); 48 cut.setAccelerator(KeyStroke.getKeyStroke("ctrl X")); 49 JMenuItem copy = new JMenuItem("复制"); 50 copy.setAccelerator(KeyStroke.getKeyStroke("ctrl C")); 51 JMenuItem plaster = new JMenuItem("粘贴"); 52 plaster.setAccelerator(KeyStroke.getKeyStroke("ctrl V")); 53 JMenuItem all = new JMenuItem("全选"); 54 all.setAccelerator(KeyStroke.getKeyStroke("ctrl A")); 55 edit.add(cut); 56 edit.add(copy); 57 edit.add(plaster); 58 edit.add(all); 59 JMenu help = new JMenu("帮助");//定义“帮助”菜单项 60 JMenuItem about = new JMenuItem("关于"); 61 help.add(about); 62 63 jmb.add(file); 64 jmb.add(edit); 65 jmb.add(help); 66 setSize(600, 400); 67 //setLocation(300, 200); 68 setVisible(true); 69 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 70 71 cb = Toolkit.getDefaultToolkit().getSystemClipboard();//调用系统剪贴板 72 save.addActionListener(new ActionListener() { 73 public void actionPerformed(ActionEvent e) { 74 try { 75 saveText(); 76 } catch (Exception ex) { 77 } 78 } 79 }); 80 open.addActionListener(new ActionListener() { 81 public void actionPerformed(ActionEvent e) { 82 try { 83 openText(); 84 } catch (Exception ex) { 85 } 86 } 87 }); 88 rebuild.addActionListener(new ActionListener() { 89 public void actionPerformed(ActionEvent e) { 90 try { 91 area.setText(""); 92 name = null; 93 } catch (Exception ex) { 94 } 95 } 96 }); 97 anotherSave.addActionListener(new ActionListener() { 98 public void actionPerformed(ActionEvent e) { 99 anotherSaveText(); 100 } 101 }); 102 103 cut.addActionListener(new ActionListener() // 剪切 104 { 105 public void actionPerformed(ActionEvent e) { 106 board = area.getSelectedText(); 107 cb.setContents(new StringSelection(board), null); 108 area.replaceRange("", area.getSelectionStart(), 109 area.getSelectionEnd()); 110 } 111 }); 112 copy.addActionListener(new ActionListener() // 复制 113 { 114 public void actionPerformed(ActionEvent e) { 115 board = area.getSelectedText(); 116 cb.setContents(new StringSelection(board), null); 117 } 118 }); 119 plaster.addActionListener(new ActionListener() // 粘贴 120 { 121 public void actionPerformed(ActionEvent e) { 122 try { 123 Transferable content = cb.getContents(null); 124 String st = (String) content 125 .getTransferData(DataFlavor.stringFlavor); 126 area.replaceRange(st, area.getSelectionStart(), 127 area.getSelectionEnd()); 128 } catch (Exception ex) { 129 } 130 } 131 }); 132 all.addActionListener(new ActionListener() // 全选 133 { 134 public void actionPerformed(ActionEvent e) { 135 area.setSelectionStart(0); 136 area.setSelectionEnd(area.getText().length()); 137 } 138 }); 139 about.addActionListener(new ActionListener() {//帮助 140 public void actionPerformed(ActionEvent e) { 141 JFrame frame = new JFrame("关于"); 142 frame.setSize(150, 100); 143 frame.setLocation(400, 300); 144 JTextArea area1 = new JTextArea("java简易文本编辑器"); 145 frame.add(area1); 146 frame.setVisible(true); 147 } 148 }); 149 } 150 151 public void openText() // 打开 152 { 153 JFileChooser chooser = new JFileChooser(); 154 FileNameExtensionFilter filter = new FileNameExtensionFilter("Files", 155 "txt", "java"); 156 chooser.setFileFilter(filter); 157 chooser.setCurrentDirectory(new File(".")); 158 int result = chooser.showOpenDialog(Text.this); 159 if (result == JFileChooser.APPROVE_OPTION) { 160 name = chooser.getSelectedFile().getPath(); 161 setTitle(name); 162 try { 163 BufferedReader in = new BufferedReader(new FileReader(name)); 164 String line = null; 165 String datas = ""; 166 while ((line = in.readLine()) != null) { 167 if (datas == "") { 168 datas = datas + line; 169 } else { 170 datas = datas + "\n" + line; 171 } 172 } 173 area.setText(datas); 174 in.close(); 175 } catch (Exception ex) { 176 } 177 } 178 } 179 180 public void saveText() // 保存 181 { 182 if (name == null) { 183 JFileChooser chooser = new JFileChooser(); 184 FileNameExtensionFilter filter = new FileNameExtensionFilter( 185 "Files", "txt", "java"); 186 chooser.setFileFilter(filter); 187 chooser.setCurrentDirectory(new File(".")); 188 int result = chooser.showSaveDialog(Text.this); 189 if (result == JFileChooser.APPROVE_OPTION) { 190 name = chooser.getSelectedFile().getPath(); 191 try { 192 OutputStream out = new FileOutputStream(name); 193 String datas = area.getText(); 194 out.write(datas.getBytes()); 195 out.close(); 196 } catch (Exception ex) { 197 } 198 } 199 } else { 200 try { 201 OutputStream out = new FileOutputStream(name); 202 String datas = area.getText(); 203 out.write(datas.getBytes()); 204 out.close(); 205 } catch (Exception ex) { 206 } 207 } 208 } 209 210 public void anotherSaveText() // 另存为 211 { 212 JFileChooser chooser = new JFileChooser(); 213 FileNameExtensionFilter filter = new FileNameExtensionFilter("Files", 214 "txt", "java"); 215 chooser.setFileFilter(filter); 216 chooser.setCurrentDirectory(new File(".")); 217 int result = chooser.showSaveDialog(Text.this); 218 if (result == JFileChooser.APPROVE_OPTION) { 219 name = chooser.getSelectedFile().getPath(); 220 try { 221 OutputStream out = new FileOutputStream(name); 222 String datas = area.getText(); 223 out.write(datas.getBytes()); 224 out.close(); 225 } catch (Exception ex) { 226 } 227 } 228 } 229 230 public static void main(String[] args) throws Exception { 231 new Text(); 232 } 233 }