您好,欢迎访问三七文档
当前位置:首页 > 建筑/环境 > 工程监理 > java编程-记事本-全部功能都实现
两个放到同个包中,一起运行1.记事本.javaimportjavax.swing.text.BadLocationException;publicclass记事本{publicstaticvoidmain(Stringargs[]){Windowwindow1=newWindow();window1.setTitle(记事本);window1.setVisible(true);}}2.Window.javaimportjavax.swing.*;importjavax.swing.JOptionPane;importjava.io.*;importjava.awt.BorderLayout;importjava.awt.Color;importjava.awt.Component;importjava.awt.Font;importjava.awt.Frame;importjava.awt.GraphicsEnvironment;importjava.awt.event.*;importjava.awt.*;importjava.awt.print.*;importjava.util.*;importjava.lang.Object;importjava.lang.Throwable;importjava.lang.Exception;importjavax.swing.text.BadLocationException;importjavax.swing.JFileChooser;importjavax.swing.event.ListSelectionEvent;importjavax.swing.event.ListSelectionListener;importjavax.swing.filechooser.FileNameExtensionFilter;publicclassWindowextendsJFrameimplementsActionListener{JMenuBarmenubar;JMenumenu,menu1,menu2,itemLine,menu3,menu4;JSplitPanesplitPane;JMenuItemitemNew,itemOpen,itemSave,itemSaveAs,itemPrint,itemExit;JMenuItemitemCopy,itemCut,itemPaste,itemDelete;JMenuItemitemFont,line1,line2,itemhelp,itemabout;JTextAreatext,text1;JLabellblStatus;JTextFieldfield;KeyHandlerkHandler=newKeyHandler();JPopupMenupopupMenu;JToolBarstatusBar;JCheckBoxMenuItemitemstate;Window(){init();intGUI();setBounds(500,150,500,500);setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);}voidintGUI(){text=newJTextArea();add(newJScrollPane(text),BorderLayout.CENTER);text.addKeyListener(kHandler);lblStatus=newJLabel(未修改);}voidinit(){//建立一个菜单menubar=newJMenuBar();//建立文件菜单项menu=newJMenu(文件(F));menubar.add(menu);setJMenuBar(menubar);itemNew=newJMenuItem(新建);itemOpen=newJMenuItem(打开);itemSave=newJMenuItem(保存);itemSaveAs=newJMenuItem(另存为);itemPrint=newJMenuItem(打印);itemExit=newJMenuItem(退出);menu.add(itemNew);menu.add(itemOpen);menu.add(itemSave);menu.add(itemSaveAs);menu.add(itemPrint);menu.add(itemExit);itemNew.addActionListener(this);itemNew.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,InputEvent.CTRL_MASK));itemOpen.addActionListener(this);itemOpen.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,InputEvent.CTRL_MASK));itemSave.addActionListener(this);itemSave.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK));itemSaveAs.addActionListener(this);itemPrint.addActionListener(this);itemPrint.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P,InputEvent.CTRL_MASK));itemExit.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){if(lblStatus.getText().equals(已修改)){intconfirm=JOptionPane.showConfirmDialog(null,文件已修改,要保存吗?);if(confirm==JOptionPane.OK_OPTION)save();elseif(confirm==JOptionPane.CANCEL_OPTION)return;elseif(confirm==JOptionPane.CLOSED_OPTION)return;}System.exit(0);}});itemExit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E,InputEvent.CTRL_MASK));menu1=newJMenu(编辑(E));//建立编辑菜单项menubar.add(menu1);setJMenuBar(menubar);itemCopy=newJMenuItem(复制);itemCut=newJMenuItem(剪切);itemPaste=newJMenuItem(粘贴);itemDelete=newJMenuItem(删除);menu1.add(itemCopy);menu1.add(itemCut);menu1.add(itemPaste);menu1.add(itemDelete);itemCopy.addActionListener(this);itemCopy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_MASK));itemCut.addActionListener(this);itemCut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,InputEvent.CTRL_MASK));itemPaste.addActionListener(this);itemPaste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,InputEvent.CTRL_MASK));itemDelete.addActionListener(this);itemDelete.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE,0));menu2=newJMenu(格式(O));//建立格式菜单项menubar.add(menu2);setJMenuBar(menubar);itemLine=newJMenu(自动换行);//建立二级菜单项line1=newJMenuItem(选择自动换行);line2=newJMenuItem(取消自动换行);itemLine.add(line1);itemLine.add(line2);menu2.add(itemLine);line1.addActionListener(this);line2.addActionListener(this);itemFont=newJMenuItem(字体);menu2.add(itemFont);itemFont.addActionListener(this);menu3=newJMenu(查看(V));//建立查看菜单项menubar.add(menu3);setJMenuBar(menubar);itemstate=newJCheckBoxMenuItem(状态栏);//设置选勾菜单项menu3.add(itemstate);itemstate.addActionListener(this);menu4=newJMenu(帮助(H));//建立帮助菜单项menubar.add(menu4);setJMenuBar(menubar);itemhelp=newJMenuItem(查看帮助);menu4.add(itemhelp);itemhelp.addActionListener(this);itemabout=newJMenuItem(关于记事本);menu4.add(itemabout);itemabout.addActionListener(this);}publicvoidactionPerformed(ActionEvente){//选择if(e.getSource()==itemCopy)text.copy();elseif(e.getSource()==itemCut)text.cut();elseif(e.getSource()==itemPaste)text.paste();elseif(e.getSource()==itemNew)inew();elseif(e.getSource()==itemSave)save();elseif(e.getSource()==itemOpen)open();elseif(e.getSource()==itemSaveAs)saveas();elseif(e.getSource()==itemPrint)print();elseif(e.getSource()==itemDelete)delete();elseif(e.getSource()==line1)text.setLineWrap(true);//激活自动换行功能elseif(e.getSource()==line2)text.setLineWrap(false);elseif(e.getSource()==itemFont)font();elseif(e.getSource()==itemstate){if(itemstate.getState())state();elsestatusBar.setVisible(false);//状态栏不可见}elseif(e.getSource()==itemhelp)itemhelp();elseif(e.getSource(
本文标题:java编程-记事本-全部功能都实现
链接地址:https://www.777doc.com/doc-4219140 .html