用Java语言实现记事本的简单功能

public class NoteIo12_21_0 extends JFrame implements ActionListener{
JTextArea jtextarea=null;
    JMenu jmenu1=null;
    JMenu jmenu2=null;
    JMenuBar jmenubar=null;
    JMenuItem jmenuitem1=null;
    JMenuItem jmenuitem2=null;
public static void main(String[] args) {
// TODO Auto-generated method stub
NoteIo12_21_0 note=new NoteIo12_21_0();
}
    public NoteIo12_21_0()
    {
    jtextarea=new JTextArea();
    jmenu1=new JMenu("文件");
    jmenu2=new JMenu("工具");
         jmenubar=new JMenuBar();
         jmenuitem1=new JMenuItem("打开(F)");
         jmenuitem1.setMnemonic('f');
         jmenuitem1.addActionListener(this);
         jmenuitem1.setActionCommand("open");
         jmenuitem2=new JMenuItem("保存");
         jmenuitem2.addActionListener(this);
         jmenuitem2.setActionCommand("save");
         
         
         jmenubar.add(jmenu1);
         jmenubar.add(jmenu2);
        jmenu1.add(jmenuitem1);
        jmenu1.add(jmenuitem2);
        
        this.add(jtextarea);
        this.add(jmenubar,BorderLayout.NORTH);
        //this.setJMenuBar(jmenubar);//这两种添加的方式目前我没有发现有什么不同
    this.setTitle("记事本");
    this.setSize(400,300);
    this.setLocation(400, 300);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setVisible(true);
   
    }
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("open"))
{
System.out.println("打开文件");
JFileChooser jfilechooser=new JFileChooser();
//设置标题
jfilechooser.setDialogTitle("请选择文件.....");
//默认方式
jfilechooser.showOpenDialog(null);
jfilechooser.setVisible(true);

//得到用户选择文件的全路径
String filename=jfilechooser.getSelectedFile().getAbsolutePath();


FileReader filereader=null;

BufferedReader bufferedreader=null;

try {
filereader=new FileReader(filename);
bufferedreader=new BufferedReader(filereader);
 
//按行循环读取文件
String s="";
String allCon="";
while((s=bufferedreader.readLine())!=null)
{
allCon+=s+"\r\n";

}
//放置到jtextarea中
jtextarea.setText(allCon);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}finally{
try {
filereader.close();
bufferedreader.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}else if(e.getActionCommand().equals("save")){
//System.out.println("保存文件");
JFileChooser jfilechooser=new JFileChooser();
//设置标题
jfilechooser.setDialogTitle("文件保存.....");
jfilechooser.showSaveDialog(null);
jfilechooser.setVisible(true);
//得到用户希望保存的路径
String filename=jfilechooser.getSelectedFile().getAbsolutePath();

FileWriter filewriter=null;
BufferedWriter bufferedwriter=null;

try{
filewriter=new FileWriter(filename);
bufferedwriter=new BufferedWriter(filewriter);
bufferedwriter.write(this.jtextarea.getText());
}catch(Exception e1)
{
e1.printStackTrace();
}finally{
try {
bufferedwriter.close();
filewriter.close();//这两个文件的关闭顺序会导致错误

} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

}
}

}
}

猜你喜欢

转载自blog.csdn.net/weixin_40745306/article/details/78907455
今日推荐