簡單記事本java源碼實(shí)例
本文實(shí)例講述了簡單記事本java實(shí)現(xiàn)代碼。分享給大家供大家參考。具體如下:
完整代碼如下:
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) { //監(jiān)聽事件
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(); //創(chuàng)建對象
frame.show(); //是對象顯示
}
}
運(yùn)行結(jié)果如下圖所示:
希望本文所述對大家的java程序設(shè)計(jì)有所幫助。
相關(guān)文章
一個(gè)JAVA小項(xiàng)目--Web應(yīng)用自動(dòng)生成Word
前段時(shí)間接到一個(gè)Web應(yīng)用自動(dòng)生成Word的需求,現(xiàn)整理了下一些關(guān)鍵步驟拿來分享一下。2014-05-05Java二維數(shù)組與稀疏數(shù)組相互轉(zhuǎn)換實(shí)現(xiàn)詳解
在某些應(yīng)用場景中需要大量的二維數(shù)組來進(jìn)行數(shù)據(jù)存儲(chǔ),但是二維數(shù)組中卻有著大量的無用的位置占據(jù)著內(nèi)存空間,稀疏數(shù)組就是為了優(yōu)化二維數(shù)組,節(jié)省內(nèi)存空間2022-09-09java向多線程中傳遞參數(shù)的三種方法詳細(xì)介紹
但在多線程的異步開發(fā)模式下,數(shù)據(jù)的傳遞和返回和同步開發(fā)模式有很大的區(qū)別。由于線程的運(yùn)行和結(jié)束是不可預(yù)料的,因此,在傳遞和返回?cái)?shù)據(jù)時(shí)就無法象函數(shù)一樣通過函數(shù)參數(shù)和return語句來返回?cái)?shù)據(jù)2012-11-11使用Feign動(dòng)態(tài)設(shè)置header和原理分析
這篇文章主要介紹了使用Feign動(dòng)態(tài)設(shè)置header和原理分析,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03java面向?qū)ο?API(接口)與集合(ArrayList)
這篇文章主要介紹了Java語言面向?qū)ο蟮腁PI與集合,還是十分不錯(cuò)的,這里給大家分享下,需要的朋友可以參考,希望能夠給你帶來幫助2021-08-08Springboot通過run啟動(dòng)web應(yīng)用的方法
這篇文章主要介紹了Springboot通過run啟動(dòng)web應(yīng)用的方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03SpringBoot 添加JSP 支持并附帶在IDEA下創(chuàng)建JSP文件【測試無誤】
這篇文章主要介紹了SpringBoot 添加JSP 支持并附帶在IDEA下創(chuàng)建JSP文件的相關(guān)知識(shí),感興趣的朋友跟隨腳本之家小編一起學(xué)習(xí)吧2018-05-05JavaWeb實(shí)現(xiàn)文件上傳下載功能實(shí)例詳解
這篇文章主要介紹了JavaWeb中的文件上傳和下載功能的實(shí)現(xiàn),在開發(fā)中,文件上傳和下載功能是非常常用的功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11