简易JAVA记事本小程序Word格式文档下载.docx
《简易JAVA记事本小程序Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《简易JAVA记事本小程序Word格式文档下载.docx(19页珍藏版)》请在冰豆网上搜索。
importjavax.swing.JOptionPane;
importjavax.swing.JPanel;
importjavax.swing.JPopupMenu;
importjavax.swing.JRadioButton;
importjavax.swing.JScrollPane;
importjavax.swing.JTextArea;
importjavax.swing.JTextField;
importjavax.swing.ScrollPaneConstants;
importjavax.swing.filechooser.FileFilter;
importjavax.swing.undo.UndoManager;
/**
*@authortarena-sqc
*@time2013-5-27
*/
publicclassNotePadextendsJFrame{
privatestaticfinallongserialVersionUID=-3688252937725788365L;
privateMyJTextAreaarea;
privateJScrollPanepane;
privateJMenuBarmenuBar;
publicJTextAreagetArea(){
returnarea;
}
publicNotePad(){
area=newMyJTextArea();
pane=newJScrollPane(area);
pane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
menuBar=initBar();
/**初始化菜单栏*/
privateJMenuBarinitBar(){
JMenuBarbar=newJMenuBar();
String[]mainMenuText=newString[]{"
文件"
"
编辑"
格式"
查看"
帮助"
};
String[][]subMenuText=newString[][]{{"
打开"
保存"
另存为"
退出"
},
{"
撤销"
恢复"
剪切"
复制"
粘贴"
},{"
自动换行"
字体"
状态栏"
关于记事本"
}};
Fontfont=newFont("
宋体"
0,14);
MyActionListenerlen=newMyActionListener(this);
for(inti=0;
i<
mainMenuText.length;
i++){
JMenumainMenu=newJMenu(mainMenuText[i]);
mainMenu.setFont(font);
for(intj=0;
j<
subMenuText[i].length;
j++){
JMenuItemsubMenu=newJMenuItem(subMenuText[i][j]);
subMenu.setFont(font);
mainMenu.add(subMenu);
if(j%2==0)
mainMenu.addSeparator();
//添加分割线
subMenu.setActionCommand(subMenuText[i][j]);
//设置监听器标志
subMenu.addActionListener(len);
//追加监听器
}
bar.add(mainMenu);
}
returnbar;
/**窗口显示函数*/
publicvoidshowMe(){
//屏幕居中
Toolkitkit=Toolkit.getDefaultToolkit();
this.setLocation(kit.getScreenSize().width/2-300,
kit.getScreenSize().height/2-200);
this.setJMenuBar(menuBar);
this.add(pane);
this.setTitle("
记事本"
);
this.setSize(600,400);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
publicstaticvoidmain(String[]args){
newNotePad().showMe();
}
/**关于文件操作类*/
classFileOperation{//所有文件操作
privateNotePadnotePad;
privateJTextAreaarea;
privateJFileChooserch;
privateFilefile;
privateUndoManagerundo;
publicFileOperation(NotePadnotePad){
this.notePad=notePad;
ch=initJFileChooser();
area=notePad.getArea();
undo=newUndoManager();
//设置撤销恢复监听器
area.getDocument().addUndoableEditListener(undo);
privateJFileChooserinitJFileChooser(){
JFileChooserch=newJFileChooser();
ch.setFileFilter(newFileFilter(){
@Override
publicStringgetDescription(){
return"
*.txt"
;
publicbooleanaccept(Filef){
returnf.getName().endsWith("
.txt"
});
returnch;
/**打开文件操作*/
publicvoidopenFile(){
try{
intstate=ch.showOpenDialog(notePad);
if(state==JFileChooser.CANCEL_OPTION)
return;
file=ch.getSelectedFile();
BufferedReaderreader=newBufferedReader(newInputStreamReader(
newFileInputStream(file)));
StringBuffersb=newStringBuffer();
Stringstr;
while((str=reader.readLine())!
=null){
sb.append(str).append("
\n"
area.setText(sb.toString());
reader.close();
}catch(Exceptione){
e.printStackTrace();
/**保存文件操作*/
publicvoidsaveFile(){
if(file!
writeFile(file);
}else{
intstute=ch.showSaveDialog(notePad);
if(stute==JFileChooser.CANCEL_OPTION)
privatevoidwriteFile(Filef){
PrintWriterpw=newPrintWriter(f);
pw.println(area.getText());
pw.close();
}catch(FileNotFoundExceptione){
/**另存为文件操作*/
publicvoidotherFile(){
intstute=ch.showSaveDialog(notePad);
if(stute==JFileChooser.CANCEL_OPTION)
return;
file=ch.getSelectedFile();
writeFile(file);
/**撤销文件操作*/
publicvoidundo(){
if(undo.canUndo()){
undo.undo();
JOptionPane.showMessageDialog(notePad,"
不能撤销"
/**恢复文件操作*/
publicvoidredo(){
if(undo.canRedo()){
undo.redo();
不能恢复"
/**剪切文件操作*/
publicvoidcut(){
area.cut();
/**复制文件操作*/
publicvoidcopy(){
area.copy();
/**粘贴文件操作*/
publicvoidpaste(){
area.paste();
/**退出*/
publicvoidexit(){
intstate=JOptionPane.showConfirmDialog(notePad,"
您确定要退出了?
"
if(state==JOptionPane.OK_OPTION)
System.exit(0);
/**字体*/
publicvoidfont(){
newFontDialog(notePad,area);
/**关于记事本*/
publicvoidabout(){
JOptionPane
.showMessageDialog(notePad,
"
本程序由大庆中心达内科技教学经理-XXX开发\n如有问题请联系QQ:
271224258\n达内科技网址:
classMyActionListenerimplementsActionListener{
privateFileOperationfileOperation;
publicMyActionListener(NotePadnotePad){
fileOperation=newFileOperation(notePad);
@Override
publicvoidactionPerformed(ActionEvente){
Stringcommand=e.getActionCommand();
if(command.equals("
)){
fileOperation.openFile();
}elseif(command.equals("
fileOperation.saveFile();
fileOperation.otherFile();
fileOperation.exit();
fileOperation.undo();
fileOperation.redo();
fileOperation.cut();
fileOperation.copy();
fileOperation.paste();
fileOperation.font();
fileOperation.about();
classMyJTextAreaextendsJTextAreaimplementsMouseListener{
privatestaticfinallongserialVersionUID=3925928674894108922L;
privateJPopupMenupop;
privateJMenuItemcut,copy,paste;
publicMyJTextArea(){
this.addMouseListener(this);
pop=newJPopupMenu();
cut=newJMenuItem("
copy=newJMenuItem("
paste=newJMenuItem("
pop.add(cut);
pop.addSeparator();
pop.add(copy);
pop.add(paste);
actions();
this.add(pop);
publicvoidactions(){
copy.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
MyJTextArea.this.copy();
cut.addActionListener(newActionListener(){
MyJTextArea.this.cut();
paste.addActionListener(newActionListener(){
MyJTextArea.this.paste();
/**
*剪切板中是否有文本数据可供粘贴
*
*@returntrue为有文本数据
publicbooleanisClipboardString(){
booleanb=false;
Clipboardclipboard=this.getToolkit().getSystemClipboard();
Transferablecontent=clipboard.getContents(this);
if(content.getTransferData(DataFlavor.stringFlavor)instanceofString){
b=true;
returnb;
*文本组件中是否具备复制的条件
*@returntrue为具备
publicbooleanisCanCopy(){
intstart=this.getSelectionStart();
intend=this.getSelectionEnd();
if(start!
=end)
b=true;
publicvoidmouseClicked(MouseEvente){
publicvoidmousePressed(MouseEvente){
publicvoidmouseReleased(MouseEvente){
if(e.getButton()==MouseEvent.BUTTON3){
copy.setEnabled(isCanCopy());
paste.setEnabled(isClipboardString());
cut.setEnabled(isCanCopy());
pop.show(this,e.getX(),e.getY());
publicvoidmouseEntered(MouseEvente){
publicvoidmouseExited(MouseEvente){
/**字体对话框*/
classFontDialogimplementsActionListener,ItemListener{
privateListfontname,fontshape,fontsize;
privateJPanelpname,pshape,psize,pmodel,prb;
privateJTextFieldtxtname,txtshape,txtsize;
privateJTextAreatxtmodel;
privateJDialogdialog;
privateJButtonbtnok,btncancel;
privateJRadioButtonrbzw,rbyw,rbsz;
privateFontmodelfont;
publicFontDialog(JFramef,JTextAreaarea){
this.area=area;
GraphicsEnvironmentg=GraphicsEnvironment
.getLocalGraphicsEnvironment();
Stringname[]=g.getAvailableFontFamilyNames();
Stringshape[]={"
常规"
斜体"
粗体倾斜"
粗体"
fontname=newList(