import java.awt.*;
import java.awt.event.*;
import java.lang.String.*;
import java.awt.datatransfer.*; //資料傳送
import java.util.*;
public class Notepad {
public static void main(String args[]) {
MyMenuFrame mf = new MyMenuFrame(); // 定義主類
mf.setSize(new Dimension(300, 200)); // 用setSize()方法指定視窗的初始大小
mf.setVisible(true);
}
}
// 定義視窗
class MyMenuFrame extends Frame implements ActionListener {
Clipboard clipboard; // 定義剪下板對象
FileDialog filedialog_save, filedialog_load; // 定義檔案對話方塊
MenuBar m_MenuBar; // 定義菜單條
Menu menuFile, menuEdit, menuStyle, menuSearch, menuHelp; // 定義功能表項目
MenuItem mi_File_New, mi_File_Open, mi_File_Save, mi_file_Savaas,
mi_File_Close, mi_File_Exit, mi_Edit_Ce, mi_Edit_Copy, mi_Edit_Cut,
mi_Edit_Paste, mi_Edit_Style, mi_Style_Font, mi_Style_Auto,
mi_Search_sphere, mi_Help_Helptopic, mi_Help_About;
TextArea text;
MyMenuFrame() // 構造方法
{
super("記事本"); // 指定視窗標題
text = new TextArea(20, 20);
add(text);
clipboard = null;
clipboard = getToolkit().getSystemClipboard(); // 擷取系統剪下板
filedialog_save = new FileDialog(this, "儲存檔案對話方塊", FileDialog.SAVE);
filedialog_save.setVisible(false);
filedialog_load = new FileDialog(this, "開啟檔案對話方塊", FileDialog.LOAD);
filedialog_load.setVisible(false);
m_MenuBar = new MenuBar();
menuFile = new Menu("檔案");// 建立功能表項目,建立菜單子項並初始化
MenuItem mi_File_New = new MenuItem("建立");
MenuItem mi_File_Open = new MenuItem("開啟");
MenuItem mi_File_Save = new MenuItem("儲存");
MenuItem mi_File_Saveas = new MenuItem("另存新檔");
MenuItem mi_File_Close = new MenuItem("關閉");
MenuItem mi_File_Exit = new MenuItem(" 退出");
// mi_File_Exit.setShortcut(new MenuShortcut(´x´));//設定快速鍵
mi_File_Open.setActionCommand("開啟");// 簡化
mi_File_New.setActionCommand("建立");
mi_File_Exit.setActionCommand("退出");
mi_File_New.addActionListener(this);// 使菜單子項響應動作事件
mi_File_Open.addActionListener(this);
mi_File_Save.addActionListener(this);
mi_File_Saveas.addActionListener(this);
mi_File_Close.addActionListener(this);
mi_File_Exit.addActionListener(this);
menuFile.add(mi_File_New);// 把菜單子項加入功能表項目
menuFile.add(mi_File_Open);
menuFile.add(mi_File_Save);
menuFile.add(mi_File_Saveas);
menuFile.add(mi_File_Close);
menuFile.addSeparator();// 加一條橫向分割線
menuFile.add(mi_File_Exit);
m_MenuBar.add(menuFile);// 把功能表項目加入菜單條
menuEdit = new Menu("編輯");
mi_Edit_Ce = new MenuItem("撤消");
mi_Edit_Copy = new MenuItem("複製");
mi_Edit_Cut = new MenuItem("剪下");
mi_Edit_Paste = new MenuItem("粘貼");
mi_Edit_Copy.setActionCommand("複製");
mi_Edit_Cut.setActionCommand("剪下");
mi_Edit_Paste.setActionCommand("粘貼");
mi_Edit_Ce.addActionListener(this);
mi_Edit_Copy.addActionListener(this);
mi_Edit_Cut.addActionListener(this);
mi_Edit_Paste.addActionListener(this);
menuEdit.add(mi_Edit_Ce);
menuEdit.add(mi_Edit_Copy);
menuEdit.add(mi_Edit_Cut);
menuEdit.add(mi_Edit_Paste);
m_MenuBar.add(menuEdit);
menuStyle = new Menu("格式");
mi_Style_Auto = new MenuItem("自動換行");
mi_Style_Font = new MenuItem("字型");
mi_Style_Auto.addActionListener(this);
mi_Style_Font.addActionListener(this);
menuStyle.add(mi_Style_Auto);
menuStyle.add(mi_Style_Font);
m_MenuBar.add(menuStyle);
menuSearch = new Menu("查看");
mi_Search_sphere = new MenuItem("狀態列");
mi_Search_sphere.addActionListener(this);
menuSearch.add(mi_Search_sphere);
m_MenuBar.add(menuSearch);
menuHelp = new Menu("協助");
mi_Help_Helptopic = new MenuItem("說明主題");
mi_Help_About = new MenuItem("關於記事本");
mi_Help_Helptopic.addActionListener(this);
mi_Help_About.addActionListener(this);
menuHelp.add(mi_Help_Helptopic);
menuHelp.add(mi_Help_About);
m_MenuBar.add(menuHelp);
setMenuBar(m_MenuBar);// 把菜單條加入到菜單
addWindowListener(new WindowAdapter()// 監聽視窗(關閉視窗)
{
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
setVisible(true);
validate(); // 如果載入了其他東西,就可用它來顯示組件
}
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand() == "建立") {
text.setText(null);// 清空
}
if (e.getActionCommand() == "開啟") {
filedialog_load.setVisible(true);// 使檔案對話方塊可見
}
if (e.getActionCommand() == "儲存") {
filedialog_save.setVisible(true);// 使檔案對話方塊可見
}
if (e.getActionCommand() == "複製") {
String s = text.getSelectedText();// 複製到剪下板
StringSelection text1 = new StringSelection(s);// 拖動滑鼠選取文本
clipboard.setContents(text1, null);
}
if (e.getActionCommand() == "剪下") {
String s1 = text.getSelectedText();// 剪下到剪下板
StringSelection text1 = new StringSelection(s1);// 拖動滑鼠選取文本
clipboard.setContents(text1, null);
int j = text.getSelectionStart();
int k = text.getSelectionEnd();
text.replaceRange("", j, k);// 從Text中刪除被選取文本
}
if (e.getActionCommand() == "粘貼")// 從剪下板粘貼資料
{
Transferable transferable = clipboard.getContents(this);
DataFlavor dataflavor = DataFlavor.stringFlavor;
if (transferable.isDataFlavorSupported(dataflavor)) {
try// 處理例外狀況事件的發生(類型不符合)
{
String s3 = (String) transferable.getTransferData(dataflavor);
text.insert(s3, text.getCaretPosition());
} catch (Exception e1) {
}
}
}
if (e.getActionCommand() == "退出") {
dispose();
System.exit(0);
}
}
}