new_file.setAccelerator(SWT.CTRL + 'N'); //新建文件快捷键 open_file.setAccelerator(SWT.CTRL + 'O');//保存文件快捷键 undo.setAccelerator(SWT.CTRL + 'Z'); //撤销快捷键 cut.setAccelerator(SWT.CTRL + 'T'); //剪切快捷键 copy.setAccelerator(SWT.CTRL + 'C'); //复制快捷键 paste.setAccelerator(SWT.CTRL + 'V'); //粘贴快捷键 delete.setAccelerator(SWT.DEL); //删除快捷键 find.setAccelerator(SWT.CTRL + 'F'); //查找快捷键 find_next.setAccelerator(SWT.F3); //查找下一处快捷键 replace.setAccelerator(SWT.CTRL + 'H'); //替换快捷键 go_to.setAccelerator(SWT.CTRL + 'G'); //转到快捷键 selectAll.setAccelerator(SWT.CTRL + 'A');//全选快捷键 showDate.setAccelerator(SWT.F5); //显示时间快捷键
quit.addSelectionListener(new SelectionAdapter() {
/**
* 添加"退出"菜单的监听事件
* 功能描述:触发该菜单将退出应用程序
*/
public void widgetSelected(SelectionEvent e) {
System.exit(0);
}
});
public void DIYquit(){
/**
* 退出函数退出时进行判断是否需要保存
*/
String tips;
System.out.println(Util.shell.getText().getText());
if(Util.shell.fileDir != null || Util.shell.getText().getText() != ""){ //文件目录不为空,说明有打开着的文件,需要询问是否保存
if(Util.shell.fileDir == null){
tips = "文件 无标题 的文字已经改变。\n"+"想保存文件吗";
}else{
tips = "文件 " +Util.shell.fileDir + " 的文字已经改变。\n"+"想保存文件吗";
}
int n=JOptionPane.showConfirmDialog(null, tips,"记事本",JOptionPane.YES_NO_CANCEL_OPTION);
if(n==0){ //是 返回0 否返回1 取消 返回2
SaveMethod savemethod = new SaveMethod();
savemethod.Save(); //点是,则保存文件然后打开文件
}else if(n==1){ //返回否
System.exit(0);
}else if(n==2){ //返回取消
return ;
}
System.exit(0);
}else{
if(Util.shell.getText().getText() != ""){ //若目录为空,但是内容不为空,说明有内容但未保存,提示之
tips = "文件 无标题 的文字已经改变。\n"+"想保存文件吗";
int n=JOptionPane.showConfirmDialog(null, tips,"记事本",JOptionPane.YES_NO_CANCEL_OPTION);
if(n==0){ //是 返回0 否返回1 取消 返回2
SaveMethod savemethod = new SaveMethod();
savemethod.Save(); //点是,则保存文件然后打开文件
}else if(n==1){ //返回否
System.exit(0);
}else if(n==2){ //返回取消
return ;
}
System.exit(0);
}else{
}
}
System.exit(0);
}
public void SaveAs(){
/**
* 将另存为这个方法进行封装,方便调用
*/
FileDialog dialog = new FileDialog(Util.shell.shell,SWT.SAVE);
dialog.setFilterPath(System.getProperty("C:\\Documents and Settings"));//设置打开默认的路径
dialog.setFilterExtensions(new String[] {"*.txt", "*.*"}); //设置所打开文件的扩展名
dialog.setFilterNames( new String[]{"Text Files (*.txt)", "All Files (*.*)"}); //设置显示到下拉框中的扩展名的名称
String file = dialog.open();//打开窗口,返回用户所选的文件目录
if(file != null){
Util.shell.fileDir = file;//将文件目录保存下来,方面之后的使用
}
if ( file != null )
{
Util.shell.shell.setText((new File( file.trim())).getName());//获取文件名(不含路径)用于设置title
FileWriter fileWriter;
try {
fileWriter = new FileWriter(file);
BufferedWriter out = new BufferedWriter(fileWriter);
out.write(Util.shell.getText().getText());
out.close();
fileWriter.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
public void Save(){
/**
* 保存文件
*/
if(Util.shell.fileDir!=null ){ //表示该文件有目录,保存时直接保存,不需要弹窗
Util.shell.shell.setText((new File( Util.shell.fileDir.trim())).getName());//获取文件名(不含路径)用于设置title
FileWriter fileWriter;
try {
fileWriter = new FileWriter(Util.shell.fileDir);
BufferedWriter out = new BufferedWriter(fileWriter);
out.write(Util.shell.getText().getText());
out.close();
fileWriter.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
else{//否则为从未保存过,则调用另存为窗口
SaveAs();
}
}
public void OpenFile(){
if(Util.shell.fileDir != null || Util.shell.getText().getText() != ""){
String tips = "文件 " +Util.shell.fileDir + " 的文字已经改变。\n"+"想保存文件吗";
int n=JOptionPane.showConfirmDialog(null, tips,"记事本",JOptionPane.YES_NO_CANCEL_OPTION);
if(n==0){ //是 返回0 否返回1 取消 返回2
SaveMethod savemethod = new SaveMethod();
savemethod.Save(); //点是,则保存文件然后打开文件
}else if(n==1){ //返回否
}else if(n==2){ //返回取消
return ;
}
}else{
FileDialog dialog = new FileDialog(Util.shell.shell,SWT.OPEN);
dialog.setFilterPath(System.getProperty("C:\\Documents and Settings"));//设置打开默认的路径
dialog.setFilterExtensions(new String[] {"*.txt", "*.*"});//设置所打开文件的扩展名
dialog.setFilterNames( new String[]{"Text Files (*.txt)", "All Files (*.*)"});//设置显示到下拉框中的扩展名的名称
String file = dialog.open();//打开窗口,返回用户所选的文件目录(包括文件名)
if(file == null){
return ;
}
Util.shell.fileDir = file;
Util.shell.shell.setText((new File( file.trim())).getName());//获取文件名(不含路径)用于设置title
FileReader filereader;
try {
filereader = new FileReader(file);
BufferedReader in = new BufferedReader(filereader);
String s = null;
Util.shell.getText().setText(""); //将编辑框内容制空
try {
while((s=in.readLine())!=null){
Util.shell.getText().append(s+"\n");
}
} catch (IOException e1) {
e1.printStackTrace();
}
} catch (FileNotFoundException e2) {
e2.printStackTrace();
}
}
}
void openNewFile() {
if(Util.shell.fileDir != null || Util.shell.getText().getText() != ""){
String tips = "文件 " +Util.shell.fileDir + " 的文字已经改变。\n"+"想保存文件吗";
int n=JOptionPane.showConfirmDialog(null, tips,"记事本",JOptionPane.YES_NO_CANCEL_OPTION);
if(n==0){ //是 返回0 否返回1 取消 返回2
SaveMethod savemethod = new SaveMethod(); //点是,则保存文件然后打开文件
}else if(n==1){ //返回否
}else if(n==2){ //返回取消
return ;
}
String t = "无标题(" + ++Util.shell.cnt + ") - 记事本";
Util.shell.shell.setText(t); //设置标题
Util.shell.getText().setText(""); //清空编辑框
Util.shell.fileDir = null;
}else{
String t = "无标题(" + ++Util.shell.cnt + ") - 记事本";
Util.shell.shell.setText(t); //设置标题
Util.shell.getText().setText(""); //清空编辑框
Util.shell.fileDir = null;
}
//所有的数据都初始化
}
showDate.addSelectionListener(new SelectionAdapter() {
/**
* 添加"时间日期"菜单的监听事件
* 功能描述:触发该菜单将当前系统时间添加到编辑框光标结尾处
*/
public void widgetSelected(SelectionEvent e) {
int index = getText().getCaretPosition(); //获取当前编辑框中光标的位置
//System.out.println(index);
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
String localTime = df.format(new Date());
String newString = insertString(getText().getText(),localTime,index); //拼接好后的字符串
getText().setText("");//将原来编辑框的内容清空再将新的内容显示出来
//System.out.println(localTime);// new Date()为获取当前系统时间
getText().append(newString); //将拼接好的字符串显示出来
}
});
public String insertString(String s1,String s2,int index){
/**
* 实现字符串的插入函数
* 传入的三个参数为:原串,要插入的串,以及插入的位置
* 返回的为插入后的字符串
*/
return s1.substring(0, index) + s2 + s1.substring(index, s1.length());
}
selectAll.addSelectionListener(new SelectionAdapter() {
/**
* 添加"全选"菜单的监听事件
* 功能描述:触发该菜单将全部选中文本域的文字,同时将"删除" "剪切"这两个按钮设置成可选
*/
public void widgetSelected(SelectionEvent e) {
getText().selectAll(); //直接调用自带的函数哟
if(getText().getText() !=null){ //若内容不为空,说明全选有内容,因此需要将"删除" "剪切" "复制"这三个按钮设置成可选
delete.setEnabled(true); //将复制
cut.setEnabled(true);
copy.setEnabled(true);
}
}
});
cut.addSelectionListener(new SelectionAdapter() {
/**
* 添加"剪切"菜单的监听事件
* 功能描述:触发该菜单将选中的字符串进行剪切
*/
public void widgetSelected(SelectionEvent e) {
getText().cut();
}
});
paste.addSelectionListener(new SelectionAdapter() {
/**
* 添加"粘贴"菜单的监听事件
* 功能描述:触发该菜单将剪切板中的内容粘贴到编辑框中
*/
public void widgetSelected(SelectionEvent e) {
getText().paste();
}
});
copy.addSelectionListener(new SelectionAdapter() {
/**
* 添加"剪切"菜单的监听事件
* 功能描述:触发该菜单将选中的字符串进行剪切
*/
public void widgetSelected(SelectionEvent e) {
text.copy ();
}
find.addSelectionListener(new SelectionAdapter() {
/**
* 添加查找功能的对话框,其中需要点击查找,然后弹出一个对话框,输入查找的相关内容
*/
public void widgetSelected(SelectionEvent e) {
FindWindow fw = new FindWindow(shell,SWT.DIALOG_TRIM);
fw.open();
}
});
getText().addKeyListener(new KeyAdapter() {
/**
* 设置键盘监听事件
* 有键盘键入则需要把相关按钮设置成可见:撤销,查找,查找下一处
*/
public void keyReleased(KeyEvent e) {
if(getText().getText()!=""){//说明有输入
find.setEnabled(true);
find_next.setEnabled(true);
replace.setEnabled(true);
}else{
find.setEnabled(false);
find_next.setEnabled(false);
replace.setEnabled(true);
}
}
});
setFont.addSelectionListener(new SelectionAdapter() {
/**
* 设置字体的监听事件
*/
public void widgetSelected(SelectionEvent e) {
FontDialog fontDialog = new FontDialog(shell, SWT.NONE);
Font oldFont = getText().getFont();
if(oldFont != null) {
fontDialog.setFontList(oldFont.getFontData());
}
FontData fontData = fontDialog.open();
if(fontData == null) {
return;
}
final Display display = Display.getDefault();
Font newFont = new Font(display, fontData);
getText().setFont(newFont);
if(oldFont != null) {
oldFont.dispose();
}
}
});
Main_Window(String titleName){
/**
* 有参构造传入的参数为文件名
*/
this.titleName = titleName;
}
find_next.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
pareStr = Util.shell.getText_1(); //获取主窗口文本框内容 true
index = pareStr.indexOf(textContent.getText(),index); //获取查找串在母串中的从index处开始的位置
if(index == -1){
JOptionPane.showMessageDialog(null, "找不到\""+textContent.getText()+"\"","记事本", JOptionPane.ERROR_MESSAGE);
}
int end = index+textContent.getText().length();//获得渲染的结束位置
Util.shell.selectContent(index, end);
index = index+textContent.getText().length();
}
});
replaceBtn.addSelectionListener(new SelectionAdapter() {
/**
* 替换按钮事件
*/
public void widgetSelected(SelectionEvent e) {
pareStr = Util.shell.getText_1(); //获取主窗口文本框内容
pareStr = pareStr.replaceFirst(textContent.getText(), replaceText.getText());
Util.shell.showText(pareStr);
}
});
queding.addSelectionListener(new SelectionAdapter() {
/**
* 确定跳转到某行
*/
public void widgetSelected(SelectionEvent e) {
int n = Integer.parseInt(goto_line.getText());
goto_line.setText(""+ n + "");
Util.shell.gotoOneLine(1);
}
});
public void gotoOneLine(int row)
{
getText().setSelection(row);
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有