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