⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 model.html

📁 jsf、swing的官方指南
💻 HTML
📖 第 1 页 / 共 2 页
字号:
                                id="ToggleLeft">Hide the TOC</a>                        </div>                    </div> </div> </div> </div> </div>    <div class=PrintHeaders>        <b>Trail:</b> Creating a GUI with JFC/Swing        <br><b>Lesson:</b> Using Swing Components    </div>    <div id=LeftBar class=LeftBar_shown>        <div id=Contents>            <div class="linkLESSON"><a href="index.html">Using Swing Components</a></div><div class="linkAHEAD"><a href="components.html">A Visual Index to the Swing Components</a></div><div class="linkAHEAD"><a href="html.html">Using HTML in Swing Components</a></div><div class="linkAHEAD"><a href="toplevel.html">Using Top-Level Containers</a></div><div class="nolinkAHEAD">Using Models</div><div class="linkAHEAD"><a href="jcomponent.html">The JComponent Class</a></div><div class="linkAHEAD"><a href="text.html">Using Text Components</a></div><div class="linkBHEAD"><a href="generaltext.html">Text Component Features</a></div><div class="linkBHEAD"><a href="textapi.html">The Text Component API</a></div><div class="linkAHEAD"><a href="componentlist.html">How to Use Various Components</a></div><div class="linkBHEAD"><a href="applet.html">How to Make Applets</a></div><div class="linkBHEAD"><a href="button.html">How to Use Buttons, Check Boxes, and Radio Buttons</a></div><div class="linkBHEAD"><a href="colorchooser.html">How to Use Color Choosers</a></div><div class="linkBHEAD"><a href="combobox.html">How to Use Combo Boxes</a></div><div class="linkBHEAD"><a href="dialog.html">How to Make Dialogs</a></div><div class="linkBHEAD"><a href="editorpane.html">How to Use Editor Panes and Text Panes</a></div><div class="linkBHEAD"><a href="filechooser.html">How to Use File Choosers</a></div><div class="linkBHEAD"><a href="formattedtextfield.html">How to Use Formatted Text Fields</a></div><div class="linkBHEAD"><a href="frame.html">How to Make Frames (Main Windows)</a></div><div class="linkBHEAD"><a href="internalframe.html">How to Use Internal Frames</a></div><div class="linkBHEAD"><a href="label.html">How to Use Labels</a></div><div class="linkBHEAD"><a href="layeredpane.html">How to Use Layered Panes</a></div><div class="linkBHEAD"><a href="list.html">How to Use Lists</a></div><div class="linkBHEAD"><a href="menu.html">How to Use Menus</a></div><div class="linkBHEAD"><a href="panel.html">How to Use Panels</a></div><div class="linkBHEAD"><a href="passwordfield.html">How to Use Password Fields</a></div><div class="linkBHEAD"><a href="progress.html">How to Use Progress Bars</a></div><div class="linkBHEAD"><a href="rootpane.html">How to Use Root Panes</a></div><div class="linkBHEAD"><a href="scrollpane.html">How to Use Scroll Panes</a></div><div class="linkBHEAD"><a href="separator.html">How to Use Separators</a></div><div class="linkBHEAD"><a href="slider.html">How to Use Sliders</a></div><div class="linkBHEAD"><a href="spinner.html">How to Use Spinners</a></div><div class="linkBHEAD"><a href="splitpane.html">How to Use Split Panes</a></div><div class="linkBHEAD"><a href="tabbedpane.html">How to Use Tabbed Panes</a></div><div class="linkBHEAD"><a href="table.html">How to Use Tables</a></div><div class="linkBHEAD"><a href="textarea.html">How to Use Text Areas</a></div><div class="linkBHEAD"><a href="textfield.html">How to Use Text Fields</a></div><div class="linkBHEAD"><a href="toolbar.html">How to Use Tool Bars</a></div><div class="linkBHEAD"><a href="tooltip.html">How to Use Tool Tips</a></div><div class="linkBHEAD"><a href="tree.html">How to Use Trees</a></div><div class="linkAHEAD"><a href="icon.html">How to Use Icons</a></div><div class="linkAHEAD"><a href="border.html">How to Use Borders</a></div><div class="linkAHEAD"><a href="problems.html">Solving Common Component Problems</a></div></div>    </div>    <div id=MainFlow class=MainFlow_indented>            <span id=BreadCrumbs>                <a href=../../index.html target=_top>Home Page</a>                &gt;                <a href=../index.html target=_top>Creating a GUI with JFC/Swing</a>                &gt;                <a href=index.html target=_top>Using Swing Components</a>            </span>            <div class=NavBit>                <a target=_top href=toplevel.html>&laquo;&nbsp;Previous</a>&nbsp;&bull;&nbsp;<a target=_top href=../TOC.html>Trail</a>&nbsp;&bull;&nbsp;<a target=_top href=jcomponent.html>Next&nbsp;&raquo;</a>            </div>            <div id=PageTitle>Using Models</div>            <blockquote>Most noncontainer Swing components have models.A button (<code>JButton</code>), for example,has a model (a <code>ButtonModel</code> object)that stores the button's state &#151;what its keyboard mnemonic is,whether it's enabled, selected, or pressed,and so on.Some components have multiple models.A list (<code>JList</code>), for example,uses a <code>ListModel</code>to hold the list's contents,and a <code>ListSelectionModel</code>to track the list's current selection.<p>You often don't need to know about the modelsthat a component uses.For example, programs that use buttonsusually deal directly with the <code>JButton</code> object,and don't deal at all with the <code>ButtonModel</code> object.<p>Why then do models exist?The biggest reason is that they give youflexibility in determining how data is stored and retrieved.For example,if you're designing a spreadsheet applicationthat displays data in a sparsely populated table,you can create your own table modelthat is optimized for such use.<p>Models have other benefits, too.They mean that data isn't copied betweena program's data structuresand those of the Swing components.Also, models automaticallypropagate changes to all interested listeners,making it easy for the GUI to stay in sync with the data.For example, to add items to a listyou can invoke methods on the list model.When the model's data changes,the model fires events to the <code>JList</code> and any other registered listeners,and the GUI is updated accordingly.<p>Although Swing's model architectureis sometimes referred to as aModel-View-Controller (MVC) design,it really isn't.Swing components are generally implementedso that the view and controller are indivisible,implemented by a single UI objectprovided by the look and feel.The Swing model architecture is more accurately described as a<em>separable model architecture</em>.If you're interested in learning more about the Swing model architecture, see<a class="OutsideLink" target="_blank" href="http://java.sun.com/products/jfc/tsc/articles/architecture/">A Swing Architecture Overview</a>, an article in<em>The Swing Connection</em>.</blockquote><h3><a name="converter">An Example: Converter</a></h3><blockquote>This section features an example called Converter,which is an application that continuously converts distance measurementsbetween metric and U.S. units.You can<a href="http://java.sun.com/docs/books/tutorialJWS/uiswing/components/examples/Converter.jnlp"><b>run Converter</b></a> (it requires release 6) using<a class="TutorialLink" target="_top" href="../../information/javawebstart.html">Java<sup><font size=-2>TM</font></sup> Web Start</a>.    Or, to compile and run the example yourself,consult the<a href="examples/index.html#Converter">example index</a>.<p>As the following picture shows,Converter features two sliders,each tied to a text field.The sliders and text fields all display the same data&#151; a distance &#151; but using two different units of measure.<p><center><IMG SRC="../../figures/uiswing/components/ConverterMetal.png" WIDTH="288" HEIGHT="198" ALIGN="BOTTOM" ALT="Converter screenshot in the Metal look and feel"></center></p><p>The important thing for this programis ensuring that only one modelcontrols the value of the data.There are various ways to achieve this;we did it by deferring to the top slider's model.The bottom slider's model(an instance of a custom class called <code>FollowerRangeModel</code>)forwards all data queries to the top slider's model(an instance of a custom class called <code>ConverterRangeModel</code>).Each text field is kept in sync with its slider,and vice versa,by event handlers that listen for changes in value.Care is taken to ensure that the top slider's modelhas the final say about what distance is displayed.<p>When we started implementing the custom slider models,we first looked atthe API section of<a class="TutorialLink" target="_top" href="../components/slider.html">How to Use Sliders</a>. It informed us that all slider data models mustimplement the <code>BoundedRangeModel</code> interface.The<a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/BoundedRangeModel.html"><code>BoundedRangeModel</code> API documentation</a> tells usthat the interface has an implementing class named<code>DefaultBoundedRangeModel</code>.The<a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/DefaultBoundedRangeModel.html">API documentation for <code>DefaultBoundedRangeModel</code></a> shows that it's a general-purpose implementationof <code>BoundedRangeModel</code>.<p>We didn't use <code>DefaultBoundedRangeModel</code>directly because it stores data as integers,and Converter uses floating-point data.Thus, we implemented<code>ConverterRangeModel</code> as a subclass of<code>Object</code>.We then implemented <code>FollowerRangeModel</code>as a subclass of <code>ConverterRangeModel</code>.</blockquote><h3>For Further Information</h3><blockquote><p>To find out about the models for individual components,see the<a href="componentlist.html">"How to"</a> pagesand API documentationfor individual components.Here are some of our examples that use models directly:<ul><li> All but the simplest of the     <a href="table.html#eg">table examples</a>     implement custom table data models.<li> The <a href="colorchooser.html#eg">color chooser demos</a>     have change listeners on the color chooser's selection model     so they can be notified when the user selects a new color.     In ColorChooserDemo2, the <code>CrayonPanel</code> class     directly uses the color selection model     to set the current color.<li> The     <a href="examples/index.html#DynamicTreeDemo">DynamicTreeDemo</a>     example sets the tree model     (to an instance of <code>DefaultTreeModel</code>),     interacts directly with it,     and listens for changes to it.<li> <a href="examples/index.html#ListDemo">ListDemo</a>     sets the list data model     (to an instance of <code>DefaultListModel</code>)     and interacts directly with it.<li> <a href="examples/index.html#SharedModelDemo">SharedModelDemo</a>     defines a <code>SharedDataModel</code> class that extends     <code>DefaultListModel</code> and implements     <code>TableModel</code>.     A <code>JList</code> and  <code>JTable</code>     share an instance of <code>SharedDataModel</code>,     providing different views of the model's data.<li> In the event listener examples,<a class="TutorialLink" target="_top" href="../events/examples/index.html#ListDataEventDemo">ListDataEventDemo</a>     creates and uses a <code>DefaultListModel</code> directly.<li> Our <a href="spinner.html#eg">spinner examples</a>     create spinner models.<li> As you've already seen, the     <a href="examples/index.html#Converter">Converter</a>     example defines     two custom slider models.</ul>        </blockquote>        <div class=NavBit>            <a target=_top href=toplevel.html>&laquo; Previous</a>            &bull;            <a target=_top href=../TOC.html>Trail</a>            &bull;            <a target=_top href=jcomponent.html>Next &raquo;</a>        </div>    </div>    <div id=Footer><div id=TagNotes>    Problems with the examples? Try <a target="_blank"        href=../../information/run-examples.html>Compiling and Running        the Examples: FAQs</a>.    <br>    Complaints? Compliments? Suggestions? <a target="_blank"        href="http://developer.sun.com/contact/tutorial_feedback.jsp">Give    us your feedback</a>.<br><br>    <a target="_blank" href="../../information/copyright.html">Copyright</a>    1995-2006 Sun Microsystems, Inc.  All rights reserved.    <span id=Download></span></div>     </div>    <div class=PrintHeaders>        <b>Previous page:</b> Using Top-Level Containers        <br><b>Next page:</b> The JComponent Class    </div>    </body></html> 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -