1、打开 菜单保存新建退出另存为文件剪切查找复制字体字号插入对象替换 图4.2.1 功能架构图4.2.2程序主要组件概括1.基本的Frame框架;2.菜单;3.打开文件对话框;4.保存文件对话框;5.颜色对话框;6.Choice下拉列表,运来实现字体设置;7.简单的帮助框架。4.3详细设计4.3.1文件打开与保存文本编辑器的保存和打开功能的实现用文件对话框及输入输出流来完成。先建立打开和保存对话框,在public void actionPerformed(ActionEvent e)里分别用FileWriter()和FileReader()方法实现保存和打开。filedialog_save=new
2、 FileDialog(this,保存文件对话框,FileDialog.SAVE); filedialog_save.setVisible(false); filedialog_load=new FileDialog(this,FileDialog.LOAD); filedialog_load.setVisible(false); filedialog_save.addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e) filedialog_save.setVisible(false); );
3、filedialog_load.addWindowListener(new WindowAdapter() filedialog_load.setVisible(false); public void actionPerformed(ActionEvent e) if(e.getSource()=itemSave) filedialog_save.setVisible(true); if(filedialog_save.getFile()!=null) try File file=new File(filedialog_save.getDirectory(), filedialog_save.
4、getFile(); tofile=new FileWriter(file); out=new BufferedWriter(tofile); out.write(area.getText(),0,(area.getText().length(); out.close(); tofile.close(); catch(IOException e1) else if(e.getSource()=itemLoad) filedialog_load.setVisible(true); area.setText(null); String s; if(filedialog_load.getFile()
5、! tryFile file=new File(filedialog_load.getDirectory(), filedialog_load.getFile(); file_reader=new FileReader(file); in=new BufferedReader(file_reader); while(s=in.readLine()! area.append(s+n); in.close(); file_reader.close(); catch(IOException e1) 4.3.2字体,字形,字体大小的设置文本编辑器要实现对字体的设置,选用了GraphicsEnviron
6、ment对象调用String getAvailableFontFamilyNames()方法,该方法可以获取计算机上所有可用的字体名称,并存放到字符串数组中。Choice list;GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment(); String fontName=ge.getAvailableFontFamilyNames();public void itemStateChanged(ItemEvent e) String name=list.getSelectedItem();Font f=ne
7、w Font(name,Font.PLAIN,15); area.setFont(f); else if(e.getSource()=item8) /设置字形(常规,倾斜,加粗) Font font=area.getFont(); int style=font.getStyle(); style=style0; area.setFont(new Font(,style,font.getSize(); else if(e.getSource()=item9) style=style2; else if(e.getSource()=item10) int style=font.getStyle()
8、; style=style1; else if(e.getSource()=item11) /设置字体大小 area.setFont(new Font(font.getName(),style,12); else if(e.getSource()=item12) area.setFont(new Font(font.getName(),style,24); else if(e.getSource()=item13) area.setFont(new Font(font.getName(),style,36);4.3.3剪切,复制,粘贴设置文本编辑器中关于剪切,复制,粘贴功能的实现选用处理JTe
9、xtArea的DocumentEvent事件,通过area.cut(),area.copy(),area.paste()方法,点击“编辑”中相应菜单项可以选择将文本区中选中的内容剪切,复制,粘贴。 public void changedUpdate(DocumentEvent e) String s=area.getText(); public void removeUpdate(DocumentEvent e) changedUpdate(e); public void insertUpdate(DocumentEvent e) else if(e.getSource()=item2) ar
10、ea.cut(); else if(e.getSource()=item3) area.copy(); else if(e.getSource()=item4) area.paste();4.3.4插入子菜单,删除子菜单,创建格式菜单及其菜单项 JMenuItem insertItem=new JMenuItem(插入文本(K) insertItem.setMnemonic(K editMenu.add(insertItem); insertItem.addActionListener( new ActionListener() public void actionPerformed(Acti
11、onEvent event) JPanel insertPanel=new JPanel(); JLabel insertLabel=new JLabel(要插入的内容 JTextField inputText=new JTextField(10); insertPanel.add(insertLabel); insertPanel.add(inputText); JOptionPane.showMessageDialog(null,insertPanel); int fromIndex=displayText.getCaretPosition();/取得当前的光标位置 displayText
12、.insert(inputText.getText(),fromIndex); ); JMenuItem RemoveItem=new JMenuItem(删除(G) RemoveItem.setMnemonic(G editMenu.add(RemoveItem); RemoveItem.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) int start=displayText.getSelectionStart(); int end=displayText.getSelect
13、ionEnd(); displayText.replaceRange(null,start,end); ); editMenu.addSeparator(); bar.add( editMenu );/add editMenu JMenu formatMenu = new JMenu( 格式(R) formatMenu.setMnemonic( R4.3.5创建,添加帮助菜项 JMenu helpMenu = new JMenu( 帮助(H) helpMenu.setMnemonic( H JMenuItem helpItem = new JMenuItem( 帮助主题(H). helpIte
14、m.setMnemonic( helpMenu.add( helpItem ); helpItem.addActionListener( public void actionPerformed( ActionEvent event ) JTextArea helpText = new JTextArea( JScrollPane scroller = new JScrollPane(helpText); JOptionPane.showMessageDialog(null,scroller); bar.add( helpMenu ); /添加4.4设计成果4.4.1运行界面 图2 文本编辑器主
15、界面 图3文本编辑器编辑界面 图4文本编辑器文件界面 图5文本编辑器格式 图6文本编辑器查找界面 图7 文本编辑器帮助界面 图8文本编辑器字体名称界面 图9文本编辑器字体风格界面 图10文本编辑器中帮助中关于对话框 图11查找消息对话框 图12文本编辑器中另存为对话框4.4.2主要代码import java.awt.*;import java.awt.event.*;import java.awt.datatransfer.*;import javax.swing.*;import java.io.*;import java.lang.*; public class Notepad exten
16、ds JFrame private final Color colorvalues = Color.black, Color.blue, Color.red, Color.green ; /定义颜色数组 String styleNames = Bold, Italic ;/定义风格数组 String fontNames = 宋体华文行楷隶书/字体数组 String sizeString = new String30;/字号数组 int size = new int30;/与字号数组对应的字号整数,用于设置文字大小 private JRadioButtonMenuItem colorItems,
17、 fonts; private JCheckBoxMenuItem styleItems; private JTextArea displayText;/定义文本编辑区 private ButtonGroup fontGroup, colorGroup;/字体组,跟字色组 private int style;/字体风格 private JScrollPane scroll;/为文本编辑区提供滚动条 private String selectText = ;/存放文本编辑区中选中的文本内容 private JComboBox styleBox,fontBox,sizeBox;/工具栏 priva
18、te JPanel toolPanel;/存放工具栏 private int rowNumber = 0; private FileDialog fd = new FileDialog(this); / set up GUI public Notepad() super( 记事本 /创建菜单条 JMenuBar bar = new JMenuBar(); setJMenuBar( bar ) / 设置文件菜单及其菜单项 JMenu fileMenu = new JMenu( 文件(F) fileMenu.setMnemonic( F / 设置新建菜单项 JMenuItem newItem =
19、new JMenuItem( 新建(N) newItem.setMnemonic( N fileMenu.add( newItem ); newItem.addActionListener( new ActionListener() public void actionPerformed( ActionEvent event ) displayText.setText( ); / 设置打开菜单项 JMenuItem openItem = new JMenuItem( 打开(O) openItem.setMnemonic( O fileMenu.add( openItem ); openItem
20、.addActionListener( fd.setTitle(打开 /设置标题 fd.show(); if (fd.getFile() != null) File file = new File(fd.getFile(); /用从fd中取得的文件建立新文件,即打开的文件 displayText.setText( try FileReader f = new FileReader(file); BufferedReader b = new BufferedReader(f);/按行读打开的文件,然后传入文本域 String s; try while (s = b.readLine() ! di
21、splayText.append(s + n/将给定文本追加到文本域的当前文本(即把读的内容加入文本域) f.close(); b.close(); catch (IOException ex) catch (FileNotFoundException ex) else return; ); fileMenu.addSeparator(); /加分隔线 / 设置退出菜单项 JMenuItem exitItem = new JMenuItem( 退出(X) exitItem.setMnemonic( x fileMenu.add( exitItem ); exitItem.addActionLi
22、stener( public void actionPerformed( ActionEvent event ) System.exit( 0 ); );bar.add( fileMenu ); /剪切菜单选项 JMenuItem cutItem = new JMenuItem( 剪切(T) cutItem.setMnemonic( T editMenu.add( cutItem ); cutItem.addActionListener( selectText = displayText.getSelectedText();/取得选定的文本 int start = displayText.getSelectionStart();/选定的文本的开始位置 int end = displayText.getSelectionEnd();/选定的文本的结束位置 displayText.replaceRange(, start, end);/*用指定替换文本替换指定开始位置与结束位置之间的文本。 这里指定替换文本为空,即为剪切了文本*/ /复制菜单选项
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1