噶米课程基于于JAVA的记事本编辑器的与实现(下载后送源代码)

内容发布更新时间 : 2024/6/3 12:09:03星期一 下面是文章的全部内容请认真阅读。

} filedialog_load.getFile()); file_reader=new FileReader(file); in=new BufferedReader(file_reader); while((s=in.readLine())!=null) area.append(s+'\\n'); in.close(); file_reader.close(); } catch(IOException e1) {} } } 文本编辑器的保存和打开功能的实现用文件对话框及输入输出流来完成。先建立打开和保存对话框,在public void actionPerformed(ActionEvent e)里分别用FileWriter()和FileReader()方法实现保存和打开。 2) 调用颜色对话框 else if(e.getSource()==item5) { Color newColor=JColorChooser.showDialog(this,\调色板\ if(newColor!=null) area.setBackground(newColor); } 文本编辑器要实现背景功能可以用javax.swing包中的JColorChooser类的静态方法。Pubic static Color showDialog(Component,String title,Color initialColor),其中参数Component指定对话框所依赖的组件,即文本编辑器中的文本区area,title指定对话框的标题“调色板”,initialColor指定对话框返回的初始颜色,即对话框消失后,返回默认值。 3) 建立Choice下拉列表实现对字体的设置 Choice list; GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment(); String fontName[]=ge.getAvailableFontFamilyNames(); public void itemStateChanged(ItemEvent e) { String name=list.getSelectedItem(); Font f=new Font(name,Font.PLAIN,15); area.setFont(f); } 文本编辑器要实现对字体的设置,我选用了GraphicsEnvironment对象调用String [] getAvailableFontFamilyNames()方法,该方法可以获取计算机上所有可用的字体名称,并存放到字符串数组中。 4)字形,字体大小部分 else if(e.getSource()==item8) //设置字形(常规,倾斜,加粗) { Font font=area.getFont(); int style=font.getStyle(); style=style^0; area.setFont(new Font(\ } else if(e.getSource()==item9) { Font font=area.getFont(); int style=font.getStyle(); style=style^2; area.setFont(new Font(\ } else if(e.getSource()==item10) { Font font=area.getFont(); int style=font.getStyle(); style=style^1; area.setFont(new Font(\ } else if(e.getSource()==item11) //设置字体大小 { Font font=area.getFont(); int style=font.getStyle(); area.setFont(new Font(font.getName(),style,12)); } else if(e.getSource()==item12) { Font font=area.getFont(); int style=font.getStyle(); area.setFont(new Font(font.getName(),style,24)); } else if(e.getSource()==item13) { Font font=area.getFont(); int style=font.getStyle(); area.setFont(new Font(font.getName(),style,36)); } 5)剪切,复制,粘贴部分 public void changedUpdate(DocumentEvent e) { String s=area.getText(); } public void removeUpdate(DocumentEvent e) { changedUpdate(e); } public void insertUpdate(DocumentEvent e) { changedUpdate(e); } public void actionPerformed(ActionEvent e) { else if(e.getSource()==item2) { area.cut(); } else if(e.getSource()==item3) { area.copy(); } else if(e.getSource()==item4) { area.paste(); } } 文本编辑器中关于剪切,复制,粘贴功能的实现我选用处理JTextArea的DocumentEvent事件,通过area.cut(),area.copy(),area.paste()方法,点击“编辑”中相应菜单项可以选择将文本区中选中的内容剪切,复制,粘贴。 6) 帮助中关于主题部分 else if(e.getSource()==item6) { } class zhuti extends Frame implements ActionListener { Button btn; Label lab; zhuti() { setLayout(null); btn=new Button(\确定\zhuti zt=new zhuti(); lab=new Label(\这是一个简单的文本编辑器!\ add(btn); add(lab); btn.addActionListener(this); setBounds(200,200,250,150); setVisible(true); lab.setBounds(20 ,60,220,30); lab.setFont(new Font(\楷体\ } lab.setBackground(Color.cyan); btn.setBounds(100,100,50,30); btn.setBackground(Color.cyan); } public void actionPerformed(ActionEvent e) { if(e.getSource()==btn) { } dispose(); } 对于帮助中关于主题部分的实现,我另构建了一个Frame框架,主要是由一个标签及一个按钮构成,由标签输出“这是一个简单的文本编辑器!”,按钮用dispose()方法实现退出功能。 7)类图

联系客服:779662525#qq.com(#替换为@) 苏ICP备20003344号-4 ceshi