📄 wengdr.java
字号:
import java.awt.Graphics;
import java.io.*;
import java.lang.*;
import java.lang.Object;
import java.awt.*;
import java.awt.event.*;
import java.awt.Font;
import java.awt.color.*;
import java.awt.datatransfer.*;
import javax.swing.*;
public class Wengdr extends Frame implements ActionListener{
//定义变量
String filename="wengdr";
Color color=Color.blue,tempcolor;
int size=25,tempsize;
int style=Font.PLAIN,tempstyle;
String name="宋体",tempname;
static int fromindex=0;//指示字符串的位置
Toolkit toolkit=Toolkit.getDefaultToolkit();
Clipboard clipboard=toolkit.getSystemClipboard();//剪贴板对象
// TextArea textarea=new TextArea("wengdr is a programfan",35,110,TextArea.SCROLLBARS_VERTICAL_ONLY);
JTextArea textarea=new JTextArea("wengdr is a programfan",60,69);
MenuBar menubar=new MenuBar();
TextField txt1=new TextField(15);
TextField txt2=new TextField(14);
TextField txt3=new TextField(14);
TextField txt4=new TextField(25);
TextField txt5=new TextField(10);
TextField txt6=new TextField(10);//各对话框中的文本
List list1=new List(6);
List list2=new List(6);
List list3=new List(6);
Label label=new Label("淮海工学院 ");
Choice choice=new Choice();
Menu Menu1=new Menu(" 文件 ");
Menu Menu2=new Menu(" 编辑 ");
Menu Menu3=new Menu(" 格式 ");
Menu Menu4=new Menu(" 帮助 ");//菜单
MenuItem MenuItem1=new MenuItem("新建");
MenuItem MenuItem2=new MenuItem("打开");
MenuItem MenuItem3=new MenuItem("保存");
MenuItem MenuItem4=new MenuItem("另存为");
MenuItem MenuItem5=new MenuItem("退出");
MenuItem MenuItem6=new MenuItem("撤消");//
MenuItem MenuItem7=new MenuItem("剪切");
MenuItem MenuItem8=new MenuItem("复制");//
MenuItem MenuItem9=new MenuItem("粘贴");
MenuItem MenuItem10=new MenuItem("删除");//
MenuItem MenuItem11=new MenuItem("查找");
MenuItem MenuItem12=new MenuItem("查找下一个");
MenuItem MenuItem13=new MenuItem("替换");
MenuItem MenuItem14=new MenuItem("全选");
MenuItem MenuItem15=new MenuItem("时间/日期");
MenuItem MenuItem16=new MenuItem("中文/英文转换");
MenuItem MenuItem17=new MenuItem("字体");
MenuItem MenuItem18=new MenuItem("关于作者");
MenuItem MenuItem19=new MenuItem("关于记事本");//菜单项
FileDialog openfile=new FileDialog(this,"打开文件对话框",FileDialog.LOAD);
FileDialog savefile=new FileDialog(this,"保存文件对话框",FileDialog.SAVE);
FileDialog saveasfile=new FileDialog(this,"另存文件对话框",FileDialog.SAVE);
// Dialog time=new Dialog(this,"当前时间",true);
Dialog replace=new Dialog(this,"替换",true);
Dialog search=new Dialog(this,"查找",true);
Dialog Word=new Dialog(this,"字体",true);
Dialog aboutAuthor=new Dialog(this,"关于作者",true);
Dialog aboutNotepad=new Dialog(this,"关于记事本",true);//各个菜单项的对话框
//定义构造函数
public Wengdr(String ss){
super(ss);
textarea.setLineWrap(true);//自动换行
this.setResizable(false);
tempstyle=style;
tempsize=size;
tempcolor=color;
tempname=name;
setBackground(Color.white);
setLayout(new FlowLayout());
MenuItem1.addActionListener(this);
MenuItem2.addActionListener(this);
MenuItem3.addActionListener(this);
MenuItem4.addActionListener(this);
MenuItem5.addActionListener(this);
MenuItem6.addActionListener(this);
MenuItem6.setEnabled(false);
MenuItem7.addActionListener(this);
MenuItem8.addActionListener(this);
MenuItem9.addActionListener(this);
MenuItem10.addActionListener(this);
MenuItem11.addActionListener(this);
MenuItem12.addActionListener(this);
MenuItem12.setEnabled(false);
MenuItem13.addActionListener(this);
MenuItem14.addActionListener(this);
MenuItem15.addActionListener(this);
MenuItem15.setEnabled(false);
MenuItem16.addActionListener(this);
MenuItem16.setEnabled(false);
MenuItem17.addActionListener(this);
MenuItem18.addActionListener(this);
MenuItem19.addActionListener(this);//监听菜单项
Menu1.add(MenuItem1);
Menu1.add(MenuItem2);
Menu1.add(MenuItem3);
Menu1.add(MenuItem4);
Menu1.addSeparator();
Menu1.add(MenuItem5);
menubar.add(Menu1);//文件菜单
Menu2.add(MenuItem6);
Menu2.addSeparator();
Menu2.add(MenuItem7);
Menu2.add(MenuItem8);
Menu2.add(MenuItem9);
Menu2.add(MenuItem10);
Menu2.addSeparator();
Menu2.add(MenuItem11);
Menu2.add(MenuItem12);
Menu2.add(MenuItem13);
Menu2.addSeparator();
Menu2.add(MenuItem14);
Menu2.add(MenuItem15);
menubar.add(Menu2);//编辑菜单
Menu3.add(MenuItem16);
Menu3.addSeparator();
Menu3.add(MenuItem17);
menubar.add(Menu3);//格式菜单
Menu4.add(MenuItem18);
Menu4.addSeparator();
Menu4.add(MenuItem19);
menubar.add(Menu4);//帮助
setMenuBar(menubar);
add(textarea);
replace_dialog();
search_dialog();
Dialog_Word( );
Dialog_aboutAuthor();
Dialog_aboutNotepad();//调用对话框
}
//各菜单项的事件处理
public void actionPerformed(ActionEvent e){
Object obj=e.getSource();
if(obj==MenuItem1){textarea.setText("");
}
if(obj==MenuItem2){
openfile.show();
filename=openfile.getDirectory()+openfile.getFile();
if(filename!=null)
readfile(filename);
}
if(obj==MenuItem3){
if(filename!=null)
writefile(filename);
}
if(obj==MenuItem4){
saveasfile.show();
filename=saveasfile.getDirectory()+saveasfile.getFile();
if(filename!=null)
writefile(filename);
}
if(obj==MenuItem5){System.exit(0);}
if(obj==MenuItem7){
String text=textarea.getSelectedText();
StringSelection selection=new StringSelection(text);
clipboard.setContents(selection,null);
textarea.replaceRange("",textarea.getSelectionStart(),textarea.getSelectionEnd());
}
if(obj==MenuItem8){
String text=textarea.getSelectedText();
StringSelection selection=new StringSelection(text);
clipboard.setContents(selection,null);
}
if(obj==MenuItem9){
Transferable contents=clipboard.getContents(this);
if(contents==null)
return;
String text;
text="";
try{
text=(String)contents.getTransferData(DataFlavor.stringFlavor);
}
catch(Exception exception){}
textarea.replaceRange(text,textarea.getSelectionStart(),textarea.getSelectionEnd());
}
if(obj==MenuItem10){
textarea.replaceRange("",textarea.getSelectionStart(),textarea.getSelectionEnd
());
}
if(obj==MenuItem11){search.show();}
if(obj==MenuItem13){replace.show();}
if(obj==MenuItem14){textarea.selectAll();}
if(obj==MenuItem17){Word.show();}
if(obj==MenuItem18){ aboutAuthor.show();}
if(obj==MenuItem19){aboutNotepad.show();}
}
//读取文件
public void readfile(String filename){
try {
File file=new File(filename);
FileReader readIn=new FileReader(file);
int file_size=(int)file.length();
int charsRead=0;
char[] content=new char[file_size];
while(readIn.ready())
charsRead+=readIn.read(content,charsRead,file_size-charsRead);
readIn.close();
textarea.setText(new String(content,0,charsRead));
}
catch(IOException e) {
System.out.println("Error! Opening file");
}
}
//保存文件
public void writefile(String filename){
try{
File file=new File(filename);
FileWriter write_out=new FileWriter(file);
write_out.write(textarea.getText());
write_out.close();
}
catch(IOException e){
System.out.println("Error! writing file");
}
}
//替换
public void replace_dialog(){
replace.setSize(200,200);
replace.setResizable(false);
replace.setLayout(new FlowLayout());
replace.setBackground(Color.pink);
Label label_1=new Label(" 查找");
Label label_2=new Label(" 替换");
Button button_1=new Button(" 确定 ");
Button button_2=new Button(" 清空 ");
replace.add(label_1);
replace.add(txt5);
replace.add(label_2);
replace.add(txt6);
replace.add(button_1);
replace.add(button_2);
button_1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(txt5.getText()=="");
else{
int locate=textarea.getText().indexOf(txt5.getText(),0);
textarea.select(locate,locate+txt5.getText().length());
textarea.replaceRange(txt6.getText(),locate,locate+txt5.getText().length());
}
txt5.setText("");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -