




已阅读5页,还剩21页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
package com.java.notepad;import javax.swing.*;import javax.swing.border.Border;import javax.swing.event.MenuEvent;import javax.swing.event.MenuListener;import javax.swing.event.UndoableEditEvent;import javax.swing.event.UndoableEditListener;import javax.swing.filechooser.FileFilter;import java.awt.*;import java.awt.datatransfer.Clipboard;import java.awt.datatransfer.DataFlavor;import java.awt.datatransfer.Transferable;import java.awt.event.*;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.util.Vector;import javax.swing.JDialog;import javax.swing.undo.UndoManager;/* * 记事本 * author WMeng * version 1.0 2012.08.15 * */public class NotePad public static void main(String args)NotePadFrame frame=new NotePadFrame();frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setVisible(true);/frame.textArea.requestFocus();frame.textArea.requestFocusInWindow();class NotePadFrame extends JFrame public NotePadFrame()init();public NotePadFrame(int x,int y)this.setLocation(x,y);init();JTextArea textArea=new JTextArea(15,50);JScrollPane scrollPane;JPopupMenu popupmenu;boolean isSave=false;private void init()final UndoManager manager = new UndoManager();textArea.getDocument().addUndoableEditListener(new UndoableEditListener() public void undoableEditHappened(UndoableEditEvent e) manager.addEdit(e.getEdit(); );/设置窗体观感String plaf=com.sun.java.swing.plaf.windows.WindowsLookAndFeel;try UIManager.setLookAndFeel(plaf);SwingUtilities.updateComponentTreeUI(this); catch (ClassNotFoundException e) / TODO Auto-generated catch blocke.printStackTrace(); catch (InstantiationException e) / TODO Auto-generated catch blocke.printStackTrace(); catch (IllegalAccessException e) / TODO Auto-generated catch blocke.printStackTrace(); catch (UnsupportedLookAndFeelException e) / TODO Auto-generated catch blocke.printStackTrace();/UIManager.LookAndFeelInfo infos=UIManager.getInstalledLookAndFeels();/for(UIManager.LookAndFeelInfo i:infos)/System.out.println(i);/获取计算机平铺宽度、高度int screenWidth=Toolkit.getDefaultToolkit().getScreenSize().width;int screenHeight=Toolkit.getDefaultToolkit().getScreenSize().height; Insets screenInsets = Toolkit.getDefaultToolkit().getScreenInsets(this.getGraphicsConfiguration(); /不管任务栏移动桌面什么位置,都可以返回所需要的窗口,也就得到了所需要窗口方形 int width=screenInsets.right; int height=screenInsets.bottom; Image img=Toolkit.getDefaultToolkit().getImage(image/title.png); this.setIconImage(img);this.setTitle(记事本);this.setSize(screenWidth-width)/2,(screenHeight-height)/2);this.setLocation(500,200);x=this.getX();y=this.getY();textArea.setLineWrap(true);/激活自动换行功能textArea.setWrapStyleWord(true);/激活断行不断字功能 /* * 工具栏 */JToolBar toolBar=new JToolBar();JButton xin=new JButton(); xin.setIcon(new ImageIcon(image/toolbar/newFile.png); xin.setToolTipText(新建); xin.addActionListener(new ActionListener() /定义新建组件操作 Override public void actionPerformed(ActionEvent arg0) System.out.println(ok); NotePadFrame f=new NotePadFrame(x+20,y+20); f.setVisible(true); );JButton open=new JButton(); open.setIcon(new ImageIcon(image/toolbar/openFile.png); open.setToolTipText(打开); open.addActionListener(new ActionListener() /定义打开组件操作 Override public void actionPerformed(ActionEvent arg0) chooseToOpen(); );JButton saveFile=new JButton(); saveFile.setIcon(new ImageIcon(image/toolbar/saveFile.png); saveFile.setToolTipText(保存); saveFile.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) chooseToSave(); );JButton copy=new JButton(); copy.setIcon(new ImageIcon(image/toolbar/copy.png); copy.setToolTipText(复制); copy.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) textArea.copy(); textArea.requestFocusInWindow(); );JButton cut=new JButton(); cut.setIcon(new ImageIcon(image/toolbar/cut.png); cut.setToolTipText(剪切); cut.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) textArea.cut(); textArea.requestFocusInWindow(); );JButton paste=new JButton(); paste.setIcon(new ImageIcon(image/toolbar/paste.png); paste.setToolTipText(粘贴); paste.setEnabled(isClipboardString(); paste.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) textArea.paste(); textArea.requestFocusInWindow(); );JButton undo=new JButton(); undo.setIcon(new ImageIcon(image/toolbar/undo.png); undo.setToolTipText(撤销); undo.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) if(manager.canUndo() manager.undo(); textArea.requestFocusInWindow(); else JOptionPane.showOptionDialog(null, 没有可撤销的内容, 错误, JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, null, null); textArea.requestFocusInWindow(); );JButton redo=new JButton(); redo.setIcon(new ImageIcon(image/toolbar/redo.png); redo.setToolTipText(重复); redo.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) if(manager.canRedo() manager.redo(); textArea.requestFocusInWindow(); else JOptionPane.showOptionDialog(null, 没有可重复的内容, 错误, JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, null, null); textArea.requestFocusInWindow(); );/JLabel findLabel=new JLabel(查找);JTextField findTextField=new JTextField();JButton findButton=new JButton(查找);findButton.setIcon(new ImageIcon(image/toolbar/find.png);toolBar.add(xin);toolBar.add(open);toolBar.add(saveFile);toolBar.addSeparator();toolBar.add(copy);toolBar.add(cut);toolBar.add(paste);toolBar.addSeparator();toolBar.add(undo);toolBar.add(redo);toolBar.addSeparator();/toolBar.add(findLabel);toolBar.add(findTextField);toolBar.add(findButton);this.add(toolBar,BorderLayout.NORTH); /* * 菜单栏 */JMenuBar menuBar=new JMenuBar();this.setJMenuBar(menuBar);JMenu file=new JMenu(文件(F);file.setMnemonic(F); JMenuItem newMenuItem=new JMenuItem(新建(N),N); newMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, InputEvent.CTRL_MASK); newMenuItem.addActionListener(new ActionListener() public void actionPerformed(ActionEvent arg0) System.out.println(ok); new NotePadFrame(); ); JMenuItem openMenuItem=new JMenuItem(打开(O),O); openMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, InputEvent.CTRL_MASK); openMenuItem.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) chooseToOpen(); ); final JMenuItem save=new JMenuItem(保存(S),S); save.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_MASK); save.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) chooseToSave(); ); JMenuItem saveAs=new JMenuItem(另存为); saveAs.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) chooseToSave(); ); JMenuItem exit=new JMenuItem(退出); exit.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) System.exit(0); ); file.add(newMenuItem); file.add(openMenuItem); file.add(save); file.add(saveAs); file.addSeparator(); file.add(exit); file.addMenuListener(new MenuListener()Overridepublic void menuCanceled(MenuEvent e) / TODO Auto-generated method stubOverridepublic void menuDeselected(MenuEvent e) / TODO Auto-generated method stubOverridepublic void menuSelected(MenuEvent e) / TODO Auto-generated method stubif(isSave=true)save.setEnabled(false); );JMenu edit=new JMenu(编辑(E);edit.setMnemonic(E); final JMenuItem undoMenuItem=new JMenuItem(撤销(Z),Z); undoMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z, InputEvent.CTRL_MASK); undoMenuItem.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) manager.undo(); ); final JMenuItem redoMenuItem=new JMenuItem(重复(Y),Y); redoMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Y, InputEvent.CTRL_MASK); redoMenuItem.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) manager.redo(); ); final JMenuItem cutMenuItem=new JMenuItem(剪切(X),X); cutMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, InputEvent.CTRL_MASK); cutMenuItem.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) textArea.cut(); ); final JMenuItem copyMenuItem=new JMenuItem(复制(C),C); copyMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.CTRL_MASK); copyMenuItem.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) textArea.copy(); ); final JMenuItem pasteMenuItem=new JMenuItem(粘贴(P),P); pasteMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, InputEvent.CTRL_MASK); pasteMenuItem.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) textArea.paste(); ); JMenuItem findMenuItem=new JMenuItem(查找/替换(F),F); findMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F, InputEvent.CTRL_MASK); findMenuItem.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) ); JMenuItem findNextMenuItem=new JMenuItem(查找下一个(K),K); findNextMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_K, InputEvent.CTRL_MASK); findNextMenuItem.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) ); JMenuItem selevtAllMenuItem=new JMenuItem(全选(A),A); selevtAllMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, InputEvent.CTRL_MASK); selevtAllMenuItem.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) textArea.selectAll(); ); edit.add(undoMenuItem); edit.add(redoMenuItem); edit.addSeparator(); edit.add(copyMenuItem); edit.add(cutMenuItem); edit.add(pasteMenuItem); edit.addSeparator(); edit.add(findMenuItem); edit.add(findNextMenuItem); edit.add(selevtAllMenuItem); edit.addMenuListener(new MenuListener()Overridepublic void menuCanceled(MenuEvent arg0) / TODO Auto-generated method stubOverridepublic void menuDeselected(MenuEvent arg0) / TODO Auto-generated method stubOverridepublic void menuSelected(MenuEvent arg0) / TODO Auto-generated method stubcutMenuItem.setEnabled(isCanCopy();copyMenuItem.setEnabled(isCanCopy();pasteMenuItem.setEnabled(isClipboardString();undoMenuItem.setEnabled(manager.canUndo();redoMenuItem.setEnabled(manager.canRedo(););JMenu format=new JMenu(格式(O); format.setMnemonic(O); final JCheckBoxMenuItem autoWrap=new JCheckBoxMenuItem(自动换行); autoWrap.setSelected(true); autoWrap.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) if(autoWrap.isSelected() textArea.setLineWrap(true);/激活自动换行功能 textArea.setWrapStyleWord(true);/激活断行不断字功能 else textArea.setLineWrap(false);/激活自动换行功能 textArea.setWrapStyleWord(false);/激活断行不断字功能 ); JMenuItem font=new JMenuItem(字体(F),F); font.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) fontSet(); ); JMenuItem bgColor=new JMenuItem(背景色(C),C); bgColor.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) final JColorChooser bcolor=new JColorChooser(); JDialog dialog=JColorChooser.createDialog(textArea, 背景色设置,false,bcolor, new ActionListener() public void actionPerformed(ActionEvent e) textArea.setBackground(bcolor.getColor(); ,null); dialog.setResizable(false); dialog.setVisible(true); ); JMenuItem fgColor=new JMenuItem(前景色(K),K); fgColor.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) final JColorChooser bcolor=new JColorChooser(); JDialog dialog=JColorChooser.createDialog(textArea, 前景色设置,false,bcolor, new ActionListener() public void actionPerformed(ActionEvent e) textArea.setForeground(bcolor.getColor(); ,null); dialog.setResizable(false); dialog.setVisible(true); ); format.add(autoWrap); format.add(font); format.addSeparator(); format.add(bgColor); format.add(fgColor);JMenu findlook=new JMenu(查看(V); findlook.setMnemonic(V); JCheckBoxMenuItem status=new JCheckBoxMenuItem(状态栏); JMenuItem wordNumber=new JMenuItem(总字数); wordNumber.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) JDialog wordNumberDialog=new JDialog(); wordNumberDialog.setTitle(总字数); JLabel words=new JLabel(); JLabel lines=new JLabel(); JPanel panel=new JPanel(); words.setText(共有+textArea.getText().length()+字); lines.setText(共+textArea.getLineCount()+行); panel.add(words); panel.add(lines); wordNumberDialog.add(panel); System.out.println(textArea.getText().length(); wordNumberDialog.setSize(200,80); wordNumberDialog.setLocation(x+200,y+200); wordNumberDialog.setVisible(true); ); findlook.add(status); findlook.add(wordNumber);JMenu help=new JMenu(帮助(H); help.setMnemonic(H); JMenuItem about=new JMenuItem(关于记事本); about.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) / 显示对话框 JOptionPane.showOptionDialog(null, NotePad+ v1.0n + 程序设计:王盟盟 n + 简介:n + 一个简单的文字编辑器n + 可作为验收Java的实现对象n + 欢迎网友下载研究交流nn + , NotePad, JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, null, null); ); help.add(about); menuBar.add(file);menuBar.add(edit);menuBar.add(format);menuBar.add(findlook);menuBar.add(help); /* * 弹出菜单 */ popupmenu=new JPopupMenu(); final JMenuItem undoPopupItem=new JMenuItem(撤销(Z),Z); undoPopupItem.setAccelerator(KeyStroke.getKeyStroke(ctrl z); undoPopupItem.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) manager.undo(); ); final JMenuItem redoPopupItem=new JMenuItem(重复(Y),Y); redoPopupItem.setAccelerator(KeyStroke.getKeyStroke(ctrl y); redoPopupItem.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) manager.redo(); ); final JMenuItem cutPopupItem=new JMenuItem(剪切(X),X); cutPopupItem.setAccelerator(KeyStroke.getKe
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年音乐教师资格考试卷及答案
- 2025年社会工作与社会福利专业试卷及答案
- 2025年社会工作实务课程考试试卷及答案
- 2025年房地产经营管理考试试卷及答案
- 2025年机械设计基础试题及答案
- 2025年教师资格证考试试卷及答案
- 石料加工销售合同协议书
- 七级书法考试试题及答案
- 餐饮房租租赁合同协议书
- 2025年节能型泵及环保用泵项目合作计划书
- 电网工程设备材料信息参考价2025年第一季度
- 江苏南京茉莉环境投资有限公司招聘笔试题库2025
- 吸氧并发症预防及处理
- 针刺伤预防与处理(中华护理学会团体标准)
- 2024年安徽省初中学业水平考试生物试题含答案
- 2024年浙江省中考英语试题卷(含答案解析)
- MOOC 理解马克思-南京大学 中国大学慕课答案
- 说明书hid500系列变频调速器使用说明书s1.1(1)
- RTO处理工艺PFD计算
- 最美中铝人申报表
- 柑橘采摘机器人的结构设计说明书
评论
0/150
提交评论