文本编辑器:用JBuilder 9 开发一个文本编辑器

  、概述

  文本编辑器是种最常用应用下面我们利用Jbuilder 9集成开发环境用java语言实现个简单文本编辑器该文本编辑器具有读出、写入、编辑文本文件可以设定文字颜色、字形和编辑区域背景颜色等基本功能

  我们首先通过Jbuilder 9项目向导和应用向导创建项目然后应用可视化设计工具修改UI设计连接事件编辑源码以及常用Control控件和任务诸如菜单项、工具条、文本区域和系统事件等常用Control控件和任务处理涉及到具体技术有:

   用JFileChooser 对话框让用户选择文本文件

   用JtextArea读、写和处理文本文件中文字

   设置前景色和背景色

   用dbSwing FontChooser对话框设置字型

   在状态栏和窗口标题栏显示信息

   手工添加处理UI事件代码

   通过将代码放在个可被菜单项和按钮两个事件处理器"帮助"思路方法中 使得菜单项和按钮执行相同代码

   给JtextAreaControl控件增加个右击菜单

   保持对文件位置以及文件是否活动过跟踪展示对文件|新建, 文件|打开, 文件|保存, 文件|另存为编辑和退出等逻辑处理

   将"Text Editor" 应用展开为JAR 文件

   2、开发文本编辑器java源介绍说明

  文本编辑器包含 3个java 源即TextEditFrame.java、TextEdit.java 和TextEditFrame_AboutBox.java 下面将分别介绍如下:

  1、TextEditFrame.java代码(节选部分):

package texteditor;
//TextEditFrame.java
import java.awt.*;//导入类
import java.awt.event.*;
import javax.swing.*;
import com.borland.dbswing.*;
import java.io.*;
import javax.swing.text.*;
import javax.swing.event.*;
public TextEditFrame extends JFrame {
IntlSwingSupport lSwingSupport1 = IntlSwingSupport;
//Swing Control控件互联网化:即本地化应用需要添加行代码以便Swing Control控件JfileChooser //和JcolorChooser出现在运行语言中
JPanel contentPane; //设置内容窗(contentPane)JPanelControl控件
JMenuBar menuBar1 = JMenuBar;//创建菜单条并加入到框架窗体中
JMenu menuFile = JMenu;//创建File菜单和相应菜单项
JMenuItem menuFileExit = JMenuItem;
JMenu menuHelp = JMenu;//创建Help菜单和相应菜单项
JMenuItem menuHelpAbout = JMenuItem;
JToolBar toolBar = JToolBar;//创建工具条组件
JButton jButton1 = JButton;//创建按钮组件
JButton jButton2 = JButton;
JButton jButton3 = JButton;
ImageIcon image1;//定义图标
ImageIcon image2;
ImageIcon image3;
JLabel statusBar = JLabel;//创建标签组件
BorderLayout borderLayout1 = BorderLayout;//创建BorderLayout 布局器
JScrollPane jScrollPane1 = JScrollPane;//创建滚动窗Control控件
JTextArea jTextArea1 = JTextArea;//创建多行文本域组件
JMenuItem jMenuItem1 = JMenuItem;//创建菜单项
JMenuItem jMenuItem2 = JMenuItem;
JMenuItem jMenuItem3 = JMenuItem;
JMenuItem jMenuItem4 = JMenuItem;
FontChooser fontChooser1 = FontChooser;//创建字型选择对话框
JMenu jMenu1 = JMenu;
JMenuItem jMenuItem5 = JMenuItem;
JMenuItem jMenuItem6 = JMenuItem;
JMenuItem jMenuItem7 = JMenuItem;
JFileChooser jFileChooser1 = JFileChooser;//创建文本选择对话框
String currFileName = null; // Full path with filename. null means /untitled.
boolean dirty = false;
Document document1; //文本
DBTextDataBinder dBTextDataBinder1 = DBTextDataBinder;
// True means modied text.
//构造架框
public TextEditFrame {
 enableEvents(AWTEvent.WINDOW_EVENT_MASK);
 try {
  jbInit;
  updateCaption;
 }
 catch(Exception e) {
  e.prStackTrace;
 }
}
//组件
private void jbInit throws Exception {
 // 3个工具栏按钮图标
 image1 = ImageIcon(TextEditFrame..getResource("openFile.g"));
 image2 = ImageIcon(TextEditFrame..getResource("closeFile.g"));
 image3 = ImageIcon(TextEditFrame..getResource("help.g"));
 contentPane = (JPanel) this.getContentPane;//内容创格
 document1 = jTextArea1.getDocument;//多行文本域文档
 contentPane.Layout(borderLayout1);//borderLayout布局器
 this.Size( Dimension(400, 300));//窗口大小
 this.Title("Text Editor");//窗口标题
 statusBar.Text(" ");
 menuFile.Text("File");
 menuFileExit.Text("Exit");
 menuFileExit.addActionListener( TextEditFrame_menuFileExit_ActionAdapter (this));
 //添加事件监听器
 menuHelp.Text("Help");
 menuHelpAbout.Text("About");
 menuHelpAbout.addActionListener( TextEditFrame_menuHelpAbout_ActionAdapter (this));
 jButton1.Icon(image1);//设置 3个工具栏按钮图标添加事件监听器
 jButton1.addActionListener( TextEditFrame_jButton1_actionAdapter(this));
 jButton1.ToolTipText("Open File");
 jButton2.Icon(image2);
 jButton2.addActionListener( TextEditFrame_jButton2_actionAdapter(this));
 jButton2.ToolTipText("Close File");
 jButton3.Icon(image3);
 jButton3.addActionListener( TextEditFrame_jButton3_actionAdapter(this));
 jButton3.ToolTipText("About");
 jTextArea1.LineWrap(true);
 jTextArea1.WrapStyleWord(true);
 jTextArea1.Background(Color.white);
 jMenuItem1.Text("New");//设置菜单添加事件监听器
 jMenuItem1.addActionListener( TextEditFrame_jMenuItem1_actionAdapter(this));
 jMenuItem2.Text("Open");
 jMenuItem2.addActionListener( TextEditFrame_jMenuItem2_actionAdapter(this));
 jMenuItem3.Text("Save");
 jMenuItem3.addActionListener( TextEditFrame_jMenuItem3_actionAdapter(this));
 jMenuItem4.Text("Save As");
 jMenuItem4.addActionListener( TextEditFrame_jMenuItem4_actionAdapter(this));
 fontChooser1.Frame(this);
 fontChooser1.Title("Font");
 jMenu1.Text("Edit");
 jMenuItem5.Text("Font");
 jMenuItem5.addActionListener( TextEditFrame_jMenuItem5_actionAdapter(this));
 jMenuItem6.Text("Foreground Color");
 jMenuItem6.addActionListener( TextEditFrame_jMenuItem6_actionAdapter(this));
 jMenuItem7.Text("Background Color");
 jMenuItem7.addActionListener( TextEditFrame_jMenuItem7_actionAdapter(this));
 document1.addDocumentListener( TextEditFrame_document1_documentAdapter(this));
 dBTextDataBinder1.JTextComponent(jTextArea1);
 //Turn off right-click file Open... menu item.
 dBTextDataBinder1.EnableFileLoading(false);
 //Turn off right-click file Save... menu item.
 dBTextDataBinder1.EnableFileSaving(false);
 toolBar.add(jButton1);//工具组件添加按钮
 toolBar.add(jButton2);
 toolBar.add(jButton3);
 menuFile.add(jMenuItem1);//菜单组件添加菜单项
 menuFile.add(jMenuItem2);
 menuFile.add(jMenuItem3);
 menuFile.add(jMenuItem4);
 menuFile.addSeparator;//采单组件添加分隔线
 menuFile.add(menuFileExit);
 menuHelp.add(menuHelpAbout);
 menuBar1.add(menuFile);
 menuBar1.add(jMenu1);
 menuBar1.add(menuHelp);
 this.JMenuBar(menuBar1);
 contentPane.add(toolBar, BorderLayout.NORTH);
 //内容窗设置borderLayout布局器
 contentPane.add(statusBar, BorderLayout.SOUTH);
 contentPane.add(jScrollPane1, BorderLayout.CENTER);
 jScrollPane1.getViewport.add(jTextArea1, null);
 jMenu1.add(jMenuItem5);
 jMenu1.add(jMenuItem6);
 jMenu1.add(jMenuItem7);
}
// Display the About box.
void helpAbout {
 TextEditFrame_AboutBox dlg = TextEditFrame_AboutBox(this);
 Dimension dlgSize = dlg.getPreferredSize;
 Dimension frmSize = getSize;
 Po loc = getLocation;
 dlg.Location((frmSize.width - dlgSize.width) / 2 + loc.x, (frmSize.height - dlgSize.height) / 2 + loc.y);
 dlg.Modal(true);
 dlg.show;
}
.........
.........
  TextEditFrame.java 是实现文本编辑器主要它有下面6点编程窍门技巧介绍说明:

  1) 制作个完全充满用户界面顶部菜单栏和底部状态栏的间区域文本区

  主用户界面容器布局管理器需要采用边界布局(Borderlayout)在主容器中含有个叫做内容窗(contentPane)JPanel Control控件被改变成边界布局需要做只是在内容窗添加个文本区Control控件为此先在内容窗添加个滚动窗再在滚动窗内放上文本区Control控件(jTextArea)滚动窗提供个带滚动棒(JScollPane)文本区

  个边界布局容器被分成 5个区域:北、南、东、西、中每个区域只能有个Control控件所以最多可有 5个Control控件(注:含有多个Control控件面板被认为是个Control控件)放进中心区域Control控件完全占满该容器Control控件不被含有Control控件任何其他区域所占据例如在本例中工具栏占据北区(顶)状态栏占据南区(低步)由于东西两个区域没有安排Control控件这样滚动窗Control控件占据中心区域并扩展到容器左(西)右(东)边缘

  2) 创建菜单File (包含New、Open、Save、Save as 和Exit菜单项)菜单Edit{包含Font(字体)、Foreground(前景色)和Background(背景色)菜单项} 和菜单Help (包含About帮助介绍说明)

  ①菜单EditFont(字体)、Foreground(前景色)和Background(背景色)菜单项:

   添加字型选择对话框

  给菜单挂上事件从 Edit|Font 菜单项开始该菜单将引出个FontChooser (字型选择)对话框

  给FontChooser附加个菜单项事件(源)如下:

void jMenuItem5_actionPerformed(ActionEvent e) {
 // Handle the "Edit Font" menu item
 // Pick up the existing font from the text area
 // and put it o the FontChooser before showing
 // the FontChooser, so that we are editing the
 // existing / previous font.
 fontChooser1.SelectedFont(jTextArea1.getFont);
 // Obtain the Font from the FontChooser.
 // First test the value of showDialog to
 // see the user pressed OK.
  (fontChooser1.showDialog) {
  // Set the font of jTextArea1 to the font
  // the user selected before pressing the OK button
  jTextArea1.Font(fontChooser1.getSelectedFont);
 }
 //repas menu after item is selected
 this.repa;
 //Repas text properly some text is highlighted when font is changed.
 jTextArea1.repa;
}
  给JcolorChooser(颜色选择)附加个菜单项事件

  创建Edit|Foreground and Edit|Background两个菜单事件并将它们和Swing中JcolorChooser对话Control控件连接起来

void jMenuItem6_actionPerformed(ActionEvent e) {
 // Handle the "Foreground Color" menu item
 Color color = JColorChooser.showDialog(this,"Foreground Color",jTextArea1.getForeground);
  (color != null) {
  jTextArea1.Foreground(color);
 }
 //repas menu after item is selected
 this.repa;
}
void jMenuItem7_actionPerformed(ActionEvent e) {
 // Handle the "Background Color" menu item
 Color color = JColorChooser.showDialog(this,"Background
 Color",jTextArea1.getBackground);
  (color != null) {
  jTextArea1.Background(color);
 }
 //repas menu after item is selected
 this.repa;
}
  ②菜单File New、Open、Save、Save as 和Exit菜单项:

   添加测试文件是否被修改代码

  需要保持跟踪文件被生成、打开、或保存的后是否被修改过("脏")这样当关闭文件或退出时就可以提示问用户是否要保存操作为此增加个称作dirty布尔变量

  在源代码中添加下列okToAbandon思路方法可将这个新思路方法紧放在saveAsFile思路方法的后:

// Check file is dirty.
// If so get user to make a "Save? yes/no/cancel" decision.
boolean okToAbandon {
  value = JOptionPane.showConfirmDialog(this, "Save changes?","Text Edit", JOptionPane.YES_NO_CANCEL_OPTION);
 switch (value) {
   JOptionPane.YES_OPTION:
   // yes, please save changes
    saveFile;
   JOptionPane.NO_OPTION:
   // no, abandon edits
   // i.e. true without saving
    true;
   JOptionPane.CANCEL_OPTION:
  default:
   // cancel
    false;
 }
}
  将在随后完成上面思路方法当用户选择 File|New, File|Open, 或 File|Exit时就被 这个思路方法是测试文本是否需要保存(是否动过)若文本动过这个思路方法就用Yes, No, Cancel消息对话问用户是否保存这个思路方法在用户点选Yes按钮时saveFile若这个思路方法返回布尔值是true(真),则表明可以退出当前文件文件是干净或用户点选了Yes或No按钮如果返回值是false(假)意味着用户点选了Cancel实际检查文件是否被改变代码在随后步骤中添加目前这个思路方法总认为文件是不干净即使文字根本没被动过随后要添加个当用户在文本区域输入文字时就将dirty变量设置为true思路方法并在okToAbandon思路方法头部增加测试dirty变量代码

   添加个清除文本区菜单事件处理器

  将File|New菜单项和清除文本区事件处理器挂起钩来

void jMenuItem1_actionPerformed(ActionEvent e) {
 // Handle the File|New menu item.
  (okToAbandon) {
  // clears the text of the TextArea
  jTextArea1.Text("");
  // clear the current filename and the file as clean:
  currFileName = null;
  dirty = false;
  updateCaption;
 }
}
   添加个文件选择对话框

  将File|Open菜单项和提供给用户个文件选择对话框JfileChooserControl控件事件处理器挂起钩来如果用户选择了个文件按下了OK按钮这个事件处理器打开那个文本文件并把文字放入JTextArea(即文本区)

void jMenuItem2_actionPerformed(ActionEvent e) {
 //Handle the File|Open menu item.
  (!okToAbandon) {
  ;
 }
 // Use the OPEN version of the dialog, test for Approve/Cancel
  (JFileChooser.APPROVE_OPTION jFileChooser1.showOpenDialog(this)) {
  // Call openFile to attempt to load the text from file o
  TextArea
  openFile(jFileChooser1.getSelectedFile.getPath);
 }
 this.repa;
}
   添加从文件中读出文字代码

  添加从选定文件中将文字实际读到文本区JTextArea代码

  首先需要在用户类中增加个新思路方法执行实际打开文件操作这个思路方法叫作openFile

// Open named file; read text from file o jTextArea1; report to statusBar.
void openFile(String fileName)
{
 try
 {
  // Open a file of the given name.
  File file = File(fileName);
  // Get the size of the opened file.
   size = ()file.length;
  // Set to zero a counter for counting the number of
  // characters that have been read from the file.
   chars_read = 0;
  // Create an input reader based _disibledevent= screenSize.width;
  }
  frame.Location((screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.height) / 2);
  frame.Visible(true);
 }
 //Main method
 public void (String args) {
  try {
   UIManager.LookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName);
  }
  catch(Exception e) {
   e.prStackTrace;
  }
   TextEditClass;
 }
}
  上面这段主要是构建 TextEditorFrame 主窗口和主思路方法入口( )它有下面2点编程窍门技巧介绍说明:

Tags:  html文本编辑器 网页文本编辑器 在线文本编辑器 文本编辑器

延伸阅读

最新评论

发表评论