简单记事本java源码实例
更新时间:2014年11月25日 11:35:58 投稿:shichen2014
这篇文章主要介绍了简单记事本java源码,以一个完整的实例形式分析了记事本的Java实现方法,对于Java应用程序的开发有一定的参考借鉴价值,需要的朋友可以参考下
本文实例讲述了简单记事本java实现代码。分享给大家供大家参考。具体如下:
完整代码如下:
复制代码 代码如下:
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(); //是对象显示
}
}
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(); //是对象显示
}
}
运行结果如下图所示:
希望本文所述对大家的java程序设计有所帮助。
相关文章
用Rational Rose逆向工程(java)生成类图(教程和错误解决)
Rational Rose有个很方便的功能,将项目中的JAVA代码自动转换成UML类图2013-02-02Mybatis collection查询集合属性报错的解决方案
这篇文章主要介绍了Mybatis collection查询集合属性报错的解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2021-09-09SharedWorkerGlobalScope属性数据共享示例解析
这篇文章主要为大家介绍了SharedWorkerGlobalScope属性数据共享示例解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪2022-12-12Java日期格式化的实现(@JsonFormat和@JSONField)
本文主要介绍了Java日期格式化的实现,主要介绍了@JsonFormat和@JSONField两种方式,具有一定的参考价值,感兴趣的可以了解一下2024-05-05eclipse/intellij idea 远程调试hadoop 2.6.0
这篇文章主要介绍了eclipse/intellij idea 远程调试hadoop 2.6.0的相关资料,需要的朋友可以参考下2016-07-07
最新评论