开发工具:
文件大小: 67kb
下载次数: 0
上传时间: 2010-06-29
详细说明: //创建File菜单 menu =new JMenu("文件"); menu.setMnemonic(KeyEvent.VK_F); menubar.add(menu); menuItem =new JMenuItem("新建",KeyEvent.VK_N);//创建File子菜单 menuItem.addActionListener(al); menu.add(menuItem); menuItem =new JMenuItem("打开",KeyEvent.VK_O); menuItem.addActionListener(al); menu.add(menuItem); menuItem =new JMenuItem("保存",KeyEvent.VK_S); menuItem.addActionListener(al); menu.add(m enuItem); menuItem =new JMenuItem("另成为",KeyEvent.VK_A); menuItem.addActionListener(al); menu.add(menuItem); menu.add(new JSeparator()); menuItem =new JMenuItem("退出",KeyEvent.VK_E); menuItem.addActionListener(al); menu.add(menuItem); //创建Edit菜单 menu =new JMenu("编辑"); menu.setMnemonic(KeyEvent.VK_E); menubar.add(menu); menuItem =new JMenuItem("剪切");//创建Edit子菜单 menuItem.addActionListener(al); menu.add(menuItem); menuItem =new JMenuItem("复制",KeyEvent.VK_C); menuItem.addActionListener(al); menu.add(menuItem); menuItem =new JMenuItem("粘贴",KeyEvent.VK_P); menuItem.addActionListener(al); menu.add(menuItem); menu.add(new JSeparator()); menuItem =new JMenuItem("全部选择",KeyEvent.VK_S); menuItem.addActionListener(al); menu.add(menuItem); menu.add(new JSeparator()); menuItem =new JMenuItem("清屏",KeyEvent.VK_S); menuItem.addActionListener(al); menu.add(menuItem); //创建View菜单 menu =new JMenu("视图"); menu.setMnemonic(KeyEvent.VK_V); menubar.add(menu); radioMenuItem =new JRadioButtonMenuItem("自动换行",true);//创建View子菜单 radioMenuItem.addItemListener(il); menu.add(radioMenuItem); radioMenuItemrow =new JRadioButtonMenuItem("显示行数",false); radioMenuItemrow.addItemListener(il); menu.add(radioMenuItemrow); menu.add(new JSeparator()); submenu =new JMenu("字体");//创建字体子菜单 menu.add(submenu); ButtonGroup group =new ButtonGroup(); radioMenuItem =new JRadioButtonMenuItem("Bold"); radioMenuItem.addItemListener(il); submenu.add(radioMenuItem); group.add(radioMenuItem); radioMenuItem =new JRadioButtonMenuItem("Italic"); radioMenuItem.addItemListener(il); submenu.add(radioMenuItem); group.add(radioMenuItem); radioMenuItem =new JRadioButtonMenuItem("Plain"); radioMenuItem.addItemListener(il); submenu.add(radioMenuItem); group.add(radioMenuItem); radioMenuItem =new JRadioButtonMenuItem("Bold+Italic"); radioMenuItem.addItemListener(il); submenu.add(radioMenuItem); group.add(radioMenuItem); submenu =new JMenu("字体颜色");//创建字体颜色子菜单 menu.add(submenu); ButtonGroup group1 =new ButtonGroup(); radioMenuItem =new JRadioButtonMenuItem("Black"); radioMenuItem.addItemListener(il); submenu.add(radioMenuItem); group1.add(radioMenuItem); radioMenuItem =new JRadioButtonMenuItem("Green"); radioMenuItem.addItemListener(il); submenu.add(radioMenuItem); group1.add(radioMenuItem); radioMenuItem =new JRadioButtonMenuItem("Cyan"); radioMenuItem.addItemListener(il); submenu.add(radioMenuItem); group1.add(radioMenuItem); radioMenuItem =new JRadioButtonMenuItem("Red"); radioMenuItem.addItemListener(il); submenu.add(radioMenuItem); group1.add(radioMenuItem); radioMenuItem =new JRadioButtonMenuItem("LightGray"); radioMenuItem.addItemListener(il); submenu.add(radioMenuItem); group1.add(radioMenuItem); radioMenuItem =new JRadioButtonMenuItem("Orange"); radioMenuItem.addItemListener(il); submenu.add(radioMenuItem); group1.add(radioMenuItem); radioMenuItem =new JRadioButtonMenuItem("Pink"); radioMenuItem.addItemListener(il); submenu.add(radioMenuItem); group1.add(radioMenuItem); radioMenuItem =new JRadioButtonMenuItem("Yellow"); radioMenuItem.addItemListener(il); submenu.add(radioMenuItem); group1.add(radioMenuItem); submenu =new JMenu("背景颜色");//创建背景颜色子菜单 menu.add(submenu); ButtonGroup group2 =new ButtonGroup(); radioMenuItem =new JRadioButtonMenuItem("black"); radioMenuItem.addItemListener(il); submenu.add(radioMenuItem); group2.add(radioMenuItem); radioMenuItem =new JRadioButtonMenuItem("green"); radioMenuItem.addItemListener(il); submenu.add(radioMenuItem); group2.add(radioMenuItem); radioMenuItem =new JRadioButtonMenuItem("cyan"); radioMenuItem.addItemListener(il); submenu.add(radioMenuItem); group2.add(radioMenuItem); radioMenuItem =new JRadioButtonMenuItem("red"); radioMenuItem.addItemListener(il); submenu.add(radioMenuItem); group2.add(radioMenuItem); radioMenuItem =new JRadioButtonMenuItem("lightGray"); radioMenuItem.addItemListener(il); submenu.add(radioMenuItem); group2.add(radioMenuItem); radioMenuItem =new JRadioButtonMenuItem("orange"); radioMenuItem.addItemListener(il); submenu.add(radioMenuItem); group2.add(radioMenuItem); radioMenuItem =new JRadioButtonMenuItem("pink"); radioMenuItem.addItemListener(il); submenu.add(radioMenuItem); group2.add(radioMenuItem); radioMenuItem =new JRadioButtonMenuItem("yellow"); radioMenuItem.addItemListener(il); submenu.add(radioMenuItem); group2.add(radioMenuItem); //弹出菜单 popupmenu =new JPopupMenu(); menuItem =new JMenuItem("新建",KeyEvent.VK_N); menuItem.addActionListener(al); popupmenu.add(menuItem); popupmenu.add(new JSeparator()); menuItem =new JMenuItem("剪切",KeyEvent.VK_C); menuItem.addActionListener(al); popupmenu.add(menuItem); menuItem =new JMenuItem("复制"); menuItem.addActionListener(al); popupmenu.add(menuItem); menuItem =new JMenuItem("粘贴",KeyEvent.VK_P); menuItem.addActionListener(al); popupmenu.add(menuItem); popupmenu.add(new JSeparator()); menuItem =new JMenuItem("全部选择",KeyEvent.VK_S); menuItem.addActionListener(al); popupmenu.add(menuItem); //设置文本区域 ta =new JTextArea(500,50); font =new Font("ch",+Font.PLAIN,18); ta.setFont(font); ta.setSelectedTextColor(Color.red); ta.setLineWrap(true); //设置边框滚动条 JScrollPane scrollpanel =new JScrollPane(ta,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); frame.getContentPane().add(scrollpanel,"Center"); ta.addMouseListener(ml); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(800,600); frame.setVisible(true); } 李惠竹 实现了各记事本的基本功能 //Eidt事件处理 if(menu.getActionCommand().equals("剪切")){ ta.cut(); } else if(menu.getActionCommand().equals("复制")){ ta.copy(); } else if(menu.getActionCommand().equals("粘贴")){ ta.paste(); } else if(menu.getActionCommand().equals("选择全部")){ ta.selectAll(); } else if(menu.getActionCommand().equals("清屏")){ ta.setText(""); } } }; ItemListener il =new ItemListener(){ public void itemStateChanged(ItemEvent e){ int state =e.getStateChange(); JMenuItem menuItem =(JMenuItem)e.getSource(); //自动换行事件处理 if(menuItem.getActionCommand().equals("自动换行")){ if(state==ItemEvent.SELECTED){ ta.setLineWrap(true); } else { ta.setLineWrap(false); } } //显示行数事件处理 else if(menuItem.getActionCommand().equals("显示行数")){//显示行数 if(state==ItemEvent.SELECTED){ File file =new File("c:\\temp.txt"); int rows =1; try{ FileOutputStream fout =new FileOutputStream(file); OutputStreamWriter out =new OutputStreamWriter(fout); ta.write(out); out.close(); }catch(FileNotFoundException fe){} catch(EOFException fe){} catch(IOException fe){} try{ FileInputStream fin =new FileInputStream(file); InputStreamReader in =new InputStreamReader(fin); BufferedReader reader =new BufferedReader(in); String s=reader.readLine(); ta.setText(rows+" "+s); while((s=reader.readLine())!=null){ rows++; ta.append("\n"+rows+" "+s); } in.close(); }catch(FileNotFoundException fe){} catch(EOFException fe){} catch(IOException fe){} file.delete(); } else { File file =new File("c:\\temp.txt"); try{ FileOutputStream fout =new FileOutputStream(file); OutputStreamWriter out =new OutputStreamWriter(fout); ta.write(out); out.close(); }catch(FileNotFoundException fe){} catch(EOFException fe){} catch(IOException fe){} try{ FileInputStream fin =new FileInputStream(file); InputStreamReader in =new InputStreamReader(fin); BufferedReader reader =new BufferedReader(in); String s1=reader.readLine(); String s2=new String(s1.toCharArray(),6,s1.length()-6); ta.setText(s2); while((s1=reader.readLine())!=null){ s2=new String(s1.toCharArray(),6,s1.length()-6); ta.append("\n"+s2); } in.close(); }catch(FileNotFoundException fe){} catch(EOFException fe){} catch(IOException fe){} file.delete(); } } //字体事件处理 else if(menuItem.getActionCommand().equals("Bold")){ if(state==ItemEvent.SELECTED){ font =new Font("ch",Font.BOLD,18); ta.setFont(font); } else { font =new Font("ch",Font.PLAIN,18); ta.setFont(font); } } else if(menuItem.getActionCommand().equals("Italic")){ if(state==ItemEvent.SELECTED){ font =new Font("ch",Font.ITALIC,18); ta.setFont(font); } else { font =new Font("ch",Font.PLAIN,18); ta.setFont(font); } } else if(menuItem.getActionCommand().equals("Plain")){ if(state==ItemEvent.SELECTED){ font =new Font("ch",Font.PLAIN,18); ta.setFont(font); } else { font =new Font("ch",Font.PLAIN,18); ta.setFont(font); } } else if(menuItem.getActionCommand().equals("Bold+Italic")){ if(state==ItemEvent.SELECTED){ font =new Font("ch",Font.BOLD+Font.ITALIC,18); ta.setFont(font); } else { font =new Font("ch",Font.PLAIN,18); ta.setFont(font); } } //字体颜色事件处理 else if(menuItem.getActionCommand().equals("Black")){ if(state==ItemEvent.SELECTED){ ta.setForeground(Color.black); } } else if(menuItem.getActionCommand().equals("Green")){ if(state==ItemEvent.SELECTED){ ta.setForeground(Color.green); } } else if(menuItem.getActionCommand().equals("Cyan")){ if(state==ItemEvent.SELECTED){ ta.setForeground(Color.cyan); } } else if(menuItem.getActionCommand().equals("Red")){ if(state==ItemEvent.SELECTED){ ta.setForeground(Color.red); } } else if(menuItem.getActionCommand().equals("LightGray")){ if(state==ItemEvent.SELECTED){ ta.setForeground(Color.lightGray); } } else if(menuItem.getActionCommand().equals("Orange")){ if(state==ItemEvent.SELECTED){ ta.setForeground(Color.orange); } } else if(menuItem.getActionCommand().equals("Pink")){ if(state==ItemEvent.SELECTED){ ta.setForeground(Color.pink); } } else if(menuItem.getActionCommand().equals("Yellow")){ if(state==ItemEvent.SELECTED){ ta.setForeground(Color.yellow); } } //背景颜色事件处理 else if(menuItem.getActionCommand().equals("black")){ if(state==ItemEvent.SELECTED){ ta.setBackground(Color.black); } } else if(menuItem.getActionCommand().equals("green")){ if(state==ItemEvent.SELECTED){ ta.setBackground(Color.green); } } else if(menuItem.getActionCommand().equals("cyan")){ if(state==ItemEvent.SELECTED){ ta.setBackground(Color.cyan); } } else if(menuItem.getActionCommand().equals("red")){ if(state==ItemEvent.SELECTED){ ta.setBackground(Color.red); } } else if(menuItem.getActionCommand().equals("lightGray")){ if(state==ItemEvent.SELECTED){ ta.setBackground(Color.lightGray); } } else if(menuItem.getActionCommand().equals("orange")){ if(state==ItemEvent.SELECTED){ ta.setBackground(Color.orange); } } else if(menuItem.getActionCommand().equals("pink")){ if(state==ItemEvent.SELECTED){ ta.setBackground(Color.pink); } } else if(menuItem.getActionCommand().equals("yellow")){ if(state==ItemEvent.SELECTED){ ta.setBackground(Color.yellow); } } } }; ...展开收缩
(系统自动生成,下载前可以参看下载内容)
下载文件列表
相关说明
- 本站资源为会员上传分享交流与学习,如有侵犯您的权益,请联系我们删除.
- 本站是交换下载平台,提供交流渠道,下载内容来自于网络,除下载问题外,其它问题请自行百度。
- 本站已设置防盗链,请勿用迅雷、QQ旋风等多线程下载软件下载资源,下载后用WinRAR最新版进行解压.
- 如果您发现内容无法下载,请稍后再次尝试;或者到消费记录里找到下载记录反馈给我们.
- 下载后发现下载的内容跟说明不相乎,请到消费记录里找到下载记录反馈给我们,经确认后退回积分.
- 如下载前有疑问,可以通过点击"提供者"的名字,查看对方的联系方式,联系对方咨询.