import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.awt.datatransfer.*;
class MyMenuBar extends MenuBar{
public MyMenuBar(Frame f){
f.setMenuBar(this);
}//end 构造函数
public void addMenu(String[] menu){
for(int i=0;i<menu.length;i++){
this.add(new Menu(menu[i]));
}
}//end addMenu
public void addMenuItems(int dex,String[] items){
for(int i=0;i<items.length;i++){
if(items[i]!=null)this.getMenu(dex).add(new MenuItem(items[i]));
else this.getMenu(dex).addSeparator();
}//end for
}//end addMenuItems
public void addListener(ActionListener al){
for(int i=0;i<this.getMenuCount();i++)
for(int j=0;j<this.getMenu(i).getItemCount();j++)
this.getMenu(i).getItem(j).addActionListener(al);
}//end addListener
}//end class MyMenuBar
class MyClipboard{
private Clipboard cb;
public MyClipboard(){
Toolkit tk=Toolkit.getDefaultToolkit();
cb=tk.getSystemClipboard();
}//end 构造函数
public void setData(String data){
cb.setContents(new StringSelection(data),null);
}//end setData 将字符串存入剪切板
public String getData() throws IOException {
Transferable content=cb.getContents(null);
try{
String data=(String)content.getTransferData(DataFlavor.stringFlavor);
return data;
}catch(Exception e){}
return null;
}//从剪切板读取数据
}//end class MyClipboard
class MyFile{
private FileDialog fdlg;
public MyFile(Frame f){
fdlg=new FileDialog(f,"",FileDialog.LOAD);
}//end
public void saveFile(String data) throws IOException {
fdlg.setTitle("保存文件");
fdlg.setVisible(true);
fdlg.setMode(FileDialog.SAVE);
BufferedWriter bw=new BufferedWriter(new FileWriter(getpath()));
bw.write(data);
bw.close();
}//end savafile
public String openFile() throws IOException {
fdlg.setTitle("打开文件");
fdlg.setVisible(true);
fdlg.setMode(FileDialog.LOAD);
BufferedReader br=new BufferedReader(new FileReader(getpath()));
String s;StringBuffer sb=new StringBuffer();
while((s=br.readLine())!=null)sb.append(s+'\n');
br.close();
return sb.toString();
}//end openfile
public String getpath(){
return fdlg.getDirectory()+"\\"+fdlg.getFile();
}//end getpath
}//end class Myfile
class MyFindDialog extends Dialog implements ActionListener{
private Label lFind=new Label("待查字符串:");
private Label lReplace=new Label("替换字符串:");
private TextField tFind=new TextField(10);
private TextField tReplace=new TextField(10);
private Button bFind=new Button("查找");
private Button bReplace=new Button("替换");
private TextArea ta;
public MyFindDialog(Frame owner, TextArea ta){
super(owner,"查找",false);
this.ta=ta;
setLayout(null);
lFind.setBounds(10,30,80,20);
lReplace.setBounds(10,70,80,20);
tFind.setBounds(90,30,90,20);
tReplace.setBounds(90,70,90,20);
bFind.setBounds(190,30,80,20);
bReplace.setBounds(190,70,80,20);
add(lFind); add(tFind); add(bFind); add(lReplace);
add(tReplace); add(bReplace);
setResizable(false);
bFind.addActionListener(this);
bReplace.addActionListener(this);
addWindowListener( new WindowAdapter(){
public void windowClosing(WindowEvent e){
MyFindDialog.this.dispose();
}
});
}//end gouzao
public void showFind(){
setTitle("查找");
setSize(280,60);
setVisible(true);
}
public void showReplace(){
setTitle("查找替换");
setSize(280,110);
setVisible(true);
}
private void find(){
String text=ta.getText();
String str=tFind.getText();
int end=text.length();
int len=str.length();
int start=ta.getSelectionEnd();
if(start==end)start=0;
for( ;start><=end-len;start++){
if(text.substring(start,start+len).equals(str)){
ta.setSelectionStart(start);
ta.setSelectionEnd(start+len);
return;
}
}
ta.setSelectionStart(end);
ta.setSelectionEnd(end);
}
private void replace(){
String str=tReplace.getText();
if(ta.getSelectedText().equals(tFind.getText()))
ta.replaceRange(str,ta.getSelectionStart(),ta.getSelectionEnd());
else find();
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==bFind)
find();
else if(e.getSource()==bReplace)
replace();
}
}
class MyHelp extends Frame{
static TextArea t=new TextArea();
public MyHelp() throws Exception{
setTitle("有关记事本");
add(t);
BufferedReader br=new BufferedReader(new FileReader("D:\\Java\\leaf\\wu\\1.txt"));
String s;
while((s=br.readLine())!=null)t.append(s+'\n');
br.close();
setVisible(true);
pack();
addWindowListener( new WindowAdapter(){
public void windowClosing(WindowEvent e){
MyHelp.this.dispose();
}
});
}//end gouzaou
}//end myhelp
public class MyNote implements ActionListener{
static TextArea ta;
static Frame f;
public MyNote(){
ta=new TextArea();
}//end 构造函数
public static void main(String[] args) {
f=new Frame("记事本");
MyMenuBar mb=new MyMenuBar(f);
mb.addMenu(new String[]{"文件","编辑","查找","帮助"});
mb.addMenuItems(0,new String[]{"新建","打开","保存","退出"});
mb.addMenuItems(1,new String[]{"剪切","复制","清除","全选","粘贴"});
mb.addMenuItems(2,new String[]{"查找","替换查找"});
mb.addMenuItems(3,new String[]{"关于记事本"});
mb.addListener(new MyNote());
f.add(ta);
f.setVisible(true);
f.pack();
}//end main
public void actionPerformed(ActionEvent e){
String eg=e.getActionCommand();
if(eg.equals("新建")){
ta.setText(" ");
}//新建
if(eg.equals("打开")){
MyFile mf=new MyFile(f);
try{
ta.setText(mf.openFile());
}catch(Exception ei){System.out.println("打开失败");}
}//
if(eg.equals("保存")){
MyFile mf=new MyFile(f);
try{
mf.saveFile(ta.getText());
}catch(Exception ei){System.out.println("保存失败");}
}//
if(eg.equals("退出")){
f.dispose();
}//
if(eg.equals("剪切")){
String selected=ta.getSelectedText();
MyClipboard mcp=new MyClipboard();
mcp.setData(selected);
ta.replaceRange(" ",ta.getSelectionStart(),ta.getSelectionEnd());
}//
if(eg.equals("复制")){
String selected=ta.getSelectedText();
MyClipboard mcp=new MyClipboard();
mcp.setData(selected);
}//
if(eg.equals("粘贴")){
String selected=ta.getSelectedText();
MyClipboard mcp=new MyClipboard();
try{
ta.insert(mcp.getData(),ta.getSelectionEnd());
}catch(Exception ei){}
}
if(eg.equals("清除")){
ta.replaceRange(" ",ta.getSelectionStart(),ta.getSelectionEnd());
}
if(eg.equals("全选")){
ta.setSelectionStart(0);
ta.setSelectionEnd(ta.getText().length());
}
if(eg.equals("查找")){
MyFindDialog mfd=new MyFindDialog(f,ta);
mfd.showFind();
}
if(eg.equals("替换查找")){
MyFindDialog mfd=new MyFindDialog(f,ta);
mfd.showReplace();
}
if(eg.equals("关于记事本")){
try{
MyHelp mh=new MyHelp();
}catch(Exception ei){}
}
}//end actionperformed
}//end mynote
转载于:https://www.cnblogs.com/lan0725/archive/2007/11/01/1873984.html