java statebar_java记事本

import java.awt.*;import java.awt.event.*;import java.io.*;import java.awt.datatransfer.*;importjava.util.Date;public classNoteBook {privateFrame f;privateTextArea ta;privateMenuBar mb;privateMenu m1file, m2editor, m3formart, m4view, m5help;privateMenuItem m1new, m1open, m1save, m1saveas, m1pageset, m1print,m1quit;privateMenuItem m2back, m2copy, m2cute, m2plast, m2delete, m2find, m2findnext, m2replace, m2goto, m2selectall, m2date;privateMenuItem m3autonextline, m3font;privateMenuItem m4statebar;privateMenuItem m5helpme, m5about;privateDialog dhelp,dabout;privateLabel labout,lhelp;privateButton bhelp,babout;privateFileDialog openDia, saveDia;private File file = null;private Clipboard sysClip =Toolkit.getDefaultToolkit().getSystemClipboard();

NoteBook() {

init();

}public static voidmain(String[] args) {newNoteBook();

}voidinit() {

f= new Frame("自制记事本");

f.setBounds(100, 100, 800, 800);//f.setLayout(new FlowLayout());

mb = newMenuBar();

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= newTextArea();

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(newFlowLayout());

dabout.setLayout(newFlowLayout());

myEvent();

f.setVisible(true);

}private voidmyEvent() {

m1open.addActionListener(newActionListener()

{public voidactionPerformed(ActionEvent e)

{

openDia.setVisible(true);

String dirPath=openDia.getDirectory();

String fileName=openDia.getFile();if(dirPath==null || fileName==null)return;

ta.setText("");

file= newFile(dirPath,fileName);try{

BufferedReader bufr= new BufferedReader(newFileReader(file));

String line= null;while((line=bufr.readLine())!=null)

ta.append(line+"\r\n");

bufr.close();

}catch(IOException ex)

{throw new RuntimeException("读取失败");

}

}

});

m1save.addActionListener(newActionListener() {public voidactionPerformed(ActionEvent e) {if (file == null) {

saveDia.setVisible(true);

String dirPath=saveDia.getDirectory();

String fileName=saveDia.getFile();if (dirPath == null || fileName == null)return;

file= newFile(dirPath, fileName);

}try{

BufferedWriter bufw= new BufferedWriter(newFileWriter(file));

String text=ta.getText();

bufw.write(text);

bufw.close();

}catch(IOException ex) {throw newRuntimeException();

}

}

});

m1saveas.addActionListener(newActionListener() {public voidactionPerformed(ActionEvent e) {

saveDia.setVisible(true);

String dirPath=saveDia.getDirectory();

String fileName=saveDia.getFile();if (dirPath == null || fileName == null)return;

file= newFile(dirPath, fileName);try{

BufferedWriter bufw= new BufferedWriter(newFileWriter(file));

String text=ta.getText();

bufw.write(text);

bufw.close();

}catch(IOException ex) {throw newRuntimeException();

}

}

});

m2copy.addActionListener(newActionListener(){public voidactionPerformed(ActionEvent e){

String selected=ta.getSelectedText();if (selected.length() > 0)

copyToClipboard(selected);

}

});

m2cute.addActionListener(newActionListener(){public voidactionPerformed(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(newActionListener(){public voidactionPerformed(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(newActionListener(){public voidactionPerformed(ActionEvent e){

String selected=ta.getSelectedText();if (selected.length() != 0){int start =ta.getSelectionStart();int end =ta.getSelectionEnd();

ta.replaceRange("", start,end);

}

}

});

m2selectall.addActionListener(newActionListener(){public voidactionPerformed(ActionEvent e){

ta.selectAll();

}

});

m2date.addActionListener(newActionListener(){

@SuppressWarnings("deprecation")public voidactionPerformed(ActionEvent e){

String selected=ta.getSelectedText();

Date myDate= newDate();

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(newActionListener(){public voidactionPerformed(ActionEvent e){

dhelp.setVisible(true);

}

});

m5about.addActionListener(newActionListener(){public voidactionPerformed(ActionEvent e){

dabout.setVisible(true);

}

});

dabout.addWindowListener(newWindowAdapter() {public voidwindowClosing(WindowEvent e) {

dabout.setVisible(false);

}

});

f.addWindowListener(newWindowAdapter() {public voidwindowClosing(WindowEvent e) {

System.exit(0);

}

});

m1quit.addActionListener(newActionListener() {public voidactionPerformed(ActionEvent e) {

System.exit(0);

}

});

bhelp.addActionListener(newActionListener() {public voidactionPerformed(ActionEvent e) {

dhelp.setVisible(false);

}

});

dhelp.addWindowListener(newWindowAdapter() {public voidwindowClosing(WindowEvent e) {

dhelp.setVisible(false);

}

});

babout.addActionListener(newActionListener() {public voidactionPerformed(ActionEvent e) {

dabout.setVisible(false);

}

});

dabout.addWindowListener(newWindowAdapter() {public voidwindowClosing(WindowEvent e) {

dabout.setVisible(false);

}

});

}publicString 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();

}

}

}returnret;

}public voidcopyToClipboard(String writeMe) {

Transferable tText= newStringSelection(writeMe);

sysClip.setContents(tText,null);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值