用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - java代码库

我的记事本(简单功能实现)

2013-04-07 作者: 汉代风云举报

[java]代码库

package NoteBook;
 
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import java.text.SimpleDateFormat;
import javax.swing.*;
 
public class SampleNoteBook extends JFrame implements ActionListener, Runnable {
    private JMenuBar mb;
    private JMenu file, edit, form, help, timeShow, size, color;
    private JMenuItem newFile, open, save, save1, exit;
    private JMenuItem copy, paste, cut, time;
    private JMenuItem about;
    private JMenuItem size1, size2, size3, size4;
    private JMenuItem color1, color2, color3;
    private FileDialog openDialog, saveDialog, save1Dialog;
    private TextArea ta;
    private File file1;
 
    public void setupTime() {
 
        Thread thread1 = new Thread(this);
 
        thread1.start();
    }
 
    public void run() {
        while (true) {
            SimpleDateFormat sdf = new SimpleDateFormat("HH : mm : ss");
            timeShow.setText("当前时间:  " + sdf.format(new Date()));
            timeShow.setForeground(Color.RED);
        }
    }
 
    public SampleNoteBook() {
        Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
        int width = d.width;
        int height = d.height;
        this.setLocation((width - 700) / 2, (height - 500) / 2);
        this.setSize(700, 500);
        this.setTitle("简易记事本");
        this.setResizable(false);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
 
        // 将Frame标题栏的图标修改成你自己的图标了
        Toolkit tk = Toolkit.getDefaultToolkit();
        Image image = tk
                .createImage("D:\\Eclipse\\workspace\\NoteBook\\images\\36.gif"); /*
                                                                                 * image
                                                                                 * .
                                                                                 * gif是你的图标
                                                                                 */
        this.setIconImage(image);
 
        mb = new JMenuBar();
        file = new JMenu("文件");
        edit = new JMenu("编辑");
        form = new JMenu("格式");
        help = new JMenu("帮助");
        timeShow = new JMenu();
        size = new JMenu("字体大小");
        color = new JMenu("字体颜色");
        newFile = new JMenuItem("新建");
        open = new JMenuItem("打开");
        save = new JMenuItem("保存");
        save1 = new JMenuItem("另存为");
        exit = new JMenuItem("退出");
        copy = new JMenuItem("复制");
        paste = new JMenuItem("粘贴");
        cut = new JMenuItem("剪切");
        time = new JMenuItem("当前时间");
        ta = new TextArea();
        ta.setEditable(false);
 
        about = new JMenuItem("关于");
 
        size1 = new JMenuItem("10");
        size2 = new JMenuItem("15");
        size3 = new JMenuItem("20");
        size4 = new JMenuItem("25");
        color1 = new JMenuItem("红色");
        color2 = new JMenuItem("绿色");
        color3 = new JMenuItem("蓝色");
        color1.setForeground(Color.RED);
        color2.setForeground(Color.GREEN);
        color3.setForeground(Color.BLUE);
        /*
         * size1.setFont(new Font("宋体",Font.BOLD,10)); size2.setFont(new
         * Font("宋体",Font.BOLD,15)); size3.setFont(new Font("宋体",Font.BOLD,20));
         * size4.setFont(new Font("宋体",Font.BOLD,25));
         */
 
        openDialog = new FileDialog(this, "打开", FileDialog.LOAD);
        saveDialog = new FileDialog(this, "保存", FileDialog.SAVE);
        save1Dialog = new FileDialog(this, "另存为", FileDialog.SAVE);
 
        mb.add(file);
        file.add(newFile);
        file.add(open);
        file.addSeparator();
        file.add(save);
        file.add(save1);
        file.addSeparator();
        file.add(exit);
 
        mb.add(edit);
        edit.add(copy);
        edit.add(paste);
        edit.add(cut);
        edit.addSeparator();
        edit.add(time);
 
        mb.add(form);
        form.add(size);
        form.add(color);
 
        size.add(size1);
        size.add(size2);
        size.add(size3);
        size.add(size4);
        color.add(color1);
        color.add(color2);
        color.add(color3);
 
        mb.add(help);
        help.add(about);
 
        mb.add(timeShow);
 
        this.add(ta);
        this.setJMenuBar(mb);
 
        this.setVisible(true);
        setupTime(); // 开启时间多线程
 
        exit.addActionListener(this);
        about.addActionListener(this);
        open.addActionListener(this);
        save.addActionListener(this);
        save1.addActionListener(this);
        color1.addActionListener(this);
        color2.addActionListener(this);
        color3.addActionListener(this);
 
        // size1.addActionListener(this);
        // size2.addActionListener(this);
        // size3.addActionListener(this);
        // size4.addActionListener(this);
        newFile.addActionListener(this);
    }
 
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == exit) {
 
            int num = JOptionPane.showConfirmDialog(null, "       你确定要退出?",
                    "退出", JOptionPane.YES_NO_CANCEL_OPTION);
            if (num == JOptionPane.YES_OPTION)
                System.exit(0);
        }
 
        else if (e.getSource() == newFile) {
            ta.setEditable(true);
        }
 
        else if (e.getSource() == size1)
            ta.setFont(new Font("宋体", Font.BOLD, 10));
 
        else if (e.getSource() == size2)
            ta.setFont(new Font("宋体", Font.BOLD, 15));
 
        else if (e.getSource() == size3)
            ta.setFont(new Font("宋体", Font.BOLD, 20));
 
        else if (e.getSource() == size4)
            ta.setFont(new Font("宋体", Font.BOLD, 25));
 
        else if (e.getSource() == color1)
            ta.setForeground(Color.RED);
 
        else if (e.getSource() == color2)
            ta.setForeground(Color.GREEN);
 
        else if (e.getSource() == color3)
            ta.setForeground(Color.BLUE);
 
        else if (e.getSource() == about)
            JOptionPane.showMessageDialog(null, "     --汉代风云-作品\n  版权所有,侵权必究",
                    "版权信息", JOptionPane.INFORMATION_MESSAGE);
 
        else if (e.getSource() == open) {
            openDialog.setVisible(true);
            String dirPath = openDialog.getDirectory();
            String fileName = openDialog.getFile();
            if (dirPath == null || fileName == null)
                return;
            ta.setText("");
            file1 = new File(dirPath, fileName);
 
            try {
                BufferedReader bufr = new BufferedReader(new FileReader(file1));
                String line = null;
                while ((line = bufr.readLine()) != null) {
                    ta.append(line + "\r\n");
                }
                bufr.close();
            } catch (FileNotFoundException e1) {
 
                e1.printStackTrace();
            } catch (IOException e1) {
 
                e1.printStackTrace();
            }
 
        }
 
        else if (e.getSource() == save1) {
 
            save1Dialog.setVisible(true);
 
            String dirPath = save1Dialog.getDirectory();
            String fileName = save1Dialog.getFile();
            if (dirPath == null || fileName == null)
                return;
            file1 = new File(dirPath, fileName);
 
            try {
                BufferedWriter bufw = new BufferedWriter(new FileWriter(file1));
                String text = ta.getText();
                bufw.write(text);
                bufw.close();
 
            } catch (IOException e1) {
 
                e1.printStackTrace();
            }
 
        }
 
        else if (e.getSource() == save) {
 
            if (file1 == null) {
                saveDialog.setVisible(true);
 
                String dirPath = saveDialog.getDirectory();
                String fileName = saveDialog.getFile();
                if (dirPath == null || fileName == null)
                    return;
                file1 = new File(dirPath, fileName);
            }
 
            try {
                BufferedWriter bufw = new BufferedWriter(new FileWriter(file1));
                String text = ta.getText();
                bufw.write(text);
                bufw.close();
 
            } catch (IOException e1) {
 
                e1.printStackTrace();
            }
 
        }
    }
 
    public static void main(String[] args) {
        new SampleNoteBook();
    }
 
}

[代码运行效果截图]


我的记事本(简单功能实现)

[源代码打包下载]




网友评论    (发表评论)

共2 条评论 1/1页

发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...