import java.awt.*;import javax.swing.*;import java.awt.event.*;import ng.String;import java.io.*;public class JA V AEdit extends JFrame {JTextArea jta;private JFrame frame;JPopupMenu pop = new JPopupMenu();//定义快捷菜单用private JComboBox combox, combox1;//组合框,获取系统字体使用private JToolBar toolbar;//定义工具栏private JComboBox sizes, sizes1; // 字号组合框private JDialog dialog; //定义查找时所用窗体private JTextField textField;//输入时的文本框private FileDialog filedialog;private JOptionPane optionpane;//关于时输出对话框boolean flag=false;Font font = new Font("楷书", Font.BOLD, 16); // 储存字体String filename= "",name; //文件名// ***************************************构造函数public JA V AEdit() {frame = new JFrame("JA V A代码编译器- [无标题] ");jta = new JTextArea("", 100, 30); // 构建一个开始为空,默认100行,30列JScrollPane jsp = new JScrollPane(jta);JMenuBar jmb = new JMenuBar();JMenu mFile = new JMenu("文件");JMenu mEdit = new JMenu("编辑");JMenu mForm = new JMenu("格式");JMenu mFind = new JMenu("查找");JMenu mHelp=new JMenu("帮助");//******************************************************************************* *工具栏GraphicsEnvironment g = GraphicsEnvironment.getLocalGraphicsEnvironment();String[] fontname = g.getAvailableFontFamilyNames(); // 获取系统字体combox = new JComboBox(fontname); // 组合框获取系统字体toolbar = new JToolBar();toolbar.setFloatable(true); // 设置窗口的浮动String size[] = { "20", "30", "40", "50", "60", "70" };sizes = new JComboBox(size); // 字号组合框sizes.setEditable(true); // 设置组合框可编辑JButton news = new JButton(new ImageIcon("images/new.gif"));news.setToolTipText("新建");JButton open = new JButton(new ImageIcon("images/open.gif"));open.setToolTipText("打开");JButton save = new JButton(new ImageIcon("images/save.gif"));save.setToolTipText("保存");JButton cut = new JButton(new ImageIcon("images/cut.gif"));cut.setToolTipText("剪切");JButton copy = new JButton(new ImageIcon("images/copy.gif"));copy.setToolTipText("复制");JButton paste = new JButton(new ImageIcon("images/paste.gif"));paste.setToolTipText("粘贴");toolbar.add(news);toolbar.add(open);toolbar.add(save);toolbar.add(cut);toolbar.add(copy);toolbar.add(paste);toolbar.add(combox);toolbar.add(sizes);frame.getContentPane().add(toolbar, BorderLayout.NORTH);news.addActionListener(new newl());open.addActionListener(new openl());save.addActionListener(new savel());cut.addActionListener(new cutl());copy.addActionListener(new copyl());paste.addActionListener(new pastel());sizes.addActionListener(new listener());combox.addActionListener(new listener());//*****************************************************************************鼠标快捷键String pop1[] = { "剪切", "复制", "粘贴", "全选" };JMenuItem pops[] = new JMenuItem[pop1.length];for (int j = 0; j < pops.length; j++) {pops[j] = new JMenuItem(pop1[j]);pop.add(pops[j]);if(j<3){pop.addSeparator();}}pop.setPopupSize(120, 150); // 设置快捷菜单的大小jta.addMouseListener(new pops1());pops[0].addActionListener(new cutl());pops[1].addActionListener(new copyl());pops[2].addActionListener(new pastel());pops[3].addActionListener(new selectl());// **************************************************文件菜单的设定JMenuItem mNew = new JMenuItem("新建", KeyEvent.VK_N);mNew.addActionListener(new newl());mFile.add(mNew);JMenuItem mOpen = new JMenuItem("打开", KeyEvent.VK_O);mOpen.addActionListener(new openl());mFile.add(mOpen);JMenuItem mSave = new JMenuItem("保存");mSave.addActionListener(new savel());mFile.add(mSave);mFile.addSeparator(); // 添加分割线JMenuItem mExit = new JMenuItem("退出");mExit.addActionListener(new exitl());mFile.add(mExit);mFile.setMnemonic(KeyEvent.VK_F);// ***********************************************编辑菜单的子菜单的处理JMenuItem jmi;jmi = new JMenuItem("复制");jmi.addActionListener(new copyl());mEdit.add(jmi);jmi = new JMenuItem("剪切");jmi.addActionListener(new cutl());mEdit.add(jmi);jmi = new JMenuItem("粘贴");jmi.addActionListener(new pastel());mEdit.add(jmi);mEdit.addSeparator(); // 添加分割线jmi = new JMenuItem("全选");jmi.addActionListener(new selectl());mEdit.add(jmi);// *******************************************处理格式菜单JMenuItem jmf;jmf = new JMenuItem("字体");jmf.addActionListener(new word1());mForm.add(jmf);// *******************************************查找菜单JMenuItem mfind=new JMenuItem("查找");mfind.addActionListener(new find1());mFind.add(mfind);// ******************************************关于菜单JMenuItem mHelps=new JMenuItem("关于");mHelps.addActionListener(new helpl());mHelp.add(mHelps);// **********************************************************jmb.add(mFile);jmb.add(mEdit);jmb.add(mForm);jmb.add(mFind);jmb.add(mHelp);jta.add(pop); //把右键快捷栏加入frame.setJMenuBar(jmb);frame.getContentPane().add(jsp);frame.setSize(850, 700);frame.setVisible(true);}// ****************************************************//各个事件的监听class newl implements ActionListener {public void actionPerformed(ActionEvent e) {jta.setText("");}}class openl implements ActionListener // 打开文件的监听{public void actionPerformed(ActionEvent e) {JFileChooser jf = new JFileChooser();int result=jf.showOpenDialog(null);if(result==JFileChooser.APPROVE_OPTION){File f=jf.getSelectedFile();char c[]=new char[(int)f.length()]; //lengthy()用来获得文件的长度try{BufferedReader in=new BufferedReader(new FileReader(f));//文件输入流从字符输入流中读取文本in.read(c);in.close();}catch(FileNotFoundException fn){System.out.println("没有找到文件");jf.setVisible(false);}catch(IOException io){jf.setVisible(false);}String str=new String(c);jta.setText(str);jta.setCaretPosition(0); //设置打开文件后光标的位置如果不设置则默认为文件底部filename=jf.getCurrentDirectory()+jf.getSelectedFile().getName(); //创建文件名if(!filename.equals("nullnull"))frame.setTitle("Java编辑器- ["+filename+"]"); //设置窗口名*/}}}class savel implements ActionListener //保存文件的监听{public void actionPerformed(ActionEvent e){if(filename.equals("")||filename.equals("nullnull"))//文件未保存过时{filedialog=new FileDialog(frame,"保存",FileDialog.SA VE);filedialog.setVisible(true);try{String str=jta.getText();File f=new File(filedialog.getDirectory()+filedialog.getFile());filename=filedialog.getDirectory()+filedialog.getFile(); //文件名BufferedWriter out=new BufferedWriter(new FileWriter(f)); //文件输出流out.write(str,0,str.length());out.close();}catch(FileNotFoundException fn){System.out.println("没有选择文件");filedialog.setVisible(false);}catch(IOException io){filedialog.setVisible(false);}}else{try {String str=jta.getText();File f=new File(filename);BufferedWriter out=new BufferedWriter(new FileWriter(f));out.write(str,0,str.length());out.close();}catch(FileNotFoundException fn){System.out.println("没有选择文件");filedialog.setVisible(false);}catch(IOException io){filedialog.setVisible(false);}}if(!filename.equals("nullnull"))frame.setTitle("Java编辑器- ["+filename+"]");flag=false;}}class exitl implements ActionListener // 退出记事本的监听{public void actionPerformed(ActionEvent e) {System.exit(0);}}class copyl implements ActionListener // 拷贝的监听{public void actionPerformed(ActionEvent e) {jta.copy();}}class pastel implements ActionListener // 粘贴的监听{public void actionPerformed(ActionEvent e) {jta.paste();}}class cutl implements ActionListener // 剪切的监听{public void actionPerformed(ActionEvent e) {jta.cut();}}class find1 implements ActionListener // 查找的监听{public void actionPerformed(ActionEvent e) {dialog = new JDialog(frame, "查找", false);dialog.setLayout(new BorderLayout());dialog.setLocation(550, 200);dialog.setSize(300, 130);dialog.setVisible(true);Label label = new Label("输入你要查找的内容");textField = new JTextField(8);Panel panel1 = new Panel();panel1.add(label);Panel panel2 = new Panel();panel2.add(textField);JButton a1, a2;a1 = new JButton("确定");a2 = new JButton("取消");Panel panel3 = new Panel();panel3.add(a1);panel3.add(a2);dialog.add(panel1, BorderLayout.NORTH);dialog.add(panel2, BorderLayout.CENTER);dialog.add(panel3, BorderLayout.SOUTH);a1.addActionListener(new a11());a2.addActionListener(new a21());}}class word1 implements ActionListener // 字体选项的监听{public void actionPerformed(ActionEvent e) {GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();String[] fontnames = ge.getAvailableFontFamilyNames(); // 获取系统字体combox1 = new JComboBox(fontnames); // 组合框获取系统字体String size1[] = { "10", "20", "30", "40", "50", "60" };sizes1 = new JComboBox(size1); // 字号组合框sizes1.setEditable(false); // 设置组合框可编辑dialog = new JDialog(frame, "字体", false); // 字体选择对话框dialog.setSize(300, 150);dialog.setLocation(550, 200);dialog.setVisible(true);dialog.setLayout(new BorderLayout());Panel panel1 = new Panel();panel1.add(combox1);panel1.add(sizes1);combox1.addActionListener(new listener());sizes1.addActionListener(new listener());dialog.add(panel1, BorderLayout.CENTER); // 将组合框添加到窗口Panel panel2 = new Panel();JButton b1, b2;b1 = new JButton("确定");b2 = new JButton("取消");panel2.add(b1);panel2.add(b2);dialog.add(panel2, BorderLayout.SOUTH); // 将面板添加到窗口b1.addActionListener(new b11());b2.addActionListener(new a21());}}class pops1 implements MouseListener // 快捷栏的监听{public void mouseClicked(MouseEvent m) {if (m.getModifiers() == MouseEvent.BUTTON3_MASK)// 当鼠标按下右键时// MouseEvent.BUTTON2_MASK// 表示左键pop.show(jta, m.getX(), m.getY()); // 在单击处显示快捷菜单}public void mousePressed(MouseEvent e) {}public void mouseExited(MouseEvent e) {}public void mouseReleased(MouseEvent e) {}public void mouseEntered(MouseEvent e) {}}class selectl implements ActionListener // 对全选进行监听{public void actionPerformed(ActionEvent e) {int end = jta.getText().length();jta.setSelectionStart(0);jta.setSelectionEnd(end);}}class listener implements ActionListener // 对字体和大小进行监听{public void actionPerformed(ActionEvent e) {int s = 16;String name = (String) combox.getSelectedItem();s = Integer.parseInt((String) sizes.getSelectedItem());jta.setFont(new Font(name, Font.BOLD, s));}}class a11 implements ActionListener // 查找选项中的确定监听{public void actionPerformed(ActionEvent e) {if (textField.getText().equals(""))dialog.setVisible(false);else {String search = jta.getText();int start = search.indexOf(textField.getText());int end = start + textField.getText().length();jta.select(start, end);dialog.setVisible(false);}}}class a21 implements ActionListener // 取消按钮的监听{public void actionPerformed(ActionEvent e) {dialog.setVisible(false);}}class b11 implements ActionListener // 字体设置中的确定监听{public void actionPerformed(ActionEvent e) {Font fonts = jta.getFont();if (fonts != font)jta.setFont(font);dialog.setVisible(false);}}class helpl implements ActionListener // 关于的监听{public void actionPerformed(ActionEvent e) {optionpane.showMessageDialog(frame,"版权属于我们1&2小组");}}// ***********************************************主函数,程序入口点public static void main(String s[]) {new JA V AEdit();}}。