




版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、import java.awt.*;import java.awt.event.*;import java.io.*;import javax.swing.*;public class MyNotebook public static void main(String args) new Notebook();/Notebook()主類: class Notebook extends JFrame Container container;JFileChooser fc=new JFileChooser(); File file=null; JMenuBar mb; JMenu fileMenu
2、,editMenu,helpMenu; JMenuItem newItem,openItem,saveItem,saveasItem,exitItem,cutItem,copyItem,pasteItem,sallItem,aboutItem; JTextArea jta; JPopupMenu popUpMenu; public Notebook() /記事本主界面設(shè)計(jì)程序/setTitle(記事本);container=this.getContentPane();container.setLayout(new BorderLayout();jta=new JTextArea();conta
3、iner.add(jta);setVisible(true);setSize(650, 450); mb=new JMenuBar(); setJMenuBar(mb); /文件菜單及其菜單選項(xiàng)的程序設(shè)計(jì)/fileMenu=new JMenu(文件);newItem=new JMenuItem(新建);/設(shè)置快捷鍵/newItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N ,ActionEvent.CTRL_MASK); /設(shè)置“新建”事件監(jiān)聽(tīng)/newItem.addActionListener(new NewActionListe
4、ner();fileMenu.add(newItem);fileMenu.addSeparator();openItem=new JMenuItem(打開(kāi));openItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O ,ActionEvent.CTRL_MASK);openItem.addActionListener(new OpenActionListener();fileMenu.add(openItem);fileMenu.addSeparator(); saveItem=new JMenuItem(保存);saveItem.
5、setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S ,ActionEvent.CTRL_MASK);saveItem.addActionListener(new SaveActionListener();fileMenu.add(saveItem);fileMenu.addSeparator();saveasItem=new JMenuItem(另存為);saveasItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A ,ActionEvent.CTRL_MASK);saveasIt
6、em.addActionListener(new SaveActionListener();fileMenu.add(saveasItem);fileMenu.addSeparator();exitItem=new JMenuItem(退出);exitItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E ,ActionEvent.CTRL_MASK);exitItem.addActionListener(new ExitActionListener();fileMenu.add(exitItem);mb.add(fileMenu);/
7、編輯菜單欄及其菜單選項(xiàng)程序設(shè)計(jì)/editMenu=new JMenu(編輯);cutItem=new JMenuItem(剪切);cutItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, ActionEvent.CTRL_MASK);cutItem.addActionListener(new CutActionListener();editMenu.add(cutItem);editMenu.addSeparator();copyItem=new JMenuItem(復(fù)制);copyItem.setAccelerator(KeyS
8、troke.getKeyStroke(KeyEvent.VK_C, ActionEvent.CTRL_MASK);copyItem.addActionListener(new CopyActionListener();editMenu.add(copyItem);editMenu.addSeparator(); pasteItem=new JMenuItem(粘貼);pasteItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, ActionEvent.CTRL_MASK);pasteItem.addActionListener(n
9、ew PasteActionListener();editMenu.add(pasteItem);editMenu.addSeparator();sallItem=new JMenuItem(全選);sallItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_M, ActionEvent.CTRL_MASK);sallItem.addActionListener(new SallActionListener();editMenu.add(sallItem);mb.add(editMenu);/幫助菜單欄及其菜單選項(xiàng)程序設(shè)計(jì)/helpMe
10、nu=new JMenu(幫助);aboutItem=new JMenuItem(關(guān)于記事本);aboutItem.addActionListener(new AbouthActionListener();helpMenu.add(aboutItem);mb.add(helpMenu);/編輯菜單的鼠標(biāo)監(jiān)聽(tīng)及其彈出菜單的功能設(shè)計(jì)/popUpMenu = editMenu.getPopupMenu();jta.addMouseListener( new MouseAdapter() public void mouseReleased(MouseEvent e) if(e.getButton()
11、= MouseEvent.BUTTON3) popUpMenu.show(editMenu, e.getX(), e.getY(); public void mouseClicked(MouseEvent e) if(e.getButton() = MouseEvent.BUTTON1) popUpMenu.setVisible(false); ); /“新建”功能的實(shí)現(xiàn)/class NewActionListener implements ActionListener public void actionPerformed(ActionEvent e) jta.setText(); file
12、=null; /“保存功能的實(shí)現(xiàn)”/class SaveActionListener implements ActionListenerpublic void actionPerformed(ActionEvent e) if(file!=null) fc.setSelectedFile(file); int returnVal=fc.showSaveDialog(Notebook.this); if(returnVal=JFileChooser.APPROVE_OPTION) file=fc.getSelectedFile(); saving(); void saving() try Fil
13、eWriter Writef=new FileWriter(file); Writef.write(jta.getText(); Writef.close(); catch(Exception e)e.printStackTrace(); /“打開(kāi)”功能的實(shí)現(xiàn)/class OpenActionListener implements ActionListenerpublic void actionPerformed(ActionEvent e) if(file !=null)fc.setSelectedFile(file); int returnVal=fc.showOpenDialog(Not
14、ebook.this); if(returnVal=JFileChooser.APPROVE_OPTION) file=fc.getSelectedFile(); unfold(); void unfold() try FileReader Readf=new FileReader(file); int len=(int)file.length(); char buffer=new charlen; Readf.read(buffer,0,len); Readf.close(); jta.setText(new String(buffer); catch(Exception e)e.print
15、StackTrace(); /“退出”功能的實(shí)現(xiàn)/class ExitActionListener implements ActionListenerpublic void actionPerformed(ActionEvent e) setVisible(false); dispose(); System.exit(0);/“剪切”功能的 實(shí)現(xiàn)/class CutActionListener implements ActionListenerpublic void actionPerformed(ActionEvent e) jta.cut();/“復(fù)制”功能的實(shí)現(xiàn)/class CopyAc
16、tionListener implements ActionListenerpublic void actionPerformed(ActionEvent e) jta.copy();/“粘貼”功能的實(shí)現(xiàn)/class PasteActionListener implements ActionListenerpublic void actionPerformed(ActionEvent e) jta.paste();/“全選”功能的實(shí)現(xiàn)/class SallActionListener implements ActionListenerpublic void actionPerformed(ActionEvent e) jta.setSelectionStart(0);jta.setSelectionEnd(jt
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 機(jī)器人情感識(shí)別與表達(dá)考核試卷
- 高級(jí)審計(jì)培訓(xùn)課件
- 批發(fā)市場(chǎng)魚(yú)品安全監(jiān)管考核試卷
- 批發(fā)市場(chǎng)的小批量訂單處理考核試卷
- 飼料店轉(zhuǎn)讓合同范本
- 教學(xué)加盟合同范本
- 材料合同范本簡(jiǎn)易圖表
- 食品運(yùn)輸儲(chǔ)藏合同范本
- 家具導(dǎo)購(gòu)合同范本
- 企業(yè)級(jí)網(wǎng)絡(luò)攻擊事件預(yù)案制定協(xié)議
- 2024年全國(guó)鄉(xiāng)村醫(yī)生考試復(fù)習(xí)題庫(kù)及答案(共360題)
- 漢語(yǔ)言文學(xué)論文8000字范文
- 房產(chǎn)中介獨(dú)家委托協(xié)議
- 乒乓球課程教學(xué)市公開(kāi)課金獎(jiǎng)市賽課一等獎(jiǎng)?wù)n件
- 五年級(jí)下冊(cè)《體育與健康》全冊(cè)教案
- 建設(shè)項(xiàng)目使用草原可行性報(bào)告編寫(xiě)規(guī)范
- 物業(yè)保安接管入場(chǎng)方案
- 《職業(yè)道德與法治》開(kāi)學(xué)第一課(導(dǎo)言)(教案)-【中職專用】中職思想政治《職業(yè)道德與法治》教案(高教版2023·基礎(chǔ)模塊)
- Q-GDW1799.2-2013-電力安全工作規(guī)程-線路部分
- (2024版)肉、禽、蛋、奶及水產(chǎn)品零售行業(yè)綜合知識(shí)
- IBM咨詢-中糧生化ERP項(xiàng)目業(yè)務(wù)藍(lán)圖設(shè)計(jì)報(bào)告
評(píng)論
0/150
提交評(píng)論