Java文件(io)編程之記事本開發(fā)詳解
本文實(shí)例為大家分享了Java開發(fā)簡(jiǎn)易記事本的具體代碼,供大家參考,具體內(nèi)容如下
public class NotePad extends JFrame implements ActionListener{ //定義需要的組件 JTextArea jta=null; //多行文本框 JMenuBar jmb=null; //菜單條 JMenu jm1=null; //菜單 JMenuItem jmi1=null,jmi2=null; //菜單項(xiàng) public static void main(String[] args) { NotePad np=new NotePad(); } public NotePad(){ //構(gòu)造函數(shù) jta=new JTextArea(); //創(chuàng)建jta jmb=new JMenuBar(); jm1=new JMenu("文件"); jm1.setMnemonic('F'); //設(shè)置助記符 jmi1=new JMenuItem("打開",new ImageIcon("imag_3.jpg")); jmi1.addActionListener(this); //注冊(cè)監(jiān)聽 jmi1.setActionCommand("open"); jmi2=new JMenuItem("保存"); jmi2.addActionListener(this); jmi2.setActionCommand("save"); this.setJMenuBar(jmb); //加入 jmb.add(jm1); //把菜單放入菜單條 jm1.add(jmi1); //把item放入到Menu中 jm1.add(jmi2); this.add(jta); //放入到JFrame this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setSize(400,300); this.setTitle("記事本"); this.setIconImage((new ImageIcon("imag_2.jpg")).getImage()); this.setVisible(true); } @Override public void actionPerformed(ActionEvent arg0) { //判斷是哪個(gè)菜單被選中 if(arg0.getActionCommand().equals("open")){ //JFileChooser,創(chuàng)建一個(gè)文件選擇組件 JFileChooser jfc1=new JFileChooser(); jfc1.setDialogTitle("請(qǐng)選擇文件……"); //設(shè)置名字 jfc1.showOpenDialog(null); //默認(rèn)方式 jfc1.setVisible(true); //顯示 //得到用戶選擇的文件全路徑 String filename=jfc1.getSelectedFile().getAbsolutePath(); FileReader fr=null; BufferedReader br=null; try { fr=new FileReader(filename); br=new BufferedReader(fr); //從文件中讀取信息并顯示到j(luò)ta String s=""; String allCon=""; while((s=br.readLine())!=null){ //循環(huán)讀取文件,s不為空即還未讀完畢 allCon+=s+"\r\n"; } jta.setText(allCon); //放置到j(luò)ta } catch (Exception e) { e.printStackTrace(); }finally{ try { fr.close(); br.close(); } catch (Exception e) { e.printStackTrace(); } } }else if(arg0.getActionCommand().equals("save")){ //出現(xiàn)保存對(duì)話框 JFileChooser jfc2=new JFileChooser(); jfc2.setDialogTitle("另存為……"); jfc2.showSaveDialog(null); //按默認(rèn)的方式顯示 jfc2.setVisible(true); //得到用戶希望把文件保存到何處,文件全路徑 String filename2=jfc2.getSelectedFile().getAbsolutePath(); //準(zhǔn)備寫入到指定文件 FileWriter fw=null; BufferedWriter bw=null; try { fw=new FileWriter(filename2); bw=new BufferedWriter(fw); bw.write(this.jta.getText()); } catch (Exception e) { e.printStackTrace(); }finally{ try { bw.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
運(yùn)行效果如下
點(diǎn)擊文件按鈕,點(diǎn)擊打開菜單項(xiàng),選擇一個(gè)文本文件,效果如下:
打開后,內(nèi)容顯示如下:
對(duì)內(nèi)容稍作修改,另存為名為sss的文件,效果如下:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
java中CompletableFuture異步執(zhí)行方法
本文主要介紹了java中CompletableFuture異步執(zhí)行方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06基于Spring上下文工具類?ApplicationContextUtil
這篇文章主要介紹了基于Spring上下文工具類?ApplicationContextUtil,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11關(guān)于Mybatis的mapper接口函數(shù)重載問(wèn)題
這篇文章主要介紹了關(guān)于Mybatis的mapper接口函數(shù)重載問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02Java中高效的判斷數(shù)組中某個(gè)元素是否存在詳解
相信大家在操作Java的時(shí)候,經(jīng)常會(huì)要檢查一個(gè)數(shù)組(無(wú)序)是否包含一個(gè)特定的值?這是一個(gè)在Java中經(jīng)常用到的并且非常有用的操作。同時(shí),這個(gè)問(wèn)題在Stack Overflow中也是一個(gè)非常熱門的問(wèn)題。本文將分析幾種常見用法及其時(shí)間成本,有需要的朋友們可以參考借鑒。2016-11-11Java 8實(shí)現(xiàn)任意參數(shù)的單鏈表
這篇文章主要為大家詳細(xì)介紹了Java 8實(shí)現(xiàn)任意參數(shù)的單鏈表,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-10-10SpringBoot中mapper.xml文件存放的兩種實(shí)現(xiàn)位置
這篇文章主要介紹了SpringBoot中mapper.xml文件存放的兩種實(shí)現(xiàn)位置,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01SpringBoot 集成 Nebula的操作過(guò)程
這篇文章主要介紹了SpringBoot 集成 Nebula的操作過(guò)程,通過(guò)示例代碼介紹了java 環(huán)境下如何對(duì) Nebula Graph 進(jìn)行操作,感興趣的朋友跟隨小編一起看看吧2024-05-05淺談springboot @Repository與@Mapper的區(qū)別
本文主要介紹了淺談springboot @Repository與@Mapper的區(qū)別,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03