java记事本

——- android培训、java培训、期待与您交流! ———-

import java.awt.*;import java.awt.event.*;import java.io.*;import java.awt.datatransfer.*;import java.util.Date;public class NoteBook {private Frame f;private TextArea ta;private MenuBar mb;private Menu m1file, m2editor, m3formart, m4view, m5help;private MenuItem m1new, m1open, m1save, m1saveas, m1pageset, m1print,m1quit;private MenuItem m2back, m2copy, m2cute, m2plast, m2delete, m2find, m2findnext, m2replace, m2goto, m2selectall, m2date;private MenuItem m3autonextline, m3font;private MenuItem m4statebar;private MenuItem m5helpme, m5about;private Dialog dhelp,dabout;private Label labout,lhelp;private Button bhelp,babout;private FileDialog openDia, saveDia;private File file = null;private Clipboard sysClip = Toolkit.getDefaultToolkit().getSystemClipboard();NoteBook() {init();} main(String[] args) {new NoteBook();}void init() {f = new Frame(“自制记事本”);f.setBounds(100, 100, 800, 800);// f.setLayout(new FlowLayout());mb = new MenuBar();m1file = new Menu(“文件”);m1new = new MenuItem(“新建”);m1file.add(m1file);m1open = new MenuItem(“打开”);m1file.add(m1open);m1save = new MenuItem(“保存”);m1file.add(m1save);m1saveas = new MenuItem(“另存为”);m1file.add(m1saveas);m1pageset = new MenuItem(“-页面设置”);m1file.add(m1pageset);m1print = new MenuItem(“-打印”);m1file.add(m1print);m1quit = new MenuItem(“退出”);m1file.add(m1quit);m2editor = new Menu(“编辑”);m2back = new MenuItem(“-撤销”);m2editor.add(m2back);m2copy = new MenuItem(“复制”);m2editor.add(m2copy);m2cute = new MenuItem(“剪切”);m2editor.add(m2cute);m2plast = new MenuItem(“粘贴”);m2editor.add(m2plast);m2delete = new MenuItem(“删除”);m2editor.add(m2delete);m2find = new MenuItem(“-查找”);m2editor.add(m2find);m2findnext = new MenuItem(“-查找下一个”); m2editor.add(m2findnext);m2replace = new MenuItem(“-替换”);m2editor.add(m2replace);m2goto = new MenuItem(“-转到”);m2editor.add(m2goto);m2selectall = new MenuItem(“全选”);m2editor.add(m2selectall);m2date = new MenuItem(“日期”);m2editor.add(m2date);m3formart = new Menu(“格式”);m3autonextline=new MenuItem(“-自动换行”); m3formart.add(m3autonextline);m3font = new MenuItem(“-字体设置”);m3formart.add(m3font);m4view = new Menu(“查看”);m4statebar = new MenuItem(“-状态栏”);m4view.add(m4statebar);m5help = new Menu(“帮助”);m5helpme = new MenuItem(“帮助”);m5help.add(m5helpme);m5about = new MenuItem(“关于”);m5help.add(m5about);mb.add(m1file);mb.add(m2editor);mb.add(m3formart);mb.add(m4view);mb.add(m5help);f.setMenuBar(mb);openDia = new FileDialog(f, “打开”, FileDialog.LOAD);saveDia = new FileDialog(f, “打开”, FileDialog.SAVE);ta = new TextArea();f.add(ta);dhelp = new Dialog(f,”帮助”,true);dabout= new Dialog(f,”关于”,true);;lhelp = new Label(“很简单,实现了部分功能。菜单中没有\”-\”的都实现了”);labout = new Label(“由于换行符号的问题,美国服务器,剪切和删除有bug”);bhelp = new Button(“确定”);babout = new Button(“确定”);dhelp.add(lhelp);dhelp.add(bhelp);dabout.add(labout);dabout.add(babout);dhelp.setBounds(200, 200, 500, 100);dabout.setBounds(200, 200, 500, 100);dhelp.setLayout(new FlowLayout());dabout.setLayout(new FlowLayout());myEvent();f.setVisible(true);}private void myEvent() {m1open.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){openDia.setVisible(true);String dirPath = openDia.getDirectory();String fileName = openDia.getFile();if(dirPath==null || fileName==null)return ;ta.setText(“”);file = new File(dirPath,fileName);try{BufferedReader bufr = new BufferedReader(new FileReader(file));String line = null;while((line=bufr.readLine())!=null)ta.append(line+”\r\n”);bufr.close();}catch (IOException ex){throw new RuntimeException(“读取失败”);}}});m1save.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {if (file == null) {saveDia.setVisible(true);String dirPath = saveDia.getDirectory();String fileName = saveDia.getFile();if (dirPath == null || fileName == null)return;file = new File(dirPath, fileName);}try {BufferedWriter bufw = new BufferedWriter(new FileWriter(file));String text = ta.getText();bufw.write(text);bufw.close();} catch (IOException ex) {throw new RuntimeException();}}});m1saveas.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {saveDia.setVisible(true);String dirPath = saveDia.getDirectory();String fileName = saveDia.getFile();if (dirPath == null || fileName == null)return;file = new File(dirPath, fileName);try {BufferedWriter bufw = new BufferedWriter(new FileWriter(file));String text = ta.getText();bufw.write(text);bufw.close();} catch (IOException ex) {throw new RuntimeException();}}});m2copy.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){String selected = ta.getSelectedText();if (selected.length() > 0)copyToClipboard(selected);}});m2cute.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){String selected = ta.getSelectedText();if (selected.length() > 0){int start = ta.getSelectionStart();int end = ta.getSelectionEnd();copyToClipboard(selected);ta.replaceRange(“”, start,end);}}});m2plast.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){String selected = ta.getSelectedText();String contens = copyFromClipboard();if (selected.length() != 0){int start = ta.getSelectionStart();int end = ta.getSelectionEnd();ta.replaceRange(contens, start,end);}else{int pos = ta.getCaretPosition() ;ta.insert(contens, pos);}}});m2delete.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){String selected = ta.getSelectedText();if (selected.length() != 0){int start = ta.getSelectionStart();int end = ta.getSelectionEnd();ta.replaceRange(“”, start,end);}}});m2selectall.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){ta.selectAll();}});m2date.addActionListener(new ActionListener(){@SuppressWarnings(“deprecation”)public void actionPerformed(ActionEvent e){String selected = ta.getSelectedText();Date myDate = new Date();String contens = myDate.toLocaleString();//contens = contens.substring(0,10);contens = contens.replaceAll(“\\s\\S+$”,””);if (selected.length() != 0){int start = ta.getSelectionStart();int end = ta.getSelectionEnd();ta.replaceRange(contens, start,end);}else{int pos = ta.getCaretPosition() ;ta.insert(contens, pos);}}});m5helpme.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){dhelp.setVisible(true);}});m5about.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){dabout.setVisible(true);}});dabout.addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) {dabout.setVisible(false);}});f.addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) {System.exit(0);}});m1quit.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {System.exit(0);}});bhelp.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {dhelp.setVisible(false);}});dhelp.addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) {dhelp.setVisible(false);}});babout.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {dabout.setVisible(false);}});dabout.addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) {dabout.setVisible(false);}});}public String copyFromClipboard() {String ret = “”;Transferable clipTf = sysClip.getContents(null);if (clipTf != null) {if (clipTf.isDataFlavorSupported(DataFlavor.stringFlavor)) {try {ret = (String) clipTf.getTransferData(DataFlavor.stringFlavor);} catch (Exception e) {e.printStackTrace();}}}return ret;}public void copyToClipboard(String writeMe) {Transferable tText = new StringSelection(writeMe);sysClip.setContents(tText, null);}},服务器空间,香港空间每天告诉自己我很棒!

java记事本

相关文章:

你感兴趣的文章:

标签云: