zoukankan      html  css  js  c++  java
  • 记事本程序

    /**
     * 记事本程序
     */
    package com.test7;
    import java.awt.*;
    import java.io.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class NotePad extends JFrame implements ActionListener{

    //定义所需要的组件
    JTextArea jta=null;
    //定义菜单条
    JMenuBar jmb=null;
    //第一个JMenu
    JMenu jml=null;
    //定义JMenuIteam
    JMenuItem jmi1=null;
    JMenuItem jmi2=null;
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    NotePad np=new NotePad();
    }
    //构造函数
    public NotePad()
    {
    //创建jta文本域
    jta=new JTextArea();
    jmb=new JMenuBar();
    jml=new JMenu("文件(0)");
    //设置助记符
    jml.setMnemonic('F');
    jmi1=new JMenuItem("打开");
    jmi2=new JMenuItem("保存");
    //对保存按钮做监听
    jmi2.addActionListener(this);
    jmi2.setActionCommand("save");
    //对打开按钮注册监听
    jmi1.addActionListener(this);
    jmi1.setActionCommand("open");
    //加入
    this.setJMenuBar(jmb);
    //把jml放入到jmb
    jmb.add(jml);
    //把item放入到Menu
    jml.add(jmi1);
    jml.add(jmi2);
    //放入到JFreme
    this.add(jta);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setSize(400, 300);
    this.setVisible(true);
    }
    //
    public void actionPerformed(ActionEvent arg0) {
    if(arg0.getActionCommand().equals("save"))
    {
    //出现保存对话框
    JFileChooser jfc=new JFileChooser();
    jfc.setDialogTitle("另存为....");
    //按照默认的方式显示
    jfc.showSaveDialog(null);
    jfc.setVisible(true);
    //用户希望把文件保存到何处,文件的全路径
    String file=jfc.getSelectedFile().getAbsolutePath();
    //准备写入到指定文件
    FileWriter fw=null;
    BufferedWriter bw=null;
    try {
    fw=new FileWriter(file);
    bw=new BufferedWriter(fw);
    bw.write(this.jta.getText());
    } catch (Exception e) {
    // TODO: handle exception
    }
    finally
    {
    try {
    bw.close();
    fw.close();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }
    // TODO Auto-generated method stub
     
    else if (arg0.getActionCommand().equals("open"));
    {
    //System.out.println("选中的是打开");
    //推荐JFileChooser
    //文件选择组
    JFileChooser jfc1=new JFileChooser();
    //默认的选中框组件
    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);
    //从文件中读取信息并显示jta
    String s="";
    String allCon="";
    while((s=br.readLine())!=null)
    {
    allCon+=s+" ";
    }
    jta.setText(allCon);
    //放置到jta即可
    jta.setText(allCon);
    } catch (Exception e) {
    // TODO: handle exception
    }finally{
    try {
    br.close();
    fr.close();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }
    }
    }


  • 相关阅读:
    Redis 思维导图 (解析版)
    一张图片了解redis
    Redis 思维导图
    计算机网络协议
    IT笔面试题
    Hadoop集群搭建
    天涯论坛只看楼主
    齐秦&r大约在冬季现场版
    郁可唯茶汤现场版
    MTK平台电路设计01
  • 原文地址:https://www.cnblogs.com/toge/p/6114730.html
Copyright © 2011-2022 走看看