import java.awt.*;(本文来源于图老师网站,更多请访问http://m.tulaoshi.com/bianchengyuyan/)
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import java.lang.*;
import java.text.*;
import java.io.*;
import java.util.*;
import javax.accessibility.*;
import java.awt.datatransfer.*;
// 课设 记事本
/**********************************************************/
public class Notepad extends JFrame implements ActionListener,MouseListener
{
JPanel contentPane;
JMenuBar jMenuBar1 = new JMenuBar();
//工具条
JToolBar toolBar = new JToolBar();
JButton button1=new JButton(new ImageIcon("new.gif"));
JButton button2=new JButton(new ImageIcon("open.gif"));
JButton button3=new JButton(new ImageIcon("save.gif"));
//文件菜单
JMenu jMenuFile = new JMenu();
JMenuItem jMenuFileNew = new JMenuItem();
JMenuItem jMenuFileOpen = new JMenuItem();
JMenuItem jMenuFileSave = new JMenuItem();
JMenuItem jMenuFileSaveTo = new JMenuItem();
JMenuItem jMenuFileExit = new JMenuItem();
//帮助菜单
JMenu jMenuHelp = new JMenu();
JMenuItem jMenuHelpAbout = new JMenuItem();
BorderLayout borderLayout1 = new BorderLayout();
//编辑菜单
JMenu jMenuFileEdit = new JMenu();
JMenuItem jMenuFileCut = new JMenuItem();
JMenuItem jMenuFileCopy = new JMenuItem();
JMenuItem jMenuFilePaste = new JMenuItem();
JMenuItem jMenuFileDel = new JMenuItem();
//右键菜单
JMenuItem jMenuMouseCut = new JMenuItem("剪切");
JMenuItem jMenuMouseCopy = new JMenuItem("复制");
JMenuItem jMenuMousePaste = new JMenuItem("粘贴");
JMenuItem jMenuMouseDel = new JMenuItem("删除");
//格式菜单
JMenu jMenuFileForm = new JMenu();
JMenuItem jMenuFileFindNext = new JMenuItem();
JMenuItem jMenuFileSelect=new JMenuItem("全选");
JMenuItem jMenuFileTime = new JMenuItem("时间/日期(D)");
// JMenuItem jMenuFileLine = new JMenuItem("自动换行");
JMenuItem jMenuFileFont = new JMenuItem();
JMenuItem jcolor=new JMenuItem("颜色...");
JCheckBox jMenuCheckBox = new JCheckBox("自动换行",true);
JTextArea jTextArea1 = new JTextArea(15,15);
JLabel statubar=new JLabel("状态栏");
JPopupMenu pm=new JPopupMenu();
Calendar cal=Calendar.getInstance();
// 变量声明
String filename;
String filepath;
String strtext="";
boolean bsaved = false;
JFileChooser jFileChooser = new JFileChooser();
JColorChooser jColor=new JColorChooser();
FileDialog filedialog_save;
BufferedWriter out;
FileWriter tofile;
Clipboard cb=Toolkit.getDefaultToolkit().getSystemClipboard();
int n;
int i;
int t;
boolean check=true;
FileDialog dog;
/**********************************************************/
//组件初始化
public Notepad(){
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(borderLayout1);
this.setBounds(110,100,500, 450);
this.setFont(new Font("宋体", Font.PLAIN, 8));
this.setTitle("记事本");
jTextArea1.setDoubleBuffered(false);
jTextArea1.setToolTipText("记事本");
jTextArea1.setVerifyInputWhenFocusTarget(true);
jTextArea1.setText("");
contentPane.add(statubar,borderLayout1.SOUTH);
toolBar.add(button1);
toolBar.add(button2);
toolBar.add(button3);
jMenuFile.setText("文件");
jMenuFileExit.setText("退出");
jMenuHelp.setText("帮助");
jMenuHelpAbout.setText("关于");
jMenuFileNew.setText("新建(N)");
jMenuFileNew.setMnemonic(KeyEvent.VK_N);
KeyStroke keynew=KeyStroke.getKeyStroke(KeyEvent.VK_N,Event.CTRL_MASK);
jMenuFileNew.setAccelerator(keynew);
jMenuFileOpen.setText("打开(O)...");
jMenuFileOpen.setMnemonic(KeyEvent.VK_O);
KeyStroke keyopen=KeyStroke.getKeyStroke(KeyEvent.VK_O,Event.CTRL_MASK);
jMenuFileOpen.setAccelerator(keyopen);
jMenuFileSave.setText("保存(S)");
jMenuFileSave.setMnemonic(KeyEvent.VK_S);
KeyStroke keysave=KeyStroke.getKeyStroke(KeyEvent.VK_S,Event.CTRL_MASK);
jMenuFileSave.setAccelerator(keysave);
jMenuFileSaveTo.setText("另存为(A)");
jMenuFileEdit.setText("编辑");
jMenuFileCut.setText("剪切(T)");
jMenuFileCut.setMnemonic(KeyEvent.VK_X);
KeyStroke keycut=KeyStroke.getKeyStroke(KeyEvent.VK_O,Event.CTRL_MASK);
jMenuFileCut.setAccelerator(keycut);
jMenuFileCopy.setText("复制(C)");
jMenuFileCopy.setMnemonic(KeyEvent.VK_C);
KeyStroke keycopy=KeyStroke.getKeyStroke(KeyEvent.VK_C,Event.CTRL_MASK);
jMenuFileCopy.setAccelerator(keycopy);
jMenuFilePaste.setText("粘贴(P)");
jMenuFilePaste.setMnemonic(KeyEvent.VK_V);
KeyStroke keypaste=KeyStroke.getKeyStroke(KeyEvent.VK_V,Event.CTRL_MASK);
jMenuFilePaste.setAccelerator(keypaste);
jMenuFileDel.setText("删除(L)");
jMenuFileFindNext.setText("查找");
jMenuFileFindNext.setMnemonic(KeyEvent.VK_F);
KeyStroke keyfind=KeyStroke.getKeyStroke(KeyEvent.VK_F,Event.CTRL_MASK);
jMenuFileFindNext.setAccelerator(keyfind);
jMenuFileForm.setText("格式");
jMenuFileFont.setText("字体");
// jMenuFileLine.setText("自动换行");
// jMenuCheckBox.setText("自动换行");
jMenuBar1.add(jMenuFile);
jMenuBar1.add(jMenuFileEdit);
jMenuBar1.add(jMenuFileForm);
jMenuBar1.add(jMenuHelp);
jMenuFile.add(jMenuFileNew);
jMenuFile.add(jMenuFileOpen);
jMenuFile.add(jMenuFileSave);
jMenuFile.add(jMenuFileSaveTo);
jMenuFile.addSeparator();
jMenuFile.add(jMenuFileExit);
jMenuFileEdit.add(jMenuFileCut);
jMenuFileEdit.add(jMenuFileCopy);
jMenuFileEdit.add(jMenuFilePaste);
jMenuFileEdit.add(jMenuFileDel);
jMenuFileEdit.addSeparator();
// jMenuFileEdit.add(jMenuFileFind);
jMenuFileEdit.add(jMenuFileFindNext);
jMenuFileEdit.add(jMenuFileSelect);
jMenuFileEdit.add(jMenuFileTime);
// jMenuFileForm.add(jMenuFileLine);
jMenuFileForm.add(jMenuFileFont);
jMenuFileForm.add(jcolor);
jMenuFileForm.add(jMenuCheckBox);
jMenuHelp.add(jMenuHelpAbout);
contentPane.add(jTextArea1, BorderLayout.CENTER);
//鼠标菜单
jTextArea1.add(pm);
pm.add(jMenuMouseCut);
pm.add(jMenuMouseCopy);
pm.add(jMenuMousePaste);
pm.add(jMenuMouseDel);
//滚动条控件
JScrollPane scroller = new JScrollPane();
JViewport port = scroller.getViewport();
contentPane.add("Center", scroller);
contentPane.add("North",toolBar);
port.add(jTextArea1);
this.setJMenuBar(jMenuBar1);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//添加菜单中的监听器
jTextArea1.addMouseListener(this);
File_new_actionAdapter fnew=new File_new_actionAdapter();
jMenuFileNew.addActionListener(fnew);
button1.addActionListener(fnew);
File_open_actionAdapter fopen=new File_open_actionAdapter();
jMenuFileOpen.addActionListener(fopen);
button2.addActionListener(fopen);
File_save_actionAdapter fsave=new File_save_actionAdapter();
jMenuFileSave.addActionListener(fsave);
button3.addActionListener(fsave);
File_saveto_actionAdapter fsaveto=new File_saveto_actionAdapter();
jMenuFileSaveTo.addActionListener(fsaveto);
File_close_actionAdapter fclose=new File_close_actionAdapter();
jMenuFileExit.addActionListener(fclose);
jMenuFileCut.addActionListener(this);
jMenuFileCopy.addActionListener(this);
jMenuFilePaste.addActionListener(this);
jMenuFileDel.addActionListener(this);
//鼠标事件
jMenuMouseCut.addActionListener(this);
jMenuMouseCopy.addActionListener(this);
jMenuMousePaste.addActionListener(this);
jMenuMouseDel.addActionListener(this);
jMenuFileFindNext.addActionListener(this);
jMenuFileSelect.addActionListener(this);
jMenuFileTime.addActionListener(this);
jMenuFileFont.addActionListener(this);
// jMenuFileLine.addActionListener(this);
jMenuCheckBox.addActionListener(this);
jcolor.addActionListener(this);
jMenuHelpAbout.addActionListener(this);
show();
validate();
}