使用JavaFX如何写用户界面控制器(一)

原创|其它|编辑:郝浩|2009-06-18 11:26:46.000|阅读 857 次

概述:JavaFX对于用户界面非常有用,用它来编写控制器也很有帮助。当然这里说指的不是用JavaFX来编写一个完整的程序。可能要将它的使用范围拓宽还是有些问题。但是指定的模式和前端控制器都没有问题,而且你也可以利用其中的Binding。

# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>

  在本文中,我们关心的是BlueBill Mobile类,尤其是管理所有Search Species屏幕之后逻辑的控制器;因此本文有助于你了解JavaFX的语言性能。而且我们会举出一些实例来阐述要介绍的技巧和典型JavaFX结构的陷阱。

  笔者想应用程序中嵌入了更新的屏播。视频播放要求使用QucikTime。

  这里的概念是在搜索框中键入查询时,英文函数或科学名称函数会对清单过滤。此外,当这些生效的时候,BlueBill Mobile还可以执行自动完成输入。例如,如果在键入查询的时候你仔细查看视频会发现只输入了"a-r-d-a-c"来选择"Ardea Cinerea";或用于"Pied Avocet"的"p-i-e-< space>-a"。BlueBill Mobile 会自动会剩余部分进行补充因为在某些情况下,不存在其他选择。这是用来改善移动设备性能的重要功能:你可以以较少的输入达到相同目的。

  按照MVC模式,就非常有必要在单独的控制器中概括这种模式;此外,也很容易对这种模式进行单元测试。

  首先,让我们看一下代表了分类群的模式类:

     package it.tidalwave.bluebillmfx.taxon.model;
  import java.lang.Comparable;

  public class Taxon extends Comparable

  {

  public-read protected var displayName : String;

  public-read protected var scientificName : String;

  public-read protected var id : String;

  override function compareTo (other : Object)

  {

  return displayName.compareTo((other as Taxon).displayName);

  }

  override function toString()

  {

  return "{displayName} ({scientificName}) ({id})"

  }

  }

  public function displayNameGetter (taxon : Taxon): String

  {

  return taxon.displayName;

  }

  public function scientificNameGetter (taxon : Taxon): String

  {

  return taxon.scientificName;

  }

  public def namePropertyGetters = [displayNameGetter, scientificNameGetter];
 
  类托架外面定义的函数和变量相当于Java静态分析。

  这里我们省略了一些不相关的实际项目。基本上,该模式暴露了三个属性,其中有意思的两个分别是displayName和scientificName。我们也可以定义两个函数来处理这两个问题,我们会把这些函数放在namePropertyGetters序列中。

      package it.tidalwave.bluebillmfx.taxon.controller;
  import it.tidalwave.bluebillmfx.taxon.model.Taxon;

  public class TaxonSearchController

  {

  public var selectedTaxon = bind if (selectedTaxonIndex < 0) then null else filteredTaxons[selectedTaxonIndex];

  public var selectedTaxonIndex : Integer = -1;

  public var taxons: Taxon[];

  public var filter = "" on replace

  {

  filteredTaxons = taxons[taxon | matches(taxon, filter)];

  update();

  }

  public-read var autoCompleted = "";

  public var filteredTaxons: Taxon[];

  protected function matches (taxon : Taxon, string: String) : Boolean

  {

  if (string == "")

  {

  return true;

  }

  for (propertyGetter in Taxon.namePropertyGetters)

  {

  if (propertyGetter(taxon).toLowerCase().startsWith(filter.toLowerCase()))

  {

  return true;

  }

  }

  return false;

  }

  protected function update(): Void

  {

  def autoCompletedTry = commonLeadingSubstring(filteredTaxons, findMatchingPropertyGetter());

  //

  // Sometimes it can't find a better auto-completion than the current filter, since it searches the displayName

  // and the scientificName at the same time. In this case, we just ignore the new value.

  //

  if (autoCompletedTry.length() > filter.length())

  {

  autoCompleted = autoCompletedTry;

  }

  selectedTaxonIndex = if (sizeof filteredTaxons == 1) then 0 else -1;

  println("selectedTaxonIndex: {selectedTaxonIndex}")

  }

  protected function findMatchingPropertyGetter(): function (:Taxon): String

  {

  for (taxon in filteredTaxons)

  {

  for (propertyGetter in Taxon.namePropertyGetters)

  {

  if (propertyGetter(taxon).toLowerCase().startsWith(filter.toLowerCase()))

  {

  return propertyGetter;

  }

  }

  }

  return null;

  }

  // some stuff later

  }


标签:界面设计

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@evget.com

文章转载自:IT专家网

为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP