import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class MyNote implements ActionListener, ItemListener{
private MenuBar mb;
private Frame f;
private Menu menu,menuEdit;
private MenuItem minew,misave,miload,
miquit,micut,micopy,mipaste,mifind;
private CheckboxMenuItem cbmi;
private TextArea ta = new TextArea();
private Menu menuhelp= new Menu("help");
private Dialog diahelp,diafind;
private MenuItem miversion = new MenuItem("version");
private FileDialog fd ;
private FileDialog fdsave;
String cutText = "";
TextField tffind;
Button btnfind;
int locate=0 ;
public MyNote(){
mb = new MenuBar();
f = new Frame("title");
fd = new FileDialog(f,"打开",FileDialog.LOAD);
fdsave = new FileDialog(f,"保存",FileDialog.SAVE);
diahelp = new Dialog(f,"Mydialog",true);
TextArea taver = new TextArea("version 1.0");
diahelp.add(taver);
diahelp.setSize(300,200);
diahelp.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
diahelp.dispose();
}
});
tffind = new TextField(20);
btnfind = new Button("next");
diafind = new Dialog(f,"Find",false);
diafind.setLayout(new FlowLayout());
//diafind.pack();
diafind.setSize(300,60);
diafind.add(tffind);
diafind.add(btnfind);
diafind.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
diafind.dispose();
}
});
menuEdit = new Menu("Edit");
menu = new Menu("File");
minew = new MenuItem("New");
misave = new MenuItem("Save");
miload = new MenuItem("Open");
miquit = new MenuItem("Quit");
micut = new MenuItem("Cut");
micopy = new MenuItem("Copy");
mipaste = new MenuItem("Paste");
mifind = new MenuItem("Find");
cbmi = new CheckboxMenuItem("Checkbox");
menu.add(minew);
menu.add(misave);
menu.add(miload);
menu.addSeparator();
menu.add(miquit);
menu.add(cbmi);
menuhelp.add(miversion);
mb.add(menu);
mb.add(menuEdit);
mb.add(menuhelp);
menuEdit.add(micut);
menuEdit.add(micopy);
menuEdit.add(mipaste);
menuEdit.add(mifind);
f.setMenuBar(mb);
f.add(ta);
f.setSize(400,600);
f.show();
mifind.addActionListener(this);
minew.addActionListener(this);
misave.addActionListener(this);
miload.addActionListener(this);
miquit.addActionListener(this);
miversion.addActionListener(this);
mipaste.addActionListener(this);
micopy.addActionListener(this);
btnfind.addActionListener(this);
cbmi.addItemListener(this);
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
micut.addActionListener(this);
}
public static void main(String[] args){
new MyNote();
}
public void actionPerformed(ActionEvent e){
String cmd = e.getActionCommand();
System.out.println("Button \"" + e.getActionCommand() + "\"pressed.");
if (cmd.equals("Quit")){
System.exit(0);
}
if (cmd.equals("Open")){
fd.show();
}
if (cmd.equals("Cut")){
System.out.println("cut");
cutText = ta.getSelectedText();
ta.replaceRange("",ta.getSelectionStart(),ta.getSelectionEnd());
System.out.println(cutText);
}
if (cmd.equals("Paste")){
System.out.println("paste");
ta.replaceRange(cutText,ta.getSelectionStart(),ta.getSelectionEnd());
}
if (cmd.equals("version")){
diahelp.show();
}
if (cmd.equals("Save")){
fdsave.show();
String saveName = "";
String sdir = fdsave.getDirectory();
String sname = fdsave.getFile();
System.out.println("sdir: " + sdir);
System.out.println("sname: " + sname);
saveName = sdir + sname;
System.out.println("saveName: " + saveName);
String text = ta.getText();
FileWriter fw = null;
PrintWriter pw = null;
String str = null;
try{
fw = new FileWriter(saveName);
pw = new PrintWriter(fw);
pw.print(text);
pw.close();
fw.close();
}
catch(IOException eas){
eas.printStackTrace();
}
}
if (cmd.equals("Open")){
System.out.println("Open clicked");
String name = fd.getFile();
String dir = fd.getDirectory();
System.out.println("dir: " + dir);
System.out.println("name: " + name);
String aline;
FileReader fr = null;
BufferedReader br=null;
int count = 0;
// FileWriter fw = null;
String temp = null;
try{
fr = new FileReader(dir + name);
br = new BufferedReader(fr);
aline = new String();
while((aline = br.readLine())!=null){
count ++;
temp = temp + aline;
}
ta.setText(temp);
}
catch(IOException a){
a.printStackTrace();
}finally{
try{
fr.close();
br.close();
}
catch(Exception ex){
ex.printStackTrace();
}
}
}
if (cmd.equals("Copy")){
cutText=ta.getSelectedText();
}
if (cmd.equals("Find")){
System.out.println("Find clicked");
diafind.show();
}
if (cmd.equals("next")){
String strfind = tffind.getText();
String strta = ta.getText();
strta = strta.replaceAll("\r\n"," ");
int ibegin = strta.indexOf(strfind);
for(int i = locate ;i
System.out.println(strta.substring(i,i + strfind.length()));
if (strfind.equals(strta.substring(i, i + strfind.length() ))){
System.out.println(i);
ta.select(i,ibegin+strfind.length());
locate = i;
locate ++;
System.out.println(i);
System.out.println(locate);
break;
}
}
}
}
public void itemStateChanged(ItemEvent e){
String state = "deselected";
if(e.getStateChange()==ItemEvent.SELECTED){
state = "selected";
}
System.out.println(e.getItem() + " " + state );
}
}
阅读(1935) | 评论(5) | 转发(0) |