盾怪网教程:是一个免费提供流行杀毒软件教程、在线学习分享的学习平台!

JBuilder 9 开发一个文本编辑器

时间:2024/10/8作者:未知来源:盾怪网教程人气:

[摘要]About事件处理器中的代码收集到新的 helpAbout()方法中,即可从菜单处理器也可从按钮处理器对其调用。   // Display the About box.   void helpAbo...
About事件处理器中的代码收集到新的 helpAbout()方法中,即可从菜单处理器也可从按钮处理器对其调用。

  // Display the About box.
  void helpAbout() {
  TextEditFrame_AboutBox dlg = new TextEditFrame_AboutBox(this);
  Dimension dlgSize = dlg.getPreferredSize();
  Dimension frmSize = getSize();
  Point loc = getLocation();
  dlg.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x,(frmSize.height - dlgSize.height) / 2 + loc.y);

  dlg.setModal(true);
  dlg.show();
  }


  4) 将事件处理与文本区联系起来

  将事件处理与文本区JtextArea联系起来,这样只要有输入操作用户程序就设置dirty标志为真。为此需要在JtextArea的文本模板中添加一个Swing控件DocumentListener(文本监听器)且检查插入、删除和改变等事件。

   DocumentListener被添加到 jbInit():

  document1.addDocumentListener(new TextEditFrame_document1_documentAdapter(this));

   在void document1_changedUpdate(DocumentEvent e)事件中插入下面代码:

  dirty = true;

   为document1 再创建两个事件:insertUpdate()和removeUpdate()。 在这些事件中插入在changedUpdate()事件中使用的同样的代码。

  这样只要在文本区输入任何字符都将强迫dirty标志为真。

  5) 在文本区添加一个右击弹出菜单

  控件DBTextDataBinder用来在Swing文本控件中添加一个执行诸如剪切、复制、粘贴等简单编辑任务的右击菜单。DBTextDataBinder还有一个将文件装入JtextArea和保存文件的内部动作,但不允许用户恢复显示在状态栏的装入或保存的文件名。本例中,只是添加一个DBTextDataBinder控件给jTextArea1,不使用其中的文件打开和保存操作。

  6) 在窗口的标题栏展示文件名和状态

  添加使用应用程序的标题栏显示当前文件名和当文件受"污染"时显示一个星花号的代码。为此,创建一个更新标题栏的新方法,然后从代码改变文件名或改变dirty标志的地方调用它。给这个新方法取名为updateCaption()。

   updateCaption()方法:

  // Update the title bar of the application to show the filename and its dirty state.
  void updateCaption() {
  String caption;
  if (currFileName == null) {
  // synthesize the "Untitled" name if no name yet.
  caption = "Untitled";
  }
  else {
  caption = currFileName;
  }
  // add a "*" in the caption if the file is dirty.
  if (dirty) {
  caption = "* " + caption;
  }
  caption = "Text Editor - " + caption;
  this.setTitle(caption);
  }

   从dirty标志被实际改变的每一个地方或者每当用户改变当前文件名currFileName的时候调用 updateCaption()。特别要将调用 updateCaption()语句放在下列这些地方:

  ①在TextEditFrame()构造器的try模块内紧接着调用jbInit()方法之后;

  ② 在openFile()方法的try模块的最后一行;

  ③ 在saveFile()方法try模块返回true的前一行;

  ④ 在File

关键词:JBuilder 9 开发一个文本编辑器




Copyright © 2012-2018 盾怪网教程(http://www.dunguai.com) .All Rights Reserved 网站地图 友情链接

免责声明:本站资源均来自互联网收集 如有侵犯到您利益的地方请及时联系管理删除,敬请见谅!

QQ:1006262270   邮箱:kfyvi376850063@126.com   手机版