?? texteditor.java
字號(hào):
import java.lang.String;
import java.awt.*;
import java.awt.event.*;
import java.awt.print.*; //打印
import javax.swing.*;
import java.lang.String.*;
import java.awt.datatransfer.*;//數(shù)據(jù)傳送
import javax.swing.event.*;
import java.util.*; //日期
import java.io.*;
import java.applet.*;
public class TextEditor extends Frame implements ActionListener,ItemListener
{
char buffer[]=new char[1024];
String sou="";
String des="";
long souTime,desTime;
MenuBar mb=new MenuBar();
Menu file=new Menu("文件(F)");
Menu compilation=new Menu("編輯(E)");
Menu form=new Menu("格式(O)");
Menu examine=new Menu("查看(V)");
Menu help=new Menu("幫助(H)");
TextArea msg=new TextArea("please input the text from here",40,100);
PopupMenu pm=new PopupMenu();
Clipboard clipboard;//定義剪切板對(duì)象
FileDialog filedialog_save,filedialog_load;//定義文件對(duì)話框
JFileChooser openDialog=new JFileChooser("F:\\");
PrinterJob prt=PrinterJob.getPrinterJob();
JLabel labelInfo=new JLabel("狀態(tài)欄");
MenuItem New=new MenuItem("新建(N) Ctrl+N");
MenuItem open=new MenuItem("打開(O) Ctrl+O");
MenuItem Save=new MenuItem("保存(S) Ctrl+S");
MenuItem As=new MenuItem("另存為(A)...");
MenuItem Set=new MenuItem("頁面設(shè)置(U)...");
MenuItem Print=new MenuItem("打印(P)...Ctrl+P");
Menu Run=new Menu("運(yùn)行");
MenuItem exit=new MenuItem("退出");
MenuItem attribute=new MenuItem("文檔屬性(I)", new MenuShortcut('I'));
MenuItem deskShortCut=new MenuItem("創(chuàng)建桌面快捷方式(D)", new MenuShortcut('D'));
CheckboxMenuItem checkbox=new CheckboxMenuItem("只讀模式(R)",false);
MenuItem Copy=new MenuItem("復(fù)制 Ctrl+C");
MenuItem Cut=new MenuItem("剪切 Ctrl+X");
MenuItem Paste=new MenuItem("粘貼 Ctrl+V");
MenuItem Cancel=new MenuItem("撤銷(U) Ctrl+Z");
MenuItem Delete=new MenuItem("刪除 (L) Del");
MenuItem Find=new MenuItem("查找(F)... Ctrl+F");
MenuItem FindNext=new MenuItem("查找下一個(gè)(N) F3");
MenuItem Replace=new MenuItem("替換(R) Ctrl+H");
MenuItem Transfer=new MenuItem("轉(zhuǎn)到(G) Ctrl+G");
MenuItem SelectAll=new MenuItem("全選(A) Ctrl+A");
MenuItem date=new MenuItem("時(shí)間/日期(D) F5");
CheckboxMenuItem Autolinewrap=new CheckboxMenuItem("自動(dòng)換行(W)",true);
MenuItem font =new MenuItem("字體(F)...");
MenuItem HelpTheme=new MenuItem("幫助主題(H)...");
MenuItem Introduction=new MenuItem("關(guān)于記事本(A)");
MenuItem StateItem =new MenuItem("狀態(tài)欄(S)");
TextEditor()
{
setTitle("新建 文本文檔 - 記事本");
setSize(500,550);
add(msg);
add("South",labelInfo);
mb.add(file);
mb.add(compilation);
mb.add(form);
mb.add(examine);
mb.add(help);
clipboard = null;
clipboard = getToolkit().getSystemClipboard();//獲取系統(tǒng)剪切板
filedialog_save=new FileDialog(this,"保存文件對(duì)話框",FileDialog.SAVE);
filedialog_save.setVisible(false);
filedialog_load=new FileDialog(this,"打開文件對(duì)話框",FileDialog.LOAD);
filedialog_load.setVisible(false);
file.add(New);
file.add(open);
file.add(Save);
file.add(As);
file.add(Set);
file.add(Print);
file.addSeparator();
file.add(Run);
file.add(attribute);
file.add(deskShortCut);
file.add(checkbox);
file.addSeparator();
file.add(exit);
// 為二級(jí)菜單加入菜單項(xiàng)
Run.add("新窗口(N) Alt+N");
Run.add("空白窗口(E) Alt+O");
Run.add("使用默認(rèn)程序打開(D) Ctrl+L");
Run.add("使用外部程序打開(O) Alt+L");
Run.add("命令(C) Ctrl+R");
compilation.add(Cancel);
compilation.addSeparator();
compilation.add(Cut);
compilation.add(Copy);
compilation.add(Paste);
compilation.add(Delete);
compilation.addSeparator();
compilation.add(Find);
compilation.add(FindNext);
compilation.add(Replace);
compilation.add(Transfer);
compilation.addSeparator();
compilation.add(SelectAll);
compilation.add(date);
form.add(Autolinewrap);
form.add(font);
examine.add(StateItem);
help.add(Introduction);
help.add(HelpTheme);
//注冊(cè)事件監(jiān)聽器
Copy.addActionListener(this);
Cut.addActionListener(this);
Paste.addActionListener(this);
New.addActionListener(this);
open.addActionListener(this);
Save.addActionListener(this);
As.addActionListener(this);
Set.addActionListener(this);
Print.addActionListener(this);
Run.addActionListener(this);
attribute.addActionListener(this);
deskShortCut.addActionListener(this);
checkbox.addActionListener(this);
Cancel.addActionListener(this);
Delete.addActionListener(this);
Find.addActionListener(this);
FindNext.addActionListener(this);
Replace.addActionListener(this);
Transfer.addActionListener(this);
SelectAll.addActionListener(this);
date.addActionListener(this);
Autolinewrap.addActionListener(this);
font.addActionListener(this);
HelpTheme.addActionListener(this);
Introduction.addActionListener(this);
StateItem.addActionListener(this);
attribute.addActionListener(this);
checkbox.addItemListener(this);
exit.addActionListener(this);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}});
setMenuBar(mb); // 將菜單欄設(shè)置為指定的菜單欄
show(); // 顯示窗口
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==exit)
System.exit(0);
if (e.getSource()==Save||e.getSource()==As)
{
int len=openDialog.showSaveDialog(this);
if(len==JFileChooser.APPROVE_OPTION)
{
File resaveFile=openDialog.getSelectedFile();
des=msg.getText();
save(resaveFile);
sou=des;
}
}
if(e.getSource()==New)
msg.setText(null);
if(e.getSource()==open)
{
int len=openDialog.showOpenDialog(this);
File openFile=openDialog.getSelectedFile();
if(len==JFileChooser.APPROVE_OPTION)
{
open(openFile);
souTime=openFile.lastModified();
}
}
if(e.getSource()==Set)
Set();
if(e.getSource()==Print)
prt.printDialog();
if(e.getSource()==attribute)
attribute();
if(e.getSource()==deskShortCut)
deskShortCut();
if(e.getSource()==Cancel)
Cancel();
if(e.getSource()==Cut)
Cut();
if(e.getSource()==Copy)
Copy();
if(e.getSource()==Paste)
Paste();
if(e.getSource()==Delete)
Delete();
if(e.getSource()==Find)
Find();
if(e.getSource()==FindNext)
FindNext();
if(e.getSource()==Replace)
Replace();
if(e.getSource()==SelectAll)
msg.selectAll();
if(e.getSource()==date)
date();
if(e.getSource()==Autolinewrap)
Autolinewrap();
if(e.getSource()==font)
font();
if(e.getSource()==HelpTheme)
HelpTheme();
if(e.getSource()==Introduction)
JOptionPane.showMessageDialog(null,"彭成的記事本2008年11月");
labelInfo.setText("返回原程序");
}
public void itemStateChanged(ItemEvent e)
{
if (e.getSource()==Autolinewrap)
// msg.setLineWrap(true);
if (e.getSource()==checkbox)
if (checkbox.getState())
msg.setText(checkbox.getLabel()+"被選中");
else
msg.setText(checkbox.getLabel()+"被取消");
}
//保存文件方法
public void save(File file){
try
{
FileWriter out=new FileWriter(file);
buffer=des.toCharArray();
out.write(buffer);
out.close();
desTime=file.lastModified();
if(desTime!=souTime&&file.exists()) {
labelInfo.setText(file.getPath()+"保存成功");
souTime=desTime;}
}
catch(Exception e){}
}
//打開文件方法
public void open(File file)
{
try
{
FileReader in=new FileReader(file);
int len=in.read(buffer);
msg.setText(new String(buffer,0,len));
sou=new String(buffer,0,len);
labelInfo.setText("打開"+file.getPath());
in.close();
}
catch(Exception e){}
}
//全選方法
public void SelectAll()
{
msg.selectAll();
}
//剪切方法
public void Cut()
{
String s1 = msg.getSelectedText();//剪切到剪切板
StringSelection text1 = new StringSelection(s1);//拖動(dòng)鼠標(biāo)選取文本
clipboard.setContents(text1, null);
int j = msg.getSelectionStart();
int k = msg.getSelectionEnd();
msg.replaceRange("", j, k);//從Text中刪除被選取文本
}
//復(fù)制方法
public void Copy()
{
String s = msg.getSelectedText();//復(fù)制到剪切板
StringSelection text1= new StringSelection(s);//拖動(dòng)鼠標(biāo)選取文本
clipboard.setContents(text1, null);
}
//粘貼方法
public void Paste()
{
Transferable transferable = clipboard.getContents(this);
DataFlavor dataflavor = DataFlavor.stringFlavor;
if(transferable.isDataFlavorSupported(dataflavor))
{
try//處理異常事件的發(fā)生(類型不符合)
{
String s3 = (String)transferable.getTransferData(dataflavor);
msg.insert(s3, msg.getCaretPosition());
}
catch(Exception e1) { }
}
}
//使用系統(tǒng)日期
public void date()
{
Date d=new Date();
msg.append(d.toString());
}
public void Delete()
{
msg.replaceRange("",msg.getSelectionStart(),msg.getSelectionEnd());
}
//下面是暫時(shí)未實(shí)現(xiàn)的方法
public void Find()
{
msg.setText("暫未實(shí)現(xiàn)的功能");
}
public void FindNext()
{
msg.setText("暫未實(shí)現(xiàn)的功能");
}
public void HelpTheme()
{
msg.setText("暫未實(shí)現(xiàn)的功能");
}
public void Replace()
{
msg.setText("暫未實(shí)現(xiàn)的功能");
}
public void Autolinewrap()
{
msg.setText("暫未實(shí)現(xiàn)的功能");
}
public void font()
{
msg.setText("暫未實(shí)現(xiàn)的功能");
}
public void Introduction()
{
msg.setText("暫未實(shí)現(xiàn)的功能");
}
public void Set()
{
msg.setText("暫未實(shí)現(xiàn)的功能");
}
public void attribute()
{
msg.setText("暫未實(shí)現(xiàn)的功能");
}
public void deskShortCut()
{
msg.setText("暫未實(shí)現(xiàn)的功能");
}
public void Cancel()
{
msg.setText("暫未實(shí)現(xiàn)的功能");
}
//程序入口
public static void main(String arg[])
{
new TextEditor();
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -