Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 903999 of XUL user interfaces

  • Revision slug: Web/Guide/CSS/Getting_started/XUL_user_interfaces
  • Revision title: XUL user interfaces
  • Revision id: 903999
  • Created:
  • Creator: cughudson_1
  • Is current revision? No
  • Comment

Revision Content

{{ CSSTutorialTOC() }}

本页用来描述创建用户界面的Mozilla特定语言。

你可以创建一个小型的DEMO在你的火狐浏览器中运行。

Information: 用户界面

虽然HTML已经差不多可以支持用户界面,但是HTML却不能完全支持创建一个独立程序(用户界面)时所需要的所有特性。

Mozilla 克服这方面的困难,创建了一种能用于创建用户界面的特定语言,这种语言就是:XUL (基于XML的用户界面描述语言, 通常这个单词发音类似于 "zool").

很多通用的与用户界面相关的特性都内建到了XULI中,n XUL,例如XUL除了提供类似于windows系统中的对话框和wizard外,还提供了状态栏、菜单、工具栏甚至是“浏览器 ”。

更多特定的功能可通过使用XUL与一些其他的技术(在CSS、JavaScript以及XBL bindings中有提到过)相结合实现,

与其他基于XML的语言一样,XUL也利用CSS来进行布局。

详见
更多关于XUL方面的信息,请参看XUL 页面

Action: 一个XUL案例

用创建纯文本文档的形式创建一个新的XUL文档,并命名为doc7.xul,复制、粘贴下面的内容(滑动右侧的滚动条,确保把框内的内容复制完全) C

<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="style7.css"?>
<!DOCTYPE window>

<window
  xmlns="https://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
  title="CSS Getting Started - XUL demonstration"
  onload="init();">

<script type="application/javascript" src="script7.js"/>

<label class="head-1" value="XUL demonstration"/>

<vbox>

  <groupbox class="demo-group">
    <caption label="Day of week calculator"/>
    <grid>
      <columns>
        <column/>
        <column/>
        </columns>
      <rows>
        <row>
          <label class="text-prompt" value="Date:" 
            accesskey="D" control="date-text"/>
          <textbox id="date-text" type="timed" 
            timeout="750" oncommand="refresh();"/>
          </row>
        <row>
          <label value="Day:"/>
          <hbox id="day-box">
            <label class="day" value="Sunday" disabled="true"/>
            <label class="day" value="Monday" disabled="true"/>
            <label class="day" value="Tuesday" disabled="true"/>
            <label class="day" value="Wednesday" disabled="true"/>
            <label class="day" value="Thursday" disabled="true"/>
            <label class="day" value="Friday" disabled="true"/>
            <label class="day" value="Saturday" disabled="true"/>
            </hbox>
          </row>
        </rows>
      </grid>
    <hbox class="buttons">
      <button id="clear" label="Clear" accesskey="C" 
        oncommand="clearDate();"/>
      <button id="today" label="Today" accesskey="T"
        oncommand="setToday();"/>
      </hbox>
    </groupbox>

  <statusbar>
    <statusbarpanel id="status"/>
    </statusbar>

</vbox>

</window>

创建一个新的CSS文档,命名为style7.css,并复制、粘贴下列框内的内容。

/*** XUL demonstration ***/
window {
  -moz-box-align: start;
  background-color: -moz-dialog;
  font: -moz-dialog;
  padding: 2em;
  }

.head-1 {
  font-weight: bold;
  font-size: 200%;
  padding-left: 5px;
  }


/* the group box */
.demo-group {
  padding: 1em;
  }

.demo-group grid {
  margin-bottom: 1em;
  }

.demo-group column {
  margin-right: .5em;
  }

.demo-group row {
  margin-bottom: .5em;
  }

.demo-group .buttons {
  -moz-box-pack: end;
  }


/* the day-of-week labels */
.day {
  margin-left: 1em;
  }

.day[disabled] {
  color: #777;
  }

.day:first-child {
  margin-left: 4px;
  }


/* the left column labels */
.text-prompt {
  padding-top: .25em;
  }


/* the date input box */
#date-text {
  max-width: 8em;
  }


/* the status bar */
statusbar {
  width: 100%;
  border: 1px inset -moz-dialog;
  margin: 4px;
  padding: 0px 4px;
  }

#status {
  padding: 4px;
  }

#status[warning] {
  color: red;
  }

创建一个新的CSS文档,命名为script7.js,e, script7.js. 并复制、粘贴下列框内的内容。

// XUL demonstration

var dateBox, dayBox, currentDay, status; // elements

// called by window onLoad
function init() {
  dateBox = document.getElementById("date-text")
  dayBox = document.getElementById("day-box")
  status = document.getElementById("status")
  setToday();
  }

// called by Clear button
function clearDate() {
  dateBox.value = ""
  refresh()
  }

// called by Today button
function setToday() {
  var d = new Date()
  dateBox.value = (d.getMonth() + 1)
    + "/" + d.getDate()
    + "/" + d.getFullYear()
  refresh()
  }

// called by Date textbox
function refresh() {
  var d = dateBox.value
  var theDate = null

  showStatus(null)
  if (d != "") {
    try {
      var a = d.split("/")
      var theDate = new Date(a[2], a[0] - 1, a[1])
      showStatus(theDate)
      }
    catch (ex) {}
    }
  setDay(theDate)
  }

// internal
function setDay(aDate) {
  if (currentDay) currentDay.setAttribute("disabled", "true")
  if (aDate == null) currentDay = null
  else {
    var d = aDate.getDay()
    currentDay = dayBox.firstChild
    while (d-- > 0) currentDay = currentDay.nextSibling
    currentDay.removeAttribute("disabled")
    }
  dateBox.focus();
  }

function showStatus(aDate) {
  if (aDate == null) {
    status.removeAttribute("warning")
    status.setAttribute("label", "")
    }
  else if (aDate === false || isNaN(aDate.getTime())) {
    status.setAttribute("warning", "true")
    status.setAttribute("label", "Date is not valid")
    }
  else {
    status.removeAttribute("warning")
    status.setAttribute("label", aDate.toLocaleDateString())
    }
  }

To see the result exactly as intended, use the default theme in your browser. If you use a different theme, it changes some user-interface styles and the demonstration might look strange.

Open the document in your Mozilla browser and use the interface.

This wiki does not support XUL and JavaScript in pages, so it is not possible to show the demonstration here. It looks something like this:

XUL demonstration

Day of week calculator

Date: 6/27/2005
Day: Sunday Monday Tuesday Wednesday Thurdsay Friday Saturday
 

Clear Today

June 27, 2005

Notes about this demonstration:

  • The XUL document links the stylesheet as usual, and it also links the script.
  • The script is not important in this demonstration.
  • Much of the style that you see is determined by your browser's theme.

Examine the document's stylesheet to ensure that you understand all the rules there. If there is a rule that you do not understand, comment it out and refresh your browser to see the effect on the document.

Challenge
Use the DOM Inspector tool to examine the Date textbox. It is made up of other elements that are generated by its XBL binding.

Discover the class of its html:input element. This is the element that actually receives user input.

Using this knowledge, add a rule to the stylesheet that makes the background of the Date box pale blue when it has keyboard focus (but white when keyboard focus is somewhere else).

What next?

If you had difficulty understanding this page, or if you have other comments about it, please contribute to its Discussion page.

In this demonstration, you see the standard rectangular shapes that are common to most user interfaces. Mozilla also supports a specialized graphics language for creating shapes, using CSS stylesheets to specify the style. The next page demonstrates this: SVG and CSS.

Revision Source

<p>{{ CSSTutorialTOC() }}</p>

<p>本页用来描述创建用户界面的Mozilla特定语言。</p>

<p>你可以创建一个小型的DEMO在你的火狐浏览器中运行。</p>

<h3 id="Information:_User_interfaces" name="Information:_User_interfaces">Information: 用户界面</h3>

<p>虽然HTML已经差不多可以支持用户界面,但是HTML却不能完全支持创建一个独立程序(用户界面)时所需要的所有特性。</p>

<p>Mozilla 克服这方面的困难,创建了一种能用于创建用户界面的特定语言,这种语言就是:XUL&nbsp;(基于XML的用户界面描述语言, 通常这个单词发音类似于 "<em>zool</em>").</p>

<p>很多通用的与用户界面相关的特性都内建到了XULI中,n XUL,例如XUL除了提供类似于windows系统中的对话框和wizard外,还提供了状态栏、菜单、工具栏甚至是“浏览器&nbsp;”。</p>

<p>更多特定的功能可通过使用XUL与一些其他的技术(在CSS、JavaScript以及XBL bindings中有提到过)相结合实现,</p>

<p>与其他基于XML的语言一样,XUL也利用CSS来进行布局。</p>

<table style="background-color:#f4f4f4; border:1px solid #36b; margin-bottom:1em; padding:1em; width:100%">
 <caption style="font-weight:bold; text-align:left; margin-top:1em;">详见</caption>
 <tbody>
  <tr>
   <td>更多关于XUL方面的信息,请参看<a href="/en/XUL" title="en/XUL">XUL</a>&nbsp;页面</td>
  </tr>
 </tbody>
</table>

<h3 id="Action:_A_XUL_demonstration" name="Action:_A_XUL_demonstration">Action: 一个XUL案例</h3>

<p>用创建纯文本文档的形式创建一个新的XUL文档,并命名为doc7.xul,复制、粘贴下面的内容(滑动右侧的滚动条,确保把框内的内容复制完全)&nbsp;C</p>

<div style="width:48em; height:12em; overflow:auto;">
<pre>
&lt;?xml version="1.0"?&gt;
&lt;?xml-stylesheet type="text/css" href="style7.css"?&gt;
&lt;!DOCTYPE window&gt;

&lt;window
  xmlns="https://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
  title="CSS Getting Started - XUL demonstration"
  onload="init();"&gt;

&lt;script type="application/javascript" src="script7.js"/&gt;

&lt;label class="head-1" value="XUL demonstration"/&gt;

&lt;vbox&gt;

  &lt;groupbox class="demo-group"&gt;
    &lt;caption label="Day of week calculator"/&gt;
    &lt;grid&gt;
      &lt;columns&gt;
        &lt;column/&gt;
        &lt;column/&gt;
        &lt;/columns&gt;
      &lt;rows&gt;
        &lt;row&gt;
          &lt;label class="text-prompt" value="Date:" 
            accesskey="D" control="date-text"/&gt;
          &lt;textbox id="date-text" type="timed" 
            timeout="750" oncommand="refresh();"/&gt;
          &lt;/row&gt;
        &lt;row&gt;
          &lt;label value="Day:"/&gt;
          &lt;hbox id="day-box"&gt;
            &lt;label class="day" value="Sunday" disabled="true"/&gt;
            &lt;label class="day" value="Monday" disabled="true"/&gt;
            &lt;label class="day" value="Tuesday" disabled="true"/&gt;
            &lt;label class="day" value="Wednesday" disabled="true"/&gt;
            &lt;label class="day" value="Thursday" disabled="true"/&gt;
            &lt;label class="day" value="Friday" disabled="true"/&gt;
            &lt;label class="day" value="Saturday" disabled="true"/&gt;
            &lt;/hbox&gt;
          &lt;/row&gt;
        &lt;/rows&gt;
      &lt;/grid&gt;
    &lt;hbox class="buttons"&gt;
      &lt;button id="clear" label="Clear" accesskey="C" 
        oncommand="clearDate();"/&gt;
      &lt;button id="today" label="Today" accesskey="T"
        oncommand="setToday();"/&gt;
      &lt;/hbox&gt;
    &lt;/groupbox&gt;

  &lt;statusbar&gt;
    &lt;statusbarpanel id="status"/&gt;
    &lt;/statusbar&gt;

&lt;/vbox&gt;

&lt;/window&gt;
</pre>
</div>

<p>创建一个新的CSS文档,命名为style7.css,并复制、粘贴下列框内的内容。</p>

<div style="width:48em; height:12em; overflow:auto;">
<pre>
/*** XUL demonstration ***/
window {
  -moz-box-align: start;
  background-color: -moz-dialog;
  font: -moz-dialog;
  padding: 2em;
  }

.head-1 {
  font-weight: bold;
  font-size: 200%;
  padding-left: 5px;
  }


/* the group box */
.demo-group {
  padding: 1em;
  }

.demo-group grid {
  margin-bottom: 1em;
  }

.demo-group column {
  margin-right: .5em;
  }

.demo-group row {
  margin-bottom: .5em;
  }

.demo-group .buttons {
  -moz-box-pack: end;
  }


/* the day-of-week labels */
.day {
  margin-left: 1em;
  }

.day[disabled] {
  color: #777;
  }

.day:first-child {
  margin-left: 4px;
  }


/* the left column labels */
.text-prompt {
  padding-top: .25em;
  }


/* the date input box */
#date-text {
  max-width: 8em;
  }


/* the status bar */
statusbar {
  width: 100%;
  border: 1px inset -moz-dialog;
  margin: 4px;
  padding: 0px 4px;
  }

#status {
  padding: 4px;
  }

#status[warning] {
  color: red;
  }
</pre>
</div>

<p>创建一个新的CSS文档,命名为script7.js,e, <code>script7.js</code>. 并复制、粘贴下列框内的内容。</p>

<div style="width:48em; height:12em; overflow:auto;">
<pre>
// XUL demonstration

var dateBox, dayBox, currentDay, status; // elements

// called by window onLoad
function init() {
  dateBox = document.getElementById("date-text")
  dayBox = document.getElementById("day-box")
  status = document.getElementById("status")
  setToday();
  }

// called by Clear button
function clearDate() {
  dateBox.value = ""
  refresh()
  }

// called by Today button
function setToday() {
  var d = new Date()
  dateBox.value = (d.getMonth() + 1)
    + "/" + d.getDate()
    + "/" + d.getFullYear()
  refresh()
  }

// called by Date textbox
function refresh() {
  var d = dateBox.value
  var theDate = null

  showStatus(null)
  if (d&nbsp;!= "") {
    try {
      var a = d.split("/")
      var theDate = new Date(a[2], a[0] - 1, a[1])
      showStatus(theDate)
      }
    catch (ex) {}
    }
  setDay(theDate)
  }

// internal
function setDay(aDate) {
  if (currentDay) currentDay.setAttribute("disabled", "true")
  if (aDate == null) currentDay = null
  else {
    var d = aDate.getDay()
    currentDay = dayBox.firstChild
    while (d-- &gt; 0) currentDay = currentDay.nextSibling
    currentDay.removeAttribute("disabled")
    }
  dateBox.focus();
  }

function showStatus(aDate) {
  if (aDate == null) {
    status.removeAttribute("warning")
    status.setAttribute("label", "")
    }
  else if (aDate === false || isNaN(aDate.getTime())) {
    status.setAttribute("warning", "true")
    status.setAttribute("label", "Date is not valid")
    }
  else {
    status.removeAttribute("warning")
    status.setAttribute("label", aDate.toLocaleDateString())
    }
  }
</pre>
</div>

<p>To see the result exactly as intended, use the default theme in your browser. If you use a different theme, it changes some user-interface styles and the demonstration might look strange.</p>

<p>Open the document in your Mozilla browser and use the interface.</p>

<p>This wiki does not support XUL and JavaScript in pages, so it is not possible to show the demonstration here. It looks something like this:</p>

<table style="background-color:threedface; border:2px outset #36b; cursor:default; margin:.5em 0; padding:1em; white-space:nowrap">
 <tbody>
  <tr>
   <td>
    <p style="font-size:150%;font-weight:bold;margin:0;padding:0;">XUL demonstration</p>

    <div style="position:relative;border:2px groove threedhighlight;margin-top:1em;">
    <p style="float:left;margin:-1em 0 0 .5em;padding:0;background-color:threedface;">Day of week calculator</p>

    <table style="background-color:threedface; border-spacing:.25em; clear:left; margin:.5em; padding-right:.5em">
     <tbody>
      <tr>
       <td style="padding-right:.5em;"><u>D</u>ate:</td>
       <td style="background-color:white;border:1px solid #000;width:8em;float:left;cursor:text;padding:.15em .25em;">6/27/2005</td>
      </tr>
      <tr>
       <td>Day:</td>
       <td style="color:graytext;word-spacing:.5em;">Sunday <span style="color:#000">Monday</span> Tuesday Wednesday Thurdsay Friday Saturday</td>
      </tr>
      <tr>
       <td>&nbsp;</td>
       <td>
        <div style="float:right;margin-top:.5em;word-spacing:.5em;">
        <p><span style="border:2px outset threedface; padding:.25em 1em"><u>C</u>lear</span> <span style="border:2px outset threedface; padding:.25em 1em"><u>T</u>oday</span></p>
        </div>
       </td>
      </tr>
     </tbody>
    </table>
    </div>

    <div style="border:1px inset threedface;margin-top:1em;">
    <p style="margin:0;padding:.25em .5em;">June 27, 2005</p>
    </div>
   </td>
  </tr>
 </tbody>
</table>

<p>Notes about this demonstration:</p>

<ul>
 <li>The XUL document links the stylesheet as usual, and it also links the script.</li>
 <li>The script is not important in this demonstration.</li>
 <li>Much of the style that you see is determined by your browser's theme.</li>
</ul>

<p>Examine the document's stylesheet to ensure that you understand all the rules there. If there is a rule that you do not understand, comment it out and refresh your browser to see the effect on the document.</p>

<table style="background-color:#fffff4; border:1px solid #36b; margin-bottom:1em; padding:1em">
 <caption style="font-weight:bold; text-align:left;">Challenge</caption>
 <tbody>
  <tr>
   <td>Use the DOM Inspector tool to examine the Date textbox. It is made up of other elements that are generated by its XBL binding.
    <p>Discover the <em>class</em> of its <code>html:input</code> element. This is the element that actually receives user input.</p>

    <p>Using this knowledge, add a rule to the stylesheet that makes the background of the Date box pale blue when it has keyboard focus (but white when keyboard focus is somewhere else).</p>
   </td>
  </tr>
 </tbody>
</table>

<h4 id="What_next.3F" name="What_next.3F">What next?</h4>

<p>If you had difficulty understanding this page, or if you have other comments about it, please contribute to its <a href="/Talk:en/CSS/Getting_Started/XUL_user_interfaces" title="Talk:en/CSS/Getting_Started/XUL_user_interfaces">Discussion</a> page.</p>

<p>In this demonstration, you see the standard rectangular shapes that are common to most user interfaces. Mozilla also supports a specialized graphics language for creating shapes, using CSS stylesheets to specify the style. The next page demonstrates this: <a href="/en/CSS/Getting_Started/SVG_and_CSS" title="en/CSS/Getting_Started/SVG_and_CSS"><strong>SVG and CSS</strong></a>.</p>
Revert to this revision