代码如下

import java.awt.*;
import java.io.*;
import java.awt.datatransfer.*;
import java.awt.event.*; public class Main extends Frame implements ActionListener {
private static final long serialVersionUID = 1L;
TextArea textArea = new TextArea();
MenuBar menuBar = new MenuBar();
Menu fileMenu = new Menu("File");
MenuItem newItem = new MenuItem("New");
MenuItem openItem = new MenuItem("Open");
MenuItem saveItem = new MenuItem("Save");
MenuItem saveAsItem = new MenuItem("Save As");
MenuItem exitItem = new MenuItem("Exit");
Menu editMenu = new Menu("Edit");
MenuItem selectItem = new MenuItem("Select All");
MenuItem copyItem = new MenuItem("Copy");
MenuItem cutItem = new MenuItem("Cut");
MenuItem pasteItem = new MenuItem("Paste");
String fileName = null;
Toolkit toolKit=Toolkit.getDefaultToolkit();
Clipboard clipBoard=toolKit.getSystemClipboard(); private FileDialog openFileDialog = new FileDialog(this,"Open File",FileDialog.LOAD);
private FileDialog saveAsFileDialog = new FileDialog(this,"Save File As",FileDialog.SAVE); public Main(){
setTitle("记事本程序-by Jackbase");
setFont(new Font("Times New Roman",Font.PLAIN,12));
setBackground(Color.white);
setSize(400,300);
fileMenu.add(newItem);
fileMenu.add(openItem);
fileMenu.addSeparator();
fileMenu.add(saveItem);
fileMenu.add(saveAsItem);
fileMenu.addSeparator();
fileMenu.add(exitItem);
editMenu.add(selectItem);
editMenu.addSeparator();
editMenu.add(copyItem);
editMenu.add(cutItem);
editMenu.add(pasteItem);
menuBar.add(fileMenu);
menuBar.add(editMenu);
setMenuBar(menuBar);
add(textArea);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
newItem.addActionListener(this);
openItem.addActionListener(this);
saveItem.addActionListener(this);
saveAsItem.addActionListener(this);
exitItem.addActionListener(this);
selectItem.addActionListener(this);
copyItem.addActionListener(this);
cutItem.addActionListener(this);
pasteItem.addActionListener(this);
} public void actionPerformed(ActionEvent e) { //监听事件
Object eventSource = e.getSource();
if(eventSource == newItem){
textArea.setText("");
}else if(eventSource == openItem){
openFileDialog.show();
fileName = openFileDialog.getDirectory()+openFileDialog.getFile();
if(fileName != null)
readFile(fileName);
}else if (eventSource == saveItem){
if(fileName != null)
writeFile(fileName);
}else if(eventSource == saveAsItem){
saveAsFileDialog.show();
fileName = saveAsFileDialog.getDirectory()+saveAsFileDialog.getFile();
if (fileName!= null)
writeFile(fileName);
}else if(eventSource == selectItem){
textArea.selectAll();
}else if(eventSource == copyItem){
String text=textArea.getSelectedText();
StringSelection selection=new StringSelection(text);
clipBoard.setContents(selection,null);
}else if(eventSource == cutItem){
String text=textArea.getSelectedText();
StringSelection selection=new StringSelection(text);
clipBoard.setContents(selection,null);
textArea.replaceRange("",textArea.getSelectionStart(),textArea.getSelectionEnd());
}else if(eventSource == pasteItem){
Transferable contents=clipBoard.getContents(this);
if(contents==null) return;
String text;
text="";
try{
text=(String)contents.getTransferData(DataFlavor.stringFlavor);
}catch(Exception exception){
}
textArea.replaceRange(text,textArea.getSelectionStart(),textArea.getSelectionEnd());
}else if(eventSource == exitItem){
System.exit(0);
}
} public void readFile(String fileName){ //读取文件处理
try{
File file = new File(fileName);
FileReader readIn = new FileReader(file);
int size = (int)file.length();
int charsRead = 0;
char[] content = new char[size];
while(readIn.ready())
charsRead += readIn.read(content, charsRead, size - charsRead);
readIn.close();
textArea.setText(new String(content, 0, charsRead));
}
catch(IOException e){
System.out.println("Error opening file");
}
} public void writeFile(String fileName){ //写入文件处理
try{
File file = new File (fileName);
FileWriter writeOut = new FileWriter(file);
writeOut.write(textArea.getText());
writeOut.close();
}
catch(IOException e){
System.out.println("Error writing file");
}
} @SuppressWarnings("deprecation")
public static void main(String[] args){
Frame frame = new Main(); //创建对象
frame.show(); //是对象显示
}
}

  运行结果
<ignore_js_op>

详细说明:http://java.662p.com/thread-2217-1-2.html

简单记事本程序java源码项目的更多相关文章

  1. Eclipse导入Hadoop源码项目及编写Hadoop程序

    一 Eclipse导入Hadoop源码项目 基本步骤: 1)在Eclipse新建一个java项目[hadoop-1.2.1] 2)将Hadoop压缩包解压目录src下的core,hdfs,mapred ...

  2. 如何阅读Java源码 阅读java的真实体会

    刚才在论坛不经意间,看到有关源码阅读的帖子.回想自己前几年,阅读源码那种兴奋和成就感(1),不禁又有一种激动. 源码阅读,我觉得最核心有三点:技术基础+强烈的求知欲+耐心.   说到技术基础,我打个比 ...

  3. 如何阅读Java源码

    刚才在论坛不经意间,看到有关源码阅读的帖子.回想自己前几年,阅读源码那种兴奋和成就感(1),不禁又有一种激动.源码阅读,我觉得最核心有三点:技术基础+强烈的求知欲+耐心. 说到技术基础,我打个比方吧, ...

  4. [收藏] Java源码阅读的真实体会

    收藏自http://www.iteye.com/topic/1113732 刚才在论坛不经意间,看到有关源码阅读的帖子.回想自己前几年,阅读源码那种兴奋和成就感(1),不禁又有一种激动. 源码阅读,我 ...

  5. Java源码阅读的真实体会(一种学习思路)

    Java源码阅读的真实体会(一种学习思路) 刚才在论坛不经意间,看到有关源码阅读的帖子.回想自己前几年,阅读源码那种兴奋和成就感(1),不禁又有一种激动. 源码阅读,我觉得最核心有三点:技术基础+强烈 ...

  6. Java源码阅读的真实体会(一种学习思路)【转】

    Java源码阅读的真实体会(一种学习思路)   刚才在论坛不经意间,看到有关源码阅读的帖子.回想自己前几年,阅读源码那种兴奋和成就感(1),不禁又有一种激动. 源码阅读,我觉得最核心有三点:技术基础+ ...

  7. Java 源码学习线路————_先JDK工具包集合_再core包,也就是String、StringBuffer等_Java IO类库

    http://www.iteye.com/topic/1113732 原则网址 Java源码初接触 如果你进行过一年左右的开发,喜欢用eclipse的debug功能.好了,你现在就有阅读源码的技术基础 ...

  8. 【java集合框架源码剖析系列】java源码剖析之java集合中的折半插入排序算法

    注:关于排序算法,博主写过[数据结构排序算法系列]数据结构八大排序算法,基本上把所有的排序算法都详细的讲解过,而之所以单独将java集合中的排序算法拿出来讲解,是因为在阿里巴巴内推面试的时候面试官问过 ...

  9. 2018-09-15 Java源码英翻中库以及服务原型

    服务很简单, 只为演示这个库, 源码在: program-in-chinese/code_translator_service. 在Postman测试效果: 演示服务地址: 74.91.17.250: ...

随机推荐

  1. JAVA元运算符,一元运算符,二元运算符,三元运算符

    一元运算符: 序号 一元运算符 说明 1 i++ 给i加1 2 i-- 给i减1 3 ++i 给i加1 4 --i 给i减1 i++;/*例:int i=1;i++;//先将i的值1赋值给i,然后i再 ...

  2. jQuery如何动态添加具有删除按钮的行

    代码实例如下: <!DOCTYPE html><html><head><meta charset=" utf-8"><meta ...

  3. 使用python + tornado 做项目demo演示模板

    很简单,可是却也折腾了不是时间,走了不少弯路.在此备注记录一下,以供后需. # web_server.py #!/usr/bin/env python # coding=utf-8 import os ...

  4. Java中的匿名类

    我们知道接口一般用于定义一种规范或操作协议,然后子类实现接口的所有功能.如下面的简单代码: 定义IMessage接口 package org.lyk.entities; public interfac ...

  5. Oracle Grid Infrastructure: Understanding Split-Brain Node Eviction (文档 ID 1546004.1)

    In this Document   Purpose   Scope   Details   What does "split brain" mean?   Why is this ...

  6. 笔记本win7制作wifi

    笔记本win7系统, 要打开笔记本无线网卡 1. 运行 netsh wlan set hostednetwork mode=allow ssid=testwifi key=testpass model ...

  7. 直接使用editbox.clear()清空时,有时会无法清除完全,此时有清空文本框的另一种方法

    editbox = driver.find_element_by_id("id") editbox.click() content = editbox.get_attribute( ...

  8. IIS装好了无法访问localhost

    解决办法:                 [1]:                 检查你的DTC服务(全名:Distributed Transaction Coordinator)是否可以正常启动 ...

  9. hadoop的live node为0

    1.重新格式化namenode cd ~ rm -rf name mkdir name rm -rf hadoop-2.7.2/logs/ mkdir hadoop-2.7.2/logs/ hadoo ...

  10. Android基础总结(5)——数据存储,持久化技术

    瞬时数据:指那些存储在内存当中,有可能会因为程序广播或其他原因导致内存被回收而丢失的数据. 数据持久化:指将那些内存中的瞬时数据保存到存储设备中,保证即使在手机或电脑关机的情况下,这些数据仍然不丢失. ...