1 package peng_jun; 2 3 import java.awt.*; 4 import java.awt.event.*; 5 6 import javax.swing.*; 7 8 import java.io.*; 9 10 import javax.swing.filechooser.*; 11 12 import java.awt.datatransfer.*; 13 14 public class Text3 extends JFrame { 15 public static void main(String[] args) throws Exception { 16 new Text3(); 17 } 18 19 TextArea area = new TextArea();;// 文本域 20 String name = null;// 文件名 21 String board = null; 22 Clipboard cb;// 系统粘贴板 23 24 JMenu font = new JMenu("字体"); 25 JMenuItem bold = new JMenuItem("加粗"); 26 JMenuItem italics = new JMenuItem("斜体"); 27 JMenu size = new JMenu("字号"); 28 JMenuItem size_18 = new JMenuItem("18"); 29 JMenuItem size_19 = new JMenuItem("19"); 30 JMenuItem size_20 = new JMenuItem("20"); 31 JMenuItem size_21 = new JMenuItem("21"); 32 JMenu color = new JMenu("颜色"); 33 final JMenuItem red = new JMenuItem("红色"); 34 JMenuItem blue = new JMenuItem("蓝色"); 35 36 JMenu file = new JMenu("文件");// 定义“文件”菜单项 37 JMenuItem rebuild = new JMenuItem("新建"); 38 JMenuItem save = new JMenuItem("保存"); 39 JMenuItem open = new JMenuItem("打开"); 40 JMenuItem anotherSave = new JMenuItem("另存为"); 41 JMenuItem clear = new JMenuItem("清除"); 42 JMenuItem close = new JMenuItem("退出"); 43 44 JMenu edit = new JMenu("编辑");// 定义“编辑”菜单项 45 JMenuItem cut = new JMenuItem("剪切"); 46 JMenuItem copy = new JMenuItem("复制"); 47 JMenuItem plaster = new JMenuItem("粘贴"); 48 JMenuItem all = new JMenuItem("全选"); 49 50 JMenu help = new JMenu("帮助");// 定义“帮助”菜单项 51 JMenuItem about = new JMenuItem("关于"); 52 53 Toolkit kit = Toolkit.getDefaultToolkit();// Toolkit获得本机系统的屏幕的参数 54 JMenuBar jmb = new JMenuBar();// 创建菜单条 55 56 Text3() throws Exception {// 构造函数 57 setTitle("记事本"); 58 59 // 设置快捷键 60 bold.setAccelerator(KeyStroke.getKeyStroke("ctrl B")); 61 italics.setAccelerator(KeyStroke.getKeyStroke("ctrl I")); 62 rebuild.setAccelerator(KeyStroke.getKeyStroke("ctrl N")); 63 save.setAccelerator(KeyStroke.getKeyStroke("ctrl S")); 64 copy.setAccelerator(KeyStroke.getKeyStroke("ctrl C")); 65 open.setAccelerator(KeyStroke.getKeyStroke("ctrl O")); 66 cut.setAccelerator(KeyStroke.getKeyStroke("ctrl X")); 67 plaster.setAccelerator(KeyStroke.getKeyStroke("ctrl V")); 68 all.setAccelerator(KeyStroke.getKeyStroke("ctrl A")); 69 70 add(jmb); 71 setJMenuBar(jmb);// 设置菜单条 72 add(area); 73 file.add(rebuild); 74 file.add(save); 75 file.add(open); 76 file.add(anotherSave); 77 file.addSeparator(); 78 file.add(clear); 79 file.add(close); 80 // JScrollPane scroldPane=new JScrollPane(area); //设置滚动轴 81 // add(scroldPane); 82 83 edit.add(cut); 84 edit.add(copy); 85 edit.add(plaster); 86 edit.add(all); 87 88 font.add(bold); 89 font.add(italics); 90 size.add(size_18); 91 size.add(size_19); 92 size.add(size_20); 93 size.add(size_21); 94 color.add(red); 95 color.add(blue); 96 font.add(size); 97 font.add(color); 98 99 help.add(about); 100 // 添加事件 101 clear.addActionListener(new HandleAct()); 102 close.addActionListener(new HandleAct()); 103 bold.addActionListener(new HandleAct()); 104 italics.addActionListener(new HandleAct()); 105 size_18.addActionListener(new HandleAct()); 106 size_19.addActionListener(new HandleAct()); 107 size_20.addActionListener(new HandleAct()); 108 size_21.addActionListener(new HandleAct()); 109 red.addActionListener(new HandleAct()); 110 blue.addActionListener(new HandleAct()); 111 112 jmb.add(file); 113 jmb.add(edit); 114 jmb.add(font); 115 jmb.add(help); 116 setSize(600, 400);// 大小信息 117 //setLocation(300, 200);// 文本框位置 118 setVisible(true); 119 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 120 121 cb = Toolkit.getDefaultToolkit().getSystemClipboard();// 调用系统剪贴板 122 save.addActionListener(new ActionListener() { 123 public void actionPerformed(ActionEvent e) { 124 try { 125 saveText(); 126 } catch (Exception ex) { 127 } 128 } 129 }); 130 open.addActionListener(new ActionListener() { 131 public void actionPerformed(ActionEvent e) { 132 try { 133 openText(); 134 } catch (Exception ex) { 135 } 136 } 137 }); 138 rebuild.addActionListener(new ActionListener() { 139 public void actionPerformed(ActionEvent e) { 140 try { 141 area.setText(""); 142 name = null; 143 } catch (Exception ex) { 144 } 145 } 146 }); 147 anotherSave.addActionListener(new ActionListener() { 148 public void actionPerformed(ActionEvent e) { 149 anotherSaveText(); 150 } 151 }); 152 153 size_18.addActionListener(new ActionListener() { 154 155 @Override 156 public void actionPerformed(ActionEvent e) { 157 // TODO 自动生成的方法存根 158 Font oldF = area.getFont();// 获取当前字体 159 String name = oldF.getName();// 获取当前字体的名字 160 area.setFont(new Font(name, oldF.getStyle(), 18)); 161 } 162 }); 163 164 cut.addActionListener(new ActionListener() // 剪切 165 { 166 public void actionPerformed(ActionEvent e) { 167 board = area.getSelectedText(); 168 cb.setContents(new StringSelection(board), null); 169 area.replaceRange("", area.getSelectionStart(), 170 area.getSelectionEnd()); 171 } 172 }); 173 copy.addActionListener(new ActionListener() // 复制 174 { 175 public void actionPerformed(ActionEvent e) { 176 board = area.getSelectedText(); 177 cb.setContents(new StringSelection(board), null); 178 } 179 }); 180 plaster.addActionListener(new ActionListener() // 粘贴 181 { 182 public void actionPerformed(ActionEvent e) { 183 try { 184 Transferable content = cb.getContents(null); 185 String st = (String) content 186 .getTransferData(DataFlavor.stringFlavor); 187 area.replaceRange(st, area.getSelectionStart(), 188 area.getSelectionEnd()); 189 } catch (Exception ex) { 190 } 191 } 192 }); 193 all.addActionListener(new ActionListener() // 全选 194 { 195 public void actionPerformed(ActionEvent e) { 196 area.setSelectionStart(0); 197 area.setSelectionEnd(area.getText().length()); 198 } 199 }); 200 201 about.addActionListener(new ActionListener() {// 帮助 202 public void actionPerformed(ActionEvent e) { 203 JFrame frame = new JFrame("关于"); 204 frame.setSize(250, 150); 205 frame.setLocation(400, 300); 206 JTextArea area1 = new JTextArea("java简易文本编辑器开发人员:"+‘\n‘ 207 + "彭军军 201321091103,"+‘\n‘ 208 + "鲍静瑶201321091080,"+‘\n‘ 209 + "刘瑾 201321091090."); 210 frame.add(area1); 211 frame.setVisible(true); 212 } 213 }); 214 } 215 216 public void openText() // 打开 217 { 218 JFileChooser chooser = new JFileChooser(); 219 FileNameExtensionFilter filter = new FileNameExtensionFilter("Files", 220 "txt", "java"); 221 chooser.setFileFilter(filter); 222 chooser.setCurrentDirectory(new File(".")); 223 int result = chooser.showOpenDialog(Text3.this); 224 if (result == JFileChooser.APPROVE_OPTION) { 225 name = chooser.getSelectedFile().getPath(); 226 setTitle(name); 227 try { 228 BufferedReader in = new BufferedReader(new FileReader(name)); 229 String line = null; 230 String datas = ""; 231 while ((line = in.readLine()) != null) { 232 if (datas == "") { 233 datas = datas + line; 234 } else { 235 datas = datas + "\n" + line; 236 } 237 } 238 area.setText(datas); 239 in.close(); 240 } catch (Exception ex) { 241 } 242 } 243 } 244 245 public void saveText() // 保存 246 { 247 if (name == null) { 248 JFileChooser chooser = new JFileChooser(); 249 FileNameExtensionFilter filter = new FileNameExtensionFilter( 250 "Files", "txt", "java");// 限制扩展名,只能杜渠道txt和java类型的文件 251 chooser.setFileFilter(filter); 252 chooser.setCurrentDirectory(new File(".")); 253 int result = chooser.showSaveDialog(Text3.this); 254 if (result == JFileChooser.APPROVE_OPTION) { 255 name = chooser.getSelectedFile().getPath(); 256 try { 257 OutputStream out = new FileOutputStream(name); 258 String datas = area.getText(); 259 out.write(datas.getBytes()); 260 out.close(); 261 } catch (Exception ex) { 262 } 263 } 264 } else { 265 try { 266 OutputStream out = new FileOutputStream(name); 267 String datas = area.getText(); 268 out.write(datas.getBytes()); 269 out.close(); 270 } catch (Exception ex) { 271 } 272 } 273 } 274 275 public void anotherSaveText() // 另存为 276 { 277 JFileChooser chooser = new JFileChooser(); 278 FileNameExtensionFilter filter = new FileNameExtensionFilter("Files", 279 "txt", "java"); 280 chooser.setFileFilter(filter); 281 chooser.setCurrentDirectory(new File(".")); 282 int result = chooser.showSaveDialog(Text3.this); 283 if (result == JFileChooser.APPROVE_OPTION) { 284 name = chooser.getSelectedFile().getPath(); 285 try { 286 OutputStream out = new FileOutputStream(name); 287 String datas = area.getText(); 288 out.write(datas.getBytes()); 289 out.close(); 290 } catch (Exception ex) { 291 } 292 } 293 } 294 295 class HandleAct implements ActionListener {//内部类 296 297 public void actionPerformed(ActionEvent e) { 298 // TODO 自动生成的方法存根 299 Font oldF = area.getFont();// 获取当前字体 300 String name = oldF.getName();// 获取当前字体的名字 301 int size = oldF.getSize();// 当前字体的大小 302 if (e.getSource() == size_18) 303 area.setFont(new Font(name, oldF.getStyle(), 18)); 304 else if (e.getSource() == size_19) 305 area.setFont(new Font(name, oldF.getStyle(), 19)); 306 else if (e.getSource() == size_20) 307 area.setFont(new Font(name, oldF.getStyle(), 20)); 308 else if (e.getSource() == size_21) 309 area.setFont(new Font(name, oldF.getStyle(), 21)); 310 311 if (e.getSource() == red) { 312 area.setForeground(Color.red);// 设置文字颜色 313 } else if (e.getSource() == blue) { 314 area.setForeground(Color.blue);// 设置文字颜色 315 } 316 317 if (e.getSource() == bold) { 318 area.setFont(new Font(name, Font.BOLD, size)); 319 } else if (e.getSource() == italics) { 320 area.setFont(new Font(name, Font.ITALIC, size)); 321 } 322 323 if (e.getSource() == clear) { 324 area.setText(" "); 325 } else if (e.getSource() == close) { 326 System.exit(0); 327 } 328 329 } 330 331 } 332 333 }
时间: 2024-10-25 23:47:12