java记事本开发

来源:互联网 发布:java 日志收集框架 编辑:程序博客网 时间:2024/06/06 19:10
<span style="font-size:18px;">//记事本开发(界面+功能)package com.notepad;import java.io.*;import java.awt.*;import java.awt.event.*;import javax.swing.*;public class Demo extends JFrame implements ActionListener{//定义需要的组件JTextArea jta=null;//菜单条JMenuBar jmb=null;//第一JMenuJMenu jm1=null;//定义JMenuItemJMenuItem jmi1=null;JMenuItem jmi2=null;JMenuItem jmi3=null;public static void main(String[] args) {Demo demo=new Demo();}//构造函数public Demo(){//创建jtajta=new JTextArea();jmb=new JMenuBar();jm1=new JMenu("文件");//设置助记符jm1.setMnemonic('F');jmi1=new JMenuItem("打开",new ImageIcon("new.png"));//注册监听jmi1.addActionListener(this);jmi1.setActionCommand("open");jmi2=new JMenuItem("保存",new ImageIcon("save.png"));//注册监听jmi2.addActionListener(this);jmi2.setActionCommand("save");jmi3=new JMenuItem("退出",new ImageIcon("close.png"));jmi3.addActionListener(this);jmi3.setActionCommand("close");//加入this.setJMenuBar(jmb);//把菜单放到MenuBarjmb.add(jm1);//把Item放入Menujm1.add(jmi1);jm1.add(jmi2);jm1.add(jmi3);//放入到JFramethis.add(jta);this.setTitle("记事本..");this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setSize(600,500);this.setVisible(true);}@Overridepublic void actionPerformed(ActionEvent e) {//判断那个菜单被选中if(e.getActionCommand().equals("open"))//打开{System.out.println("打开");//打开文件对话框JFileChooserJFileChooser jfc1=new JFileChooser();//创建一个文件选择组件jfc1.setDialogTitle("请选择文件..");jfc1.showOpenDialog(null);//默认方式jfc1.setVisible(true);//设置显示String filename=jfc1.getSelectedFile().getAbsolutePath();//得到用户选择的全路径//System.out.println(filename);FileReader fr=null;BufferedReader br=null;try{fr=new FileReader(filename);br=new BufferedReader(fr);//从文件中读取信息并显示到JTextArea中String s="";String allcon="";while((s=br.readLine())!=null){allcon+=s+"\r\n";}//将内容放置到jtajta.setText(allcon);}catch(Exception ex){ex.printStackTrace();}finally{try {fr.close();br.close();} catch (IOException e1) {e1.printStackTrace();}}}else if(e.getActionCommand().equals("save"))//保存{System.out.println("保存");JFileChooser jfc2=new JFileChooser();jfc2.setDialogTitle("另存为..");jfc2.showSaveDialog(null);jfc2.setVisible(true);//得到选择的路径,全路径String file=jfc2.getSelectedFile().getAbsolutePath();//准备写入到指定文件FileWriter fw=null;BufferedWriter bw=null;try{fw = new FileWriter(file);bw = new BufferedWriter(fw);//将JTextArea内容写入到指定文件夹//自己优化,用字符数组bw.write(this.jta.getText());}catch(Exception ex){ex.printStackTrace();}finally{try {bw.close();fw.close();} catch (IOException e1) {e1.printStackTrace();}}}else if(e.getActionCommand().equals("close")){System.exit(0);}}}</span>

运行效果:




0 0
原创粉丝点击