access.html

来自「jsf、swing的官方指南」· HTML 代码 · 共 1,331 行 · 第 1/4 页

HTML
1,331
字号
using the Accessibility API,which is defined in the<a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/accessibility/package-summary.html"><code>javax.accessibility</code></a> package.<p>Because support for the Accessibility APIis built into the Swing components,your Swing program will probably work just finewith assistive technologies, even if you do nothing special.For example, assistive technologies can automaticallyget the text information that is set by the following lines of code:<blockquote><pre>JButton button = new JButton("I'm a Swing button!");label = new JLabel(labelPrefix + "0    ");label.setText(labelPrefix + numClicks);JFrame frame = new JFrame("SwingApplication");</pre></blockquote>Assistive technologies can also grabthe tool-tip text (if any)associated with a componentand use it to describe the component to the user.<p>Making your program function smoothly with assistivetechnologies is easy to do and, in the UnitedStates, may be required by federal law.For more information see<a class="OutsideLink" target="_blank" href="http://www.sun.com/access/background/laws.html">Global Legal Resources for IT Related Accessibility Issues</a>.<p>The rest of this section covers these topics:<ul><li><a href="#supporting">Rules for Supporting Accessibility</a><li><a href="#testing">Testing for Accessibility</a><li><a href="#namesdescriptions">Setting    Accessible Names and Descriptions on Components</a><li><a href="#developing">Concepts: How Accessibility Works</a> <li><a href="#accessiblecomponents">Making Custom Components Accessible</a><li><a href="#api">The Accessibility API</a><li><a href="#eg">Examples that Use the Accessibility API</a></ul><a name="supporting"></blockquote><h3>Rules for Supporting Accessibility</h3></a><blockquote>Here are a few things you can do to make your programwork as well as possible with assistive technologies:<ul><li> If a component doesn't display a short string     (which serves as its default name),     specify a name with the <code>setAccessibleName</code> method.     You might want to do this for image-only buttons,      panels that provide logical groupings, text areas, and so on.<p><li> Set <a class="TutorialLink" target="_top" href="../components/tooltip.html">tool tip</a>     text for components     whenever it makes sense to do so.     For example:<blockquote><pre>aJComponent.setToolTipText(     "Clicking this component causes XYZ to happen.");</pre></blockquote><p><li> If you don't want to provide a tool tip for a component,     use the <code>setAccessibleDescription</code> method      to provide a description that assistive technologies can give the user.     For example:<blockquote><pre>aJComponent.getAccessibleContext().    setAccessibleDescription(    "Clicking this component causes XYZ to happen.");</pre></blockquote><p><li> Specify keyboard alternatives wherever possible.     Make sure you can use your program     with only the keyboard.     Try hiding your mouse!     Note that if the focus is in an editable text component,      you can use Shift-Tab to move focus to the next component.     <p>     Support for keyboard alternatives varies by component.<a class="TutorialLink" target="_top" href="../components/button.html">Buttons</a>     support keyboard alternatives with the <code>setMnemonic</code>     method.     Menus inherit the button mnemonic support     and also support accelerators,     as described in <a class="TutorialLink" target="_top" href="../components/menu.html#mnemonic">Enabling Keyboard Operation</a>. Other components can use<a class="TutorialLink" target="_top" href="../components/jcomponent.html#keyboardAction">key bindings</a> to associate user typing with program actions.<p><li> Assign a textual description to all     <a href="../components/icon.html"><code>ImageIcon</code></a>     objects in your program.  You can set this property     by using either the <code>setDescription</code>     method or one of the <code>String</code>     forms of the <code>ImageIcon</code> constructors.    <p><li> If a bunch of components form a logical group,     try to put them into one container.     For example, use a<a class="TutorialLink" target="_top" href="../components/panel.html"><code>JPanel</code></a>     to contain     all the radio buttons in a radio button group.<p><li> Whenever you have a<a class="TutorialLink" target="_top" href="../components/label.html">label</a>     that describes another component,     use the <code>setLabelFor</code> method     so that assistive technologies can find the component     that the label is associated with.     This is especially important when the label      displays a mnemonic      for another component (such as a text field).<p><li> If you create a custom component,     make sure it supports accessibility.     In particular, be aware that     subclasses of <code>JComponent</code>     are not automatically accessible.     Custom components that are     descendants of other Swing components should override     inherited accessibility information as necessary.     For more information, see     <a href="#developing">Concepts: How Accessibility Works</a>     and     <a href="#accessiblecomponents">Making Custom Components Accessible</a>.<p><li> Use the examples provided with the accessibility utilities     to test your program.     Although the primary purpose of these examples is to show programmers     how to use the Accessibility API when implementing assistive technologies,     these examples are also quite useful     for testing application programs for accessibility.     <a href="#testing">Testing for Accessibility</a>     shows <code>ScrollDemo</code> running with Monkey&#151one     of the accessibility utilities examples.     Monkey shows the tree of accessible components in a program and     lets you interact with those components.<p><li> Finally, don't break what you get for free!     If your GUI has an inaccessible container&#151for     example, your own subclass of <code>Container</code>     or <code>JComponent</code>     or any other container that doesn't implement     the <code>Accessible</code> interface&#151any     components inside that container become     inaccessible.</ul><a name="testing"><h3>Testing for Accessibility</h3></a><blockquote>The examples that come with the accessibility utilitiescan give you an idea of how accessible your program is.For instructions on getting these utilities, see the<a class="OutsideLink" target="_blank" href="http://java.sun.com/products/jfc/accessibility/">Accessibility home page</a>.Follow the instructions in the accessibility utilities documentationfor setting up the Java Virtual Machine (VM)to run one or more of the utilities automatically.<p>Let's use an accessibility utility to compare the original version of one of our demos to a version in which the rulesfor supporting accessibility have been applied.Here's a picture of a program called <code>ScrollDemo</code>.<p><center><IMG SRC="../../figures/uiswing/components/ScrollDemo.gif" WIDTH="350" HEIGHT="340" ALIGN="BOTTOM" ALT="A snapshot of the original, inaccessible ScrollDemo."></center></p><blockquote><hr><strong>Try this:</strong>&nbsp;<ol><li><a href="http://java.sun.com/docs/books/tutorialJWS/uiswing/components/examples/ScrollDemo.jnlp">Run ScrollDemo</a>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 class="TutorialLink" target="_top" href="../components/examples/index.html#ScrollDemo">example index for components</a>.<li>Next, <a href="http://java.sun.com/docs/books/tutorialJWS/uiswing/misc/examples/AccessibleScrollDemo.jnlp">run AccessibleScrollDemo</a>using Java Web Start.Or, to compile and run the example yourself, consult the<a href="examples/index.html#AccessibleScrollDemo">example index</a>.<li> Compare the two versions side by side.     The only noticeable difference is that     the <strong>cm</strong> toggle button     and the photograph have tool tips in the accessible version.<li> Now run the two versions under the accessibility utility     called Monkey.     Note that when the accessibility tools have been     downloaded and configured in the     <code>accessibility.properties</code> file, the Monkey     window automatically comes up when you click on the     Run ScrollDemo and AccessibleScrollDemo links     (in steps 1 and 2).     <p>     If the Monkey window does not appear on startup,     the problem may be that the <code>accessibility.properties</code>     file is not present in the version of the VM being used     by Java Web Start.  You can change the VM you use by     running the Java Web Start     <a href="../../information/player.jnlp">Application Manager</a> and     selecting <b>File > Preferences > Java</b>.     <p> <li> Note that when the Monkey window comes up you need to select     <b>File > Refresh Trees</b> to see information appear under     <code>Accessible Tree</code>.  You can then expand the tree     by successively clicking on the horizontal icons displayed     by each folder icon.  When the tree has been expanded,     you can see detailed information for the various components.     The custom components (rules and corners)     that weren't accessible in the original version     are accessible in the modified version.     This can make quite a difference to assistive technologies.</ol><hr></blockquote><p>Here's a snapshot of Monkey running on <code>ScrollDemo</code>:<p><center><IMG SRC="../../figures/uiswing/misc/InaccessibleMonkey.gif" WIDTH="514" HEIGHT="338" ALIGN="BOTTOM" ALT="Monkey running on inaccessible version of ScrollDemo."></center></p><h4 align=center><font color=red>[PENDING: recapture the output.]</font></h4><p>The left side of the split pane showsthe actual component hierarchy for the program.The right side shows the accessible components in the hierarchy,which is what interests us.<p>The first thing to notice is that,even with no explicit support in <code>ScrollDemo</code>,Monkey is able to discover a lot of information aboutthe various components in the program.Most of the components and their children appear in the tree.However, the names for most of thecomponents are empty (null), which is rather unhelpful.The descriptions are also empty.<p>Further trouble comes withthe program's custom components.The two rulers are inaccessible,so they are not included in the accessible tree.The viewports that contain the rulers are displayed as leaf nodesbecause they have no accessible children.The custom corners are also missing from the accessible tree.<p>Now here's a picture of the Monkey window for<code>AccessibleScrollDemo</code>:<p><center><IMG SRC="../../figures/uiswing/misc/Monkey.gif" WIDTH="513" HEIGHT="415" ALIGN="BOTTOM" ALT="Monkey running on accessible version of ScrollDemo."></center></p><h4 align=center><font color=red>[PENDING: recapture the output.]</font></h4><br>The rules are now listed as children of the viewports,and the corners are listed as children of the scroll pane.Furthermore, many of the components now have non-null names.<p>In the previous snapshot of Monkey,the Column Header item is selected.Monkey highlights the corresponding componentin <code>ScrollDemo</code> program.<p><center><IMG SRC="../../figures/uiswing/misc/ScrollDemoWithRulerSelected.gif" WIDTH="350" HEIGHT="320" ALIGN="BOTTOM" ALT="ScrollDemo shown when Column Header selected in Monkey."></center></p><h4 align=center><font color=red>[PENDING: new screenshot needed]</font></h4>When an item is selected,you can use Monkey's <b>Panels</b> menuto bring up one of four different panelsthat let you interact with the selected component.Choosing<strong>Panels > Accessibility API panel</strong> brings up a panel like the oneshown in the following figure.This panel displays informationavailable through methods definedin the <code>AccessibleContext</code> base classand the <code>AccessibleComponent</code> interface.<p><center><IMG SRC="../../figures/uiswing/misc/APIPanelForColumnHeader.gif" WIDTH="357" HEIGHT="301" ALIGN="BOTTOM" ALT="Accessibility API Panel for Column Header."></center></p>Monkey has three other panels:<ul><li> <strong>AccessibleAction</strong>&#151     Shows the actions supported by an accessible component     and lets you invoke the action.     Works only with an accessible component whose context implements     the <code>AccessibleAction</code> interface.<li> <strong>AccessibleSelection</strong>&#151     Shows the current selection of an accessible component     and lets you manipulate the selection.     Works only with accessible component whose context implements     the <code>AccessibleSelection</code> interface.<li> <strong>AccessibleHypertext</strong>&#151     Shows any hyperlinks contained within an accessible component     and lets you traverse them.     Works only with accessible component whose context implements     the <code>AccessibleHypertext</code> interface.</ul>The accessibility utilities examplesare handy as testing toolsand can give you an idea of how accessiblethe components in your program are.However, even if your components behave wellin Monkey or the other examples,they still might not be completely accessible becauseMonkey and the other examples exercise only certainportions of the Accessibility API.The only true test of accessibility is to run your programswith real-world assistive technologies.

⌨️ 快捷键说明

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