当前位置:百问九>百科问答>java记事本代码

java记事本代码

2024-09-12 20:34:33 编辑:zane 浏览量:513

问题补充说明:(1) 要求如何实现记事本的打开与保存操作? (2) 如何实现记事本的复制、剪切、粘帖操作

java记事本代码

importjava.awt.event.ActionListener;

im编采奏影冷上容顶土解portjava.util.EventListener;

importjava.awt.event.*;

importjava.awt.*;

import***.io.*;

importjava.lang.*;

impor布州氧放航古以持tjava.awt.datatransfer.*;

importjavax.swing.*;

publicclassMiniEditextendsJFrameimplementsActionListener{

/**

*Methodmain

*

*

*@paramargs

*

*/

MenuBarmenuBar=newMenuBar();

Menufile=newMenu("File"),

edit=newMenu("Edit"),

help=newMenu("Help")劳存应征绿死展激哥范干;

MenuItem[]menuItem={

newMenuItem("New"),

newMenuItem("Open"),

n缩现ewMenuItem("Save"),

newMenuItem("Exit"),

newMenuItem("SelectAll"),

newMenuItem("Copy"),

newMenuItem("Cut"),

newM苗又敌教提把过enuItem("Pa伤燃议好钱ste"),

newMenuItem("Help")

};

TextAreatextArea=n缺析话套菜又须ewTextAre两进考何亮序万效a();

StringfileName="NoName";

ToolkittoolKit=Toolkit.getDefaultToolkit();

Clipboardclipboard=toolKit.getSystemClip独收排试board();

//opneandclosemessagedialogs

privateFileDialogopenF底述间需降鱼触操想左单ileDialog=

newFileDialog(this,"OpenFile",FileDialog.LOAD);

privateFileDialogsav他古钱满备门周皇下棉eFileDialog=

newFile欢可龙八Dialog(this,"SaveFile",***.SAVE);

publicstaticvoidmain(String[]args){

//TODO:Addyourcodehere

MiniEditMyEdit=newMiniEdit()副精发味负交型天记甚造;

***.show由称如原许台剂器练毛算();

}

/**

*MethodMiniEdit

*

*

*/

publicMiniEdit(){

//TODO:Addyourcodehere

setTitle("MiniEdit");

盐setFont(newFont("Times督格束情火NewRoman",Font.PLAIN,15));

setBackground(Color.white);

setSize(50号行0,500);

setMenuBar(menuBar);

menuBar.add(file);

menuBar.add(edit);

序menuBar.add(help);

for(inti=0;i<4;i++)

{

file.add(menuItem[i]);

edit.add(menuItem[i+4]);

}

help.add(menuItem[8]);

add(textArea);

addWindowListener(newWindowAdapter(){

publicvoidwindowClosing(WindowEvente){

e.getWindow().dispose();

System.exit(0);

}

});

//addactionListener

for(inti=0;i<menuItem.length;i++)

{

menuItem[i].addActionListener(this);

}

}

/**

*MethodactionPerformed

*

*

*@parame

*

*/

publicvoidactionPerformed(ActionEvente){

//TODO:Addyourcodehere

ObjecteventSource=e.getSource();

if(eventSource==menuItem[0])//newItem

{

textArea.setText("");

}

elseif(eventSource==menuItem[1])//OpenItem

{

***.show();

fileName=openFileDialog.getDirectory()+openFileDialog.getFile();

if(fileName!=null)

{

openFile(fileName);

}

}

elseif(eventSource==menuItem[2])//SaveItem

{

***.show();

fileName=saveFileDialog.getDirectory()+saveFileDialog.getFile();

if(fileName!=null)

{

writeFile(fileName);

}

}

elseif(eventSource==menuItem[3])//exitItem

{

System.exit(0);

}

elseif(eventSource==menuItem[4])//SelectAll

{

textArea.selectAll();

}

elseif(eventSource==menuItem[5])//copy

{

Stringtext=textArea.getSelectedText();

StringSelectionselection=newStringSelection(text);

clipboard.setContents(selection,null);

}

elseif(eventSource==menuItem[6])//cut

{

Stringtext=textArea.getSelectedText();

StringSelectionselection=newStringSelection(text);

clipboard.setContents(selection,null);

textArea.replaceText("",textArea.getSelectionStart(),

textArea.getSelectionEnd());

}

elseif(eventSource==menuItem[7])//Paste

{

Transferablecontents=clipboard.getContents(this);

if(contents==null)

return;

Stringtext;

text="";

try{

text=(String)contents.getTransferData(DataFlavor.stringFlavor);

}catch(Exceptionex){}

textArea.replaceText(text,

textArea.getSelectionStart(),textArea.getSelectionEnd());

}

elseif(eventSource==menuItem[8])

{

//JOptionPane.showMessageDialog(null,"ThisisaMiniEdit.");

}

}

//Readfile

publicvoidopenFile(StringfileName){

try{

Filefile=newFile(fileName);

FileReaderreadIn=newFileReader(file);

intsize=(int)file.length();

intcharsRead=0;

char[]content=newchar[size];

while(***.ready())

charsRead+=***.read(content,charsRead,size-charsRead);

readIn.close();

textArea.setText(newString(content,0,charsRead));

}catch(Exceptione)

{

System.out.println("Erroropeningfile!");

}

}

//writefile

publicvoidwriteFile(StringfileName){

try{

Filefile=newFile(fileName);

FileWriterwrite=newFileWriter(file);

write.write(textArea.getText());

write.close();

}catch(Exceptione){

System.out.println("Errorclosingfile!");

就这样交,不会错的

版权声明:文章由 百问九 整理收集,来源于互联网或者用户投稿,如有侵权,请联系我们,我们会立即处理。如转载请保留本文链接:https://www.baiwen9.com/answer/386196.html
热门文章
二维码