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 Text4 extends JFrame { 15 public static void main(String[] args) throws Exception { 16 new Text4(); 17 } 18 19 JTextArea area = new JTextArea();;// 文本域 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 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 JMenuItem replice = new JMenuItem("替换"); 50 JMenuItem check = new JMenuItem("查找"); 51 52 JMenu help = new JMenu("帮助");// 定义“帮助”菜单项 53 JMenuItem about = new JMenuItem("关于"); 54 55 Toolkit kit = Toolkit.getDefaultToolkit();// Toolkit获得本机系统的屏幕的参数 56 JMenuBar jmb = new JMenuBar();// 创建菜单条 57 58 Text4() throws Exception {// 构造函数 59 setTitle("记事本"); 60 61 // 设置快捷键 62 bold.setAccelerator(KeyStroke.getKeyStroke("ctrl B")); 63 italics.setAccelerator(KeyStroke.getKeyStroke("ctrl I")); 64 rebuild.setAccelerator(KeyStroke.getKeyStroke("ctrl N")); 65 save.setAccelerator(KeyStroke.getKeyStroke("ctrl S")); 66 copy.setAccelerator(KeyStroke.getKeyStroke("ctrl C")); 67 open.setAccelerator(KeyStroke.getKeyStroke("ctrl O")); 68 cut.setAccelerator(KeyStroke.getKeyStroke("ctrl X")); 69 plaster.setAccelerator(KeyStroke.getKeyStroke("ctrl V")); 70 all.setAccelerator(KeyStroke.getKeyStroke("ctrl A")); 71 replice.setAccelerator(KeyStroke.getKeyStroke("ctrl H")); 72 check.setAccelerator(KeyStroke.getKeyStroke("ctrl F")); 73 74 75 add(jmb); 76 setJMenuBar(jmb);// 设置菜单条 77 add(area); 78 file.add(rebuild); 79 file.add(save); 80 file.add(open); 81 file.add(anotherSave); 82 file.addSeparator(); 83 file.add(clear); 84 file.add(close); 85 // JScrollPane scroldPane=new JScrollPane(area); //设置滚动轴 86 // add(scroldPane); 87 88 edit.add(cut); 89 edit.add(copy); 90 edit.add(plaster); 91 edit.add(all); 92 edit.addSeparator(); 93 edit.add(replice); 94 edit.add(check); 95 96 font.add(bold); 97 font.add(italics); 98 size.add(size_18); 99 size.add(size_19); 100 size.add(size_20); 101 size.add(size_21); 102 color.add(red); 103 color.add(blue); 104 font.add(size); 105 font.add(color); 106 107 help.add(about); 108 // 添加事件 109 clear.addActionListener(new HandleAct()); 110 close.addActionListener(new HandleAct()); 111 bold.addActionListener(new HandleAct()); 112 italics.addActionListener(new HandleAct()); 113 size_18.addActionListener(new HandleAct()); 114 size_19.addActionListener(new HandleAct()); 115 size_20.addActionListener(new HandleAct()); 116 size_21.addActionListener(new HandleAct()); 117 red.addActionListener(new HandleAct()); 118 blue.addActionListener(new HandleAct()); 119 replice.addActionListener(new HandleAct()); 120 check.addActionListener(new HandleAct()); 121 122 jmb.add(file); 123 jmb.add(edit); 124 jmb.add(font); 125 jmb.add(help); 126 setSize(600, 400);// 大小信息 127 // setLocation(300, 200);// 文本框位置 128 setVisible(true); 129 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 130 131 cb = Toolkit.getDefaultToolkit().getSystemClipboard();// 调用系统剪贴板 132 save.addActionListener(new ActionListener() { 133 public void actionPerformed(ActionEvent e) { 134 try { 135 saveText(); 136 } catch (Exception ex) { 137 } 138 } 139 }); 140 open.addActionListener(new ActionListener() { 141 public void actionPerformed(ActionEvent e) { 142 try { 143 openText(); 144 } catch (Exception ex) { 145 } 146 } 147 }); 148 rebuild.addActionListener(new ActionListener() { 149 public void actionPerformed(ActionEvent e) { 150 try { 151 area.setText(""); 152 name = null; 153 } catch (Exception ex) { 154 } 155 } 156 }); 157 anotherSave.addActionListener(new ActionListener() { 158 public void actionPerformed(ActionEvent e) { 159 anotherSaveText(); 160 } 161 }); 162 163 size_18.addActionListener(new ActionListener() { 164 165 @Override 166 public void actionPerformed(ActionEvent e) { 167 // TODO 自动生成的方法存根 168 Font oldF = area.getFont();// 获取当前字体 169 String name = oldF.getName();// 获取当前字体的名字 170 area.setFont(new Font(name, oldF.getStyle(), 18)); 171 } 172 }); 173 174 cut.addActionListener(new ActionListener() // 剪切 175 { 176 public void actionPerformed(ActionEvent e) { 177 board = area.getSelectedText(); 178 cb.setContents(new StringSelection(board), null); 179 area.replaceRange("", area.getSelectionStart(), 180 area.getSelectionEnd()); 181 } 182 }); 183 copy.addActionListener(new ActionListener() // 复制 184 { 185 public void actionPerformed(ActionEvent e) { 186 board = area.getSelectedText(); 187 cb.setContents(new StringSelection(board), null); 188 } 189 }); 190 plaster.addActionListener(new ActionListener() // 粘贴 191 { 192 public void actionPerformed(ActionEvent e) { 193 try { 194 Transferable content = cb.getContents(null); 195 String st = (String) content 196 .getTransferData(DataFlavor.stringFlavor); 197 area.replaceRange(st, area.getSelectionStart(), 198 area.getSelectionEnd()); 199 } catch (Exception ex) { 200 } 201 } 202 }); 203 all.addActionListener(new ActionListener() // 全选 204 { 205 public void actionPerformed(ActionEvent e) { 206 area.setSelectionStart(0); 207 area.setSelectionEnd(area.getText().length()); 208 } 209 }); 210 211 about.addActionListener(new ActionListener() {// 帮助 212 public void actionPerformed(ActionEvent e) { 213 JFrame frame = new JFrame("关于"); 214 frame.setSize(250, 150); 215 frame.setLocation(400, 300); 216 JTextArea area1 = new JTextArea("java简易文本编辑器开发人员:" + ‘\n‘ 217 + "彭军军 201321091103," + ‘\n‘ + "鲍静瑶201321091080," 218 + ‘\n‘ + "刘瑾 201321091081."); 219 frame.add(area1); 220 frame.setVisible(true); 221 } 222 }); 223 } 224 225 public void openText() // 打开 226 { 227 /* 228 * 在选择文件方法中,首先创建JFileChooser文件选择器, 229 * 使用JFileChooser类的setFileFilter()方法创建文件扩展名过滤器,再使用JFileChooser类 230 * 的setFileSelectionMode()方法设置文件选择模式为文件, 231 * 通过JFileChooser类的showOpenDialog()方法显示文件打开对话框,确定用户按下打开按钮, 232 * 而非取消按钮后,通过JFileChooser类的getSelectedFile()方法获取用户选择的文件对象, 233 * 使用JTextField类的setText()方法显示文件信息到文本框。 234 */ 235 JFileChooser chooser = new JFileChooser();//创建JFileChooser文件选择器 236 FileNameExtensionFilter filter = new FileNameExtensionFilter("Files", 237 "txt", "java","html","css","js");//文件过滤器 238 chooser.setFileFilter(filter); 239 chooser.setCurrentDirectory(new File(".")); 240 int result = chooser.showOpenDialog(Text4.this);//打开当前文件夹 241 if (result == JFileChooser.APPROVE_OPTION) {//用户按下了yes 242 name = chooser.getSelectedFile().getPath();//获取文件对象 243 setTitle(name); 244 try { 245 BufferedReader in = new BufferedReader(new FileReader(name)); 246 String line = null; 247 String datas = "";//保存数据 248 //用while循环来判断读取是否结束 此处用的是行读取 249 while ((line = in.readLine()) != null) { 250 if (datas == "") { 251 datas = datas + line; 252 } else { 253 datas = datas + "\n" + line; 254 } 255 } 256 area.setText(datas); 257 in.close(); 258 } catch (Exception ex) { 259 } 260 } 261 } 262 263 public void saveText() // 保存 264 { 265 if (name == null) { 266 JFileChooser chooser = new JFileChooser(); 267 FileNameExtensionFilter filter = new FileNameExtensionFilter( 268 "Files", "txt", "java");// 限制扩展名,只能杜渠道txt和java类型的文件 269 chooser.setFileFilter(filter); 270 chooser.setCurrentDirectory(new File("."));//设置当前目录 271 int result = chooser.showSaveDialog(Text4.this); 272 if (result == JFileChooser.APPROVE_OPTION) {//用户按下了yes 273 name = chooser.getSelectedFile().getPath(); 274 try { 275 OutputStream out = new FileOutputStream(name); 276 String datas = area.getText(); 277 out.write(datas.getBytes()); 278 out.close(); 279 } catch (Exception ex) { 280 } 281 } 282 } 283 } 284 285 public void anotherSaveText() // 另存为 286 { 287 JFileChooser chooser = new JFileChooser(); 288 FileNameExtensionFilter filter = new FileNameExtensionFilter("Files", 289 "txt", "java","html","js","css"); 290 chooser.setFileFilter(filter); 291 chooser.setCurrentDirectory(new File(".")); 292 int result = chooser.showSaveDialog(Text4.this); 293 if (result == JFileChooser.APPROVE_OPTION) {//用户按下了yes 294 name = chooser.getSelectedFile().getPath(); 295 try { 296 OutputStream out = new FileOutputStream(name); 297 String datas = area.getText(); 298 out.write(datas.getBytes()); 299 out.close(); 300 } catch (Exception ex) { 301 } 302 } 303 } 304 305 class HandleAct implements ActionListener {// 内部类 306 307 public void actionPerformed(ActionEvent e) { 308 // TODO 自动生成的方法存根 309 Font oldF = area.getFont();// 获取当前字体 310 String name = oldF.getName();// 获取当前字体的名字 311 int size = oldF.getSize();// 当前字体的大小 312 if (e.getSource() == size_18) 313 area.setFont(new Font(name, oldF.getStyle(), 18)); 314 else if (e.getSource() == size_19) 315 area.setFont(new Font(name, oldF.getStyle(), 19)); 316 else if (e.getSource() == size_20) 317 area.setFont(new Font(name, oldF.getStyle(), 20)); 318 else if (e.getSource() == size_21) 319 area.setFont(new Font(name, oldF.getStyle(), 21)); 320 321 if (e.getSource() == red) { 322 area.setForeground(Color.red);// 设置文字颜色 323 } else if (e.getSource() == blue) { 324 area.setForeground(Color.blue);// 设置文字颜色 325 } 326 327 if (e.getSource() == bold) { 328 area.setFont(new Font(name, Font.BOLD, size)); 329 } else if (e.getSource() == italics) { 330 area.setFont(new Font(name, Font.ITALIC, size)); 331 } 332 333 if (e.getSource() == clear) { 334 area.setText(" "); 335 } else if (e.getSource() == close) { 336 System.exit(0); 337 } 338 339 if (e.getSource() == replice) { 340 //替换弹出框的布局 341 JPanel replacePanel = new JPanel(); 342 JLabel lookupLabel = new JLabel("要替换的内容"); 343 JTextField inputText = new JTextField(10); 344 JLabel replaceLabel = new JLabel("替换为"); 345 JTextField changetoText = new JTextField(10); 346 replacePanel.add(lookupLabel); 347 replacePanel.add(inputText); 348 replacePanel.add(replaceLabel); 349 replacePanel.add(changetoText); 350 //布局结束 351 JOptionPane.showMessageDialog(null, replacePanel); 352 String text = area.getText();//获得整个文本内容 353 String changeText = text.replace(inputText.getText(), 354 changetoText.getText());//启用replace功能函数 355 area.setText(changeText); 356 } 357 else if (e.getSource() == check) { 358 //查找弹出框的布局 359 JPanel swapPanel=new JPanel(); 360 JLabel seekLabel=new JLabel("要查找的内容"); 361 JTextField inputText=new JTextField(20); 362 swapPanel.add(seekLabel); 363 swapPanel.add(inputText); 364 JOptionPane.showMessageDialog(null,swapPanel); 365 //布局结束 366 String text=area.getText();//获得整个文本内容 367 int fromIndex=area.getCaretPosition();//取得当前的光标位置 368 int lastfromIndex=text.lastIndexOf(inputText.getText(),fromIndex);//获得查找后的位置 369 area.setCaretPosition(lastfromIndex); 370 area.moveCaretPosition(lastfromIndex+inputText.getText().length());//使查找到的子字符串显示出来 371 } 372 373 } 374 375 } 376 377 }
时间: 2024-10-25 03:52:33