完整版JAVA记事本源代码毕业设计.docx
《完整版JAVA记事本源代码毕业设计.docx》由会员分享,可在线阅读,更多相关《完整版JAVA记事本源代码毕业设计.docx(57页珍藏版)》请在冰豆网上搜索。
完整版JAVA记事本源代码毕业设计
以下文档格式全部为word格式,下载后您可以任意修改编辑。
JAVA记事本源代码
importjava.awt.*;
importjava.awt.event.*;
importjava.text.*;
importjava.util.*;
importjava.io.*;
importjavax.swing.undo.*;
importjavax.swing.*;
importjavax.swing.event.*;
importjava.awt.datatransfer.*;
importjava.util.List;
publicclassNotepad4extendsJFrameimplementsActionListener,DocumentListener{
JMenumFile,mEdit,mMode,mView,mHelp;
---文件菜单
JMenuItemmFile_New,mFile_Open,mFile_Save,mFile_ASave,mFile_Print,mFile_Exit;
---编辑菜单
JMenuItemmEdit_Undo,mEdit_Cut,mEdit_Copy,mEdit_Paste,mEdit_Del,mEdit_Search,mEdit_SearchNext,mEdit_Replace,mEdit_Turnto,mEdit_SelectAll,mEdit_TimeDate;
---格式菜单
JCheckBoxMenuItemformatMenu_LineWrap;
JMenuformatMenu_Color;
JMenuItemformatMenu_Font,formatMenu_Color_FgColor,formatMenu_Color_BgColor;
---查看菜单
JCheckBoxMenuItemviewMenu_Status;
---帮助菜单
JMenuItemmHelp_HelpTopics,mHelp_About;
---弹出菜单级菜单项
JPopupMenupopupMenu;
JMenuItempopupMenu_Undo,popupMenu_Cut,popupMenu_Copy,popupMenu_Paste,popupMenu_Delete,popupMenu_SelectAll;
---工具栏按钮
JButtonnewButton,openButton,saveButton,saveAsButton,printButton,undoButton,redoButton,cutButton,copyButton,pasteButton,deleteButton,searchButton,timeButton,fontButton,boldButton,
italicButton,fgcolorButton,bgcolorButton,;
文本编辑区域
staticJTextAreaText;
状态栏标签
JLabelstatusLabel1,statusLabel2,statusLabel3;
JToolBarstatusBar;
---系统剪贴板
ToolkittoolKit=Toolkit.getDefaultToolkit();
ClipboardclipBoard=toolKit.getSystemClipboard();
---创建撤消操作管理器
protectedUndoManagerundo=newUndoManager();
protectedUndoableEditListenerundoHandler=newUndoHandler();
----其它变量
booleanisNewFile=true;是否新文件(未保存过的)
FilecurrentFile;当前文件名
StringoldValue;存放编辑区原来的内容,用于比较文本是否有改动
JButtonfontOkButton;字体设置里的"确定"按钮
----设置编辑区默认字体
protectedFontdefaultFont=newFont("宋体",Font.PLAIN,12);
GregorianCalendartime=newGregorianCalendar();
int=time.get(Calendar.MINUTE);
intsecond=time.get(Calendar.SECOND);
FilesaveFileName=null,fileName=null;
publicNotepad4(){
super("VXBB的记事本");
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
Containercontainer=getContentPane();
System.out.println(Text.getDragEnabled());支持自动拖放
JScrollPanescroll=newJScrollPane(Text);
scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
Text.setWrapStyleWord(true);设置单词在一行不足容纳时换行
Text.setLineWrap(true);
Text.setFont(defaultFont);设置编辑区默认字体
Text.setBackground(Color.white);设置编辑区默认背景色
Text.setForeground(Color.black);设置编辑区默认前景色
oldValue=Text.getText();获取原文本编辑区的内容
--编辑区注册事件监听
Text.getDocument().addUndoableEditListener(undoHandler);添加负责通知任何更改的撤消侦听器
Text.getDocument().addDocumentListener(this);添加负责通知任何更改的文档侦听器
JMenuBarMenuBar=newJMenuBar();
mFile=newJMenu("文件(F)",true);创建菜单
mEdit=newJMenu("编辑(E)",true);
mMode=newJMenu("格式(O)",true);
mView=newJMenu("查看(V)",true);
mHelp=newJMenu("帮助(H)",true);
mEdit.addActionListener(newActionListener()注册事件监听
{
publicvoidactionPerformed(ActionEvente){
checkMenuItemEnabled();设置剪切、复制、粘贴、删除等功能的可用性
}
});
mFile.setMnemonic('F');
mEdit.setMnemonic('E');
mMode.setMnemonic('O');
mView.setMnemonic('V');
mHelp.setMnemonic('H');
MenuBar.add(mFile);
MenuBar.add(mEdit);
MenuBar.add(mMode);
MenuBar.add(mView);
MenuBar.add(mHelp);
--文件菜单
mFile_New=newJMenuItem("新建(N)",'N');
mFile_Open=newJMenuItem("打开(O)",'O');
mFile_Save=newJMenuItem("保存(S)",'S');
mFile_ASave=newJMenuItem("另存为(A)",'A');
mFile_Print=newJMenuItem("打印(P)",'P');
mFile_Exit=newJMenuItem("退出(X)",'X');
mFile_New.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,InputEvent.CTRL_MASK));
mFile_Open.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,InputEvent.CTRL_MASK));
mFile_Save.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK));
mFile_Print.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P,InputEvent.CTRL_MASK));
mFile_New.addActionListener(this);注册事件监听
mFile_Open.addActionListener(this);
mFile_Save.addActionListener(this);
mFile_ASave.addActionListener(this);
mFile_Print.addActionListener(this);
mFile_Exit.addActionListener(this);
mFile.add(mFile_New);添加菜单项
mFile.add(mFile_Open);
mFile.add(mFile_Save);
mFile.add(mFile_ASave);
mFile.addSeparator();添加分割线
mFile.add(mFile_Print);
mFile.addSeparator();添加分割线
mFile.add(mFile_Exit);
--编辑菜单
mEdit_Undo=newJMenuItem("撤消(U)",'U');
mEdit_Cut=newJMenuItem("剪切(T)",'T');
mEdit_Copy=newJMenuItem("复制(C)",'C');
mEdit_Paste=newJMenuItem("粘贴(P)",'P');
mEdit_Del=newJMenuItem("删除(L)",'L');
mEdit_Search=newJMenuItem("查找(F)",'F');
mEdit_SearchNext=newJMenuItem("查找下一个(N)",'N');
mEdit_Replace=newJMenuItem("替换(R)",'R');
mEdit_Turnto=newJMenuItem("转到(G)",'G');
mEdit_SelectAll=newJMenuItem("全选(A)",'A');
mEdit_TimeDate=newJMenuItem("时间日期(D)",'D');
mEdit_Cut.setEnabled(false);
mEdit_Undo.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z,InputEvent.CTRL_MASK));
mEdit_Cut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,InputEvent.CTRL_MASK));
mEdit_Copy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_MASK));
mEdit_Paste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,InputEvent.CTRL_MASK));
mEdit_Del.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE,0));
mEdit_Search.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F,InputEvent.CTRL_MASK));
mEdit_SearchNext.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F3,0));
mEdit_Replace.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_H,InputEvent.CTRL_MASK));
mEdit_Turnto.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_G,InputEvent.CTRL_MASK));
mEdit_SelectAll.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,InputEvent.CTRL_MASK));
mEdit_TimeDate.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F5,0));
mEdit_Undo.addActionListener(this);注册事件监听
mEdit_Cut.addActionListener(this);
mEdit_Copy.addActionListener(this);
mEdit_Paste.addActionListener(this);
mEdit_Del.addActionListener(this);
mEdit_Search.addActionListener(this);
mEdit_SearchNext.addActionListener(this);
mEdit_Replace.addActionListener(this);
mEdit_Turnto.addActionListener(this);
mEdit_SelectAll.addActionListener(this);
mEdit_TimeDate.addActionListener(this);
mEdit.add(mEdit_Undo);添加菜单项
mEdit.addSeparator();添加分割线
mEdit.add(mEdit_Cut);
mEdit.add(mEdit_Copy);
mEdit.add(mEdit_Paste);
mEdit.add(mEdit_Del);
mEdit.addSeparator();
mEdit.add(mEdit_Search);
mEdit.add(mEdit_SearchNext);
mEdit.add(mEdit_Replace);
mEdit.add(mEdit_Turnto);
mEdit.addSeparator();
mEdit.add(mEdit_SelectAll);
mEdit.add(mEdit_TimeDate);
--格式菜单
formatMenu_LineWrap=newJCheckBoxMenuItem("自动换行(W)");
formatMenu_LineWrap.setMnemonic('W');
formatMenu_LineWrap.setState(true);
formatMenu_Font=newJMenuItem("字体(F)",'F');
formatMenu_Color=newJMenu("颜色");
formatMenu_Color_FgColor=newJMenuItem("字体颜色");
formatMenu_Color_BgColor=newJMenuItem("背景颜色");
formatMenu_LineWrap.addActionListener(this);注册事件监听
formatMenu_Font.addActionListener(this);
formatMenu_Color_FgColor.addActionListener(this);
formatMenu_Color_BgColor.addActionListener(this);
mMode.add(formatMenu_LineWrap);添加菜单项
mMode.addSeparator();
mMode.add(formatMenu_Font);
mMode.add(formatMenu_Color);
formatMenu_Color.add(formatMenu_Color_FgColor);
formatMenu_Color.add(formatMenu_Color_BgColor);
--查看菜单
viewMenu_Status=newJCheckBoxMenuItem("状态栏(S)");
viewMenu_Status.setMnemonic('S');
viewMenu_Status.setState(true);
viewMenu_Status.addActionListener(this);
mView.add(viewMenu_Status);
--帮助菜单
mHelp_HelpTopics=newJMenuItem("帮助(H)",'H');
mHelp_About=newJMenuItem("关于(A)",'A');
mHelp_HelpTopics.addActionListener(this);
mHelp_About.addActionListener(this);
mHelp.add(mHelp_HelpTopics);
mHelp.addSeparator();添加分割线
mHelp.add(mHelp_About);
创建右键弹出菜单
popupMenu=newJPopupMenu();
popupMenu_Undo=newJMenuItem("撤消(U)",'U');
popupMenu_Cut=newJMenuItem("剪切(T)",'T');
popupMenu_Copy=newJMenuItem("复制(C)",'C');
popupMenu_Paste=newJMenuItem("粘贴(P)",'P');
popupMenu_Delete=newJMenuItem("删除(D)",'D');
popupMenu_SelectAll=newJMenuItem("全选(A)",'A');
popupMenu_Undo.setEnabled(false);撤消选项初始设为不可用
---向右键菜单添加菜单项和分隔符
popupMenu.add(popupMenu_Undo);
popupMenu.addSeparator();
popupMenu.add(popupMenu_Cut);
popupMenu.add(popupMenu_Copy);
popupMenu.add(popupMenu_Paste);
popupMenu.add(popupMenu_Delete);
popupMenu.addSeparator();
popupMenu.add(popupMenu_SelectAll);
右键菜单注册事件
popupMenu_Undo.addActionListener(this);
popupMenu_Cut.addActionListener(this);
popupMenu_Copy.addActionListener(this);
popupMenu_Paste.addActionListener(this);
popupMenu_Delete.addActionListener(this);
popupMenu_SelectAll.addActionListener(this);
文本编辑区注册右键菜单事件
Text.addMouseListener(newMouseAdapter(){
publicvoidmousePressed(MouseEvente){
checkForTriggerEvent(e);
}
publicvoidmouseReleased(MouseEvente){
checkForTriggerEvent(e);
}
privatevoidcheckForTriggerEvent(MouseEvente){
if(e.isPopupTrigger())
popupMenu.show(e.getComponent(),e.getX(),e.getY());在组件调用者的坐标空间中的位置
X、Y
显示弹出菜单。
else{
statusLabel3.setText("当前光标所在行数:
"+getlineNumber());
}
checkMenuItemEnabled();设置剪切、复制、粘贴、删除等功能的可用性
Text.requestFocus();编辑区获取焦点
}
});
----创建工具栏
JPaneltoolBar=newJPanel();
toolBar.setLayout(newFlowLayout(FlowLayout.LEFT));
IconnewIcon=newImageIcon("Iconsnew.gif");
IconopenIcon=newImageIcon("Iconsopen.gif");
IconsaveIcon=newImageIcon("Iconssave.gif");
IconsaveAsIcon=newImageIcon("Iconssaveas.gif");
IconprintIcon=newImageIcon("Iconsprint.gif");
IconundoIcon=newImageIcon("Iconsundo.gif");
IconcutIcon=newImageIcon("Iconscut.gif");
IconcopyIcon=newImageIcon("Iconscopy.gif");
IconpasteIcon=newImageIcon(