ImageVerifierCode 换一换
格式:DOCX , 页数:17 ,大小:18.45KB ,
资源ID:9244961      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/9244961.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(记事本源程序.docx)为本站会员(b****7)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

记事本源程序.docx

1、记事本源程序import java.awt.CheckboxMenuItem;import java.awt.Color;import java.awt.Container;import java.awt.FileDialog;import java.awt.FlowLayout;import java.awt.Font;import java.awt.GraphicsEnvironment;import java.awt.Menu;import java.awt.MenuBar;import java.awt.MenuItem;import java.awt.MenuShortcut;imp

2、ort java.awt.TextArea;import java.awt.Toolkit;import java.awt.Window;import java.awt.datatransfer.Clipboard;import java.awt.datatransfer.DataFlavor;import java.awt.datatransfer.StringSelection;import java.awt.datatransfer.Transferable;import java.awt.datatransfer.UnsupportedFlavorException;import ja

3、va.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.KeyEvent;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileNotFoundException;import java.io.FileR

4、eader;import java.io.FileWriter;import java.io.IOException;import java.util.logging.Level;import java.util.logging.Logger;import javax.swing.JButton;import javax.swing.JColorChooser;import javax.swing.JComboBox;import javax.swing.JDialog;import javax.swing.JFileChooser;import javax.swing.JFrame;impo

5、rt javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JTextField;public class notebook / 记事本的具体实现类 private static final long serialVersionUID = 1L; private TextArea content; private String filePath = ;/先让路径为空 Color color=Color.red; Toolkit toolKit = Toolki

6、t.getDefaultToolkit(); Clipboard clipboard = toolKit.getSystemClipboard(); public notebook() /创建一个JFrame对象;并设置相关属性 final JFrame jf = new JFrame(记事本); jf.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); jf.setBounds(100,100,500,500); jf.setResizable(true); jf.setVisible(true); /创建菜单栏 MenuBar men

7、u = new MenuBar(); jf.setMenuBar(menu); /创建并添加文本框 content = new TextArea(,50,50,TextArea.SCROLLBARS_VERTICAL_ONLY); jf.add(content); content.setVisible(true); content.requestFocusInWindow(); /菜单栏添加内容 Menu filemenu = new Menu(文件); Menu editmenu = new Menu(编辑); Menu formatmenu = new Menu(格式); menu.add

8、(filemenu); menu.add(editmenu); menu.add(formatmenu); /创建文件菜单上的各个菜单项并添加到菜单上 MenuItem newitem = new MenuItem(新建(N)); newitem.setShortcut(new MenuShortcut(KeyEvent.VK_N,false); filemenu.add(newitem); MenuItem openitem = new MenuItem(打开(O)); openitem.setShortcut(new MenuShortcut(KeyEvent.VK_O,false); f

9、ilemenu.add(openitem); MenuItem saveitem = new MenuItem(保存(S)); saveitem.setShortcut(new MenuShortcut(KeyEvent.VK_S,false); filemenu.add(saveitem); MenuItem saveasitem = new MenuItem(另存为(A)); saveasitem.setShortcut(new MenuShortcut(KeyEvent.VK_A,false); filemenu.add(saveasitem); filemenu.addSeparato

10、r(); MenuItem exititem = new MenuItem(退出(X)); exititem.setShortcut(new MenuShortcut(KeyEvent.VK_X,false); filemenu.add(exititem); /添加监听器来实现文件菜单上的各个菜单项的功能 /新建菜单项的功能实现 newitem.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) String con = content.getText(); if(!con.equa

11、ls()/文本域里文本不为空 int result = JOptionPane.showConfirmDialog( null, (是否要保存?),(保存文件.),JOptionPane.YES_NO_CANCEL_OPTION); if(result = JOptionPane.NO_OPTION)/不保存 content.setText(); else if(result = JOptionPane.CANCEL_OPTION)/取消新建 else if(result = JOptionPane.YES_OPTION)/选择保存 JFileChooser jfc = new JFileCh

12、ooser();/用于选择保存路径的文件名 int bcf = jfc.showSaveDialog(jf); if(bcf = JFileChooser.APPROVE_OPTION) try /保存文件 BufferedWriter bfw = new BufferedWriter( new FileWriter(new File(jfc.getSelectedFile().getAbsolutePath()+.txt); filePath = jfc.getSelectedFile().getAbsolutePath()+.txt;/获取文件保存的路径 bfw.write(con);/向

13、文件写出数据 bfw.flush(); bfw.close();/关闭输出流 catch (IOException ex) Logger.getLogger(notebook.class.getName().log(Level.SEVERE, null, ex); new notebook();/新建文本文件 ); /打开菜单项的功能实现 openitem.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) FileDialog dialog = new FileDialog(new

14、 JFrame(),打开.,FileDialog.LOAD); dialog.setVisible(true); filePath = dialog.getDirectory() + dialog.getFile(); System.out.println(filePath); File file = new File(filePath); BufferedReader br = null; StringBuilder sb = new StringBuilder(); try br = new BufferedReader (new FileReader(file); String str

15、= null; while (str = br.readLine() != null) sb.append(str).append(n); content.setText(sb.toString(); catch(FileNotFoundException e1) e1.printStackTrace(); catch(IOException e1) e1.printStackTrace(); finally if(br != null) try br.close(); catch(IOException e1) e1.printStackTrace(); ); /保存菜单项的功能实现 sav

16、eitem.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) FileDialog dialog = new FileDialog(new JFrame(),保存.,FileDialog.SAVE); dialog.setVisible(true); filePath = dialog.getDirectory() + dialog.getFile(); if(filePath.equals()/没有路径时,就另存为 JFileChooser jfc = new JFileChoo

17、ser();/用于选择保存路径的文件名 int bcf = jfc.showSaveDialog(jf);/弹出保存窗口 if(bcf = JFileChooser.APPROVE_OPTION) try /保存文件 BufferedWriter bfw = new BufferedWriter( new FileWriter(new File(jfc.getSelectedFile().getAbsolutePath()+.txt); filePath = jfc.getSelectedFile().getAbsolutePath(); bfw.write(content.getText()

18、;/向文件写出数据 bfw.flush(); bfw.close();/关闭输出流 catch (IOException ex) Logger.getLogger(notebook.class.getName().log(Level.SEVERE, null, ex); else/路径不为空时,保存在原来的路径下 try /保存文件 BufferedWriter bfw = new BufferedWriter( new FileWriter( new File(filePath); bfw.write(content.getText();/向文件写出数据 bfw.flush(); bfw.c

19、lose();/关闭输出流 catch (IOException ex) Logger.getLogger(notebook.class.getName().log(Level.SEVERE, null, ex); ); /另存为菜单项的功能实现 saveasitem.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) JFileChooser jfc = new JFileChooser();/用于选择保存路径的文件名 int bcf = jfc.showSaveDialog(jf

20、);/弹出保存窗口 if(bcf = JFileChooser.APPROVE_OPTION) try /保存文件 BufferedWriter bfw = new BufferedWriter( new FileWriter(new File(jfc.getSelectedFile().getAbsolutePath()+.txt); filePath = jfc.getSelectedFile().getAbsolutePath(); bfw.write(content.getText();/向文件写出数据 bfw.flush(); bfw.close();/关闭输出流 catch (IO

21、Exception ex) Logger.getLogger(notebook.class.getName().log(Level.SEVERE, null, ex); ); /退出菜单项的功能实现 exititem.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) Object options = 是的,我要退出, 不好意思,点错了 ; int option = JOptionPane.showOptionDialog(null, 您确定要退出吗?, 退出提示.,JOptionP

22、ane.OK_CANCEL_OPTION,JOptionPane.WARNING_MESSAGE, null,options, options0); if(option = JOptionPane.OK_OPTION) System.exit(0); ); /创建编辑菜单上的各个菜单项并添加到菜单上 MenuItem undoitem = new MenuItem(撤销(U)); undoitem.setShortcut(new MenuShortcut(KeyEvent.VK_Z,false); editmenu.add(undoitem); MenuItem cutitem = new M

23、enuItem(剪切(T)); cutitem.setShortcut(new MenuShortcut(KeyEvent.VK_X,false); editmenu.add(cutitem); MenuItem copyitem = new MenuItem(复制(C)); copyitem.setShortcut(new MenuShortcut(KeyEvent.VK_C,false); editmenu.add(copyitem); MenuItem pasteitem = new MenuItem(粘贴(P)); pasteitem.setShortcut(new MenuShort

24、cut(KeyEvent.VK_V,false); editmenu.add(pasteitem); MenuItem deleteitem = new MenuItem(删除(L)); deleteitem.setShortcut(new MenuShortcut(KeyEvent.VK_DELETE,false); editmenu.add(deleteitem); /添加监听器来实现编辑菜单上的各个菜单项的功能 /撤销菜单项的功能实现 undoitem.addActionListener(new ActionListener() public void actionPerformed(A

25、ctionEvent e) ); /剪切菜单项的功能实现 cutitem.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) String text = content.getSelectedText(); StringSelection selection = new StringSelection(text); clipboard.setContents(selection, null); if(text.length() = 0) return; else content.re

26、placeRange(, content.getSelectionStart(),content.getSelectionEnd(); ); /复制菜单项的功能实现 copyitem.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) String text = content.getSelectedText(); StringSelection selection = new StringSelection(text); clipboard.setContents(selectio

27、n, null); ); /粘贴菜单项的功能实现 pasteitem.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) Transferable contents = clipboard.getContents(this); String str =null; try str = (String) contents.getTransferData(DataFlavor.stringFlavor); catch (UnsupportedFlavorException e1) e1.p

28、rintStackTrace(); catch (IOException e1) e1.printStackTrace(); if (str = null) return; try content.replaceRange(str,content.getSelectionStart(),content.getSelectionEnd(); catch (Exception e1) e1.printStackTrace(); ); /删除菜单项的功能实现 deleteitem.addActionListener(new ActionListener() public void actionPer

29、formed(ActionEvent e) content.replaceRange(,content.getSelectionStart(),content.getSelectionEnd(); ); /创建格式菜单上的各个菜单项并添加到菜单上 MenuItem worditem = new MenuItem(字体(F)); formatmenu.add(worditem); worditem.setEnabled(true); formatmenu.addSeparator(); MenuItem coloritem = new MenuItem(字体颜色(C)); formatmenu.add(colorit

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1