keybinding.html

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

HTML
806
字号
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN"><html>    <head>        <title>How to Use Key Bindings (The Java&trade; Tutorials &gt;             Creating a GUI with JFC/Swing &gt; Using Other Swing Features)</title><style type="text/css">    .FigureCaption   {         margin-left: 1in;         margin-right: 1in;         font-family: sans-serif;         font-size: smaller;         text-align: justify;    }    #TopBar_bl {        background: url(../../images/java_bar_bl.gif) 0 100% no-repeat;        width: 100%;        height: 60px;    }    #TopBar_br {        background: url(../../images/java_bar_br.gif) 100% 100% no-repeat;        width: 100%;        height: 60px;    }    #TopBar_tl {        background: url(../../images/java_bar_tl.gif) 0 0 no-repeat;        width: 100%;        height: 60px;    }    #TopBar_tr {        background: url(../../images/java_bar_tr.gif) 100% 0 no-repeat;        width: 100%;        height: 60px;    }    #TopBar {        background: #35556B url(../../images/java_bar.gif);        margin: 10px 10px 0 10px;        height:60px;        min-width:700px;        color: white;        font-family: sans-serif;         font-weight: bold;    }    @media print {        #BreadCrumbs, #Download {            display: none;        }    }    #TopBar_right {        line-height: 14px;        float: right;        padding-top: 2px;        padding-right: 30px;        text-align: center;    }    @media print {        #TopBar_right {            display: none;        }    }    #TopBar_right a {        font-size: 12px;        margin: 3px;        padding: 0;    }    #TopBar a:visited, #TopBar a:link {        color: white;        text-decoration: none;    }    #TopBar a:hover, #TopBar a:active  {        background-color: white;        color: #35556B;    }    #BreadCrumbs {        padding: 4px 5px 0.5em 0;        font-family: sans-serif;         float: right;    }    #BreadCrumbs a {        color: blue;    }    #BreadCrumbs a:visited, #BreadCrumbs a:link {        text-decoration: none;    }    #BreadCrumbs a:hover, #BreadCrumbs a:active {        text-decoration: underline;    }    #PageTitle {        margin: 0 5px 0.5em 0;        color: #E76F00;        font-family: sans-serif;         font-weight: bold;        font-size: 20px;    }    .LeftBar_shown {        width: 13em;        float: left;        margin-left: 10px;        margin-top: 4px;        margin-bottom: 2em;    }    @media print {        .LeftBar_shown {            display: none;        }    }    .LeftBar_hidden {        display: none;    }    #Footer {        padding-top: 10px;        padding-left: 10px;        margin-right: 10px;    }    .NavBit  {        padding: 4px 5px 0.5em 0;        font-family: sans-serif;     }    @media print {        .NavBit {            display: none;        }    }    #TagNotes {        text-align: right;        font-size: smaller;        font-family: sans-serif;     }    @media print {        #TagNotes a:visited, #TagNotes a:link {            color: #35556B;            text-decoration: none;        }    }    #Contents a, .NavBit a, #TagNotes a {        color: blue    }    #TagNotes a:visited, #TagNotes a:link,    #Contents a:visited, #Contents a:link,    .NavBit a:visited, .NavBit a:link {        text-decoration: none;    }    #TagNotes a:hover, #TagNotes a:active,       #Contents a:hover, #Contents a:active,       .NavBit a:hover, .NavBit a:active {          text-decoration: underline;    }    #Contents {        float: left;        font-family: sans-serif;     }    @media print {        #Contents {            display: none;        }    }    @media screen {        div.PrintHeaders {            display: none;        }    }    .linkLESSON, .nolinkLESSON {        margin-left: 0.5em;        text-indent: -0.5em    }    .linkAHEAD, .nolinkAHEAD, .linkQUESTIONS, .nolinkQUESTIONS   {        margin-left: 1.5em;         text-indent: -0.5em    }    .linkBHEAD, .nolinkBHEAD   {        margin-left: 2.5em;        text-indent: -0.5em    }    .linkCHEAD, .nolinkCHEAD   {        margin-left: 3.5em;        text-indent: -0.5em    }    .nolinkLESSON, .nolinkAHEAD, .nolinkBHEAD, .nolinkCHEAD,    .nolinkQUESTIONS {        font-weight: bold;        color: #E76F00;    }    .MainFlow_indented {        margin-right: 10px;        margin-left: 15em;        margin-bottom: 2em;    }    .MainFlow_wide {        margin-right: 10px;        margin-left: 10px;        margin-bottom: 2em;    }    @media print {        .MainFlow_indented, .MainFlow_wide {            padding-top: 0;            margin-top: 10px;            margin-right: 10px;            margin-left: 0;        }    }    h3, h4, h5 {        color: #E76F00;        font-family: sans-serif;    }    #ToggleLeft {        display: none;    }    /t</style><script type="text/javascript">    function leftBar() {        var nameq = 'tutorial_showLeftBar='        var cookies = document.cookie.split(';');        for (var i = 0; i < cookies.length; i++) {            var cookieString = cookies[i];            while (cookieString.charAt(0) == ' ') {                cookieString = cookieString.substring(1, cookieString.length);            }            if (cookieString.indexOf(nameq) == 0) {                cookieValue =  cookieString.substring(nameq.length,                        cookieString.length);                return cookieValue == 'yes';            }        }        return true;    }    function showLeft(b) {        var contents = document.getElementById("LeftBar");        var main = document.getElementById("MainFlow");        var toggle = document.getElementById("ToggleLeft");        if (b) {            contents.className = "LeftBar_shown";            main.className = "MainFlow_indented";            toggle.innerHTML = "Hide the TOC";            document.cookie = 'tutorial_showLeftBar=yes; path=/';        } else {            contents.className = "LeftBar_hidden";            main.className = "MainFlow_wide";            toggle.innerHTML = "Show the TOC";            document.cookie = 'tutorial_showLeftBar=no; path=/';        }    }    function toggleLeft() {        showLeft(document.getElementById("LeftBar").className ==                "LeftBar_hidden");        document.getElementById("ToggleLeft").blur();    }    function load() {        showLeft(leftBar());        document.getElementById("ToggleLeft").style.display="inline";    }    </script>    </head><body onload="load()">    <div id=TopBar> <div id=TopBar_tr> <div id=TopBar_tl> <div id=TopBar_br> <div id=TopBar_bl>                         <div id=TopBar_right>                             <a target="_blank"                                href="http://java.sun.com/javase/6/download.jsp">Download                                the JDK</a>                            <br>                            <a href="../../search.html" target="_blank">Search the                                Tutorials</a>                            <br>                            <a href="javascript:toggleLeft()"                                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 Other Swing Features    </div>    <div id=LeftBar class=LeftBar_shown>        <div id=Contents>            <div class="linkLESSON"><a href="index.html">Using Other Swing Features</a></div><div class="linkAHEAD"><a href="action.html">How to Use Actions</a></div><div class="linkAHEAD"><a href="timer.html">How to Use Swing Timers</a></div><div class="linkAHEAD"><a href="access.html">How to Support Assistive Technologies</a></div><div class="linkAHEAD"><a href="focus.html">How to Use the Focus Subsystem</a></div><div class="nolinkAHEAD">How to Use Key Bindings</div><div class="linkAHEAD"><a href="problems.html">Solving Common Problems Using Other Swing Features</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 Other Swing Features</a>            </span>            <div class=NavBit>                <a target=_top href=focus.html>&laquo;&nbsp;Previous</a>&nbsp;&bull;&nbsp;<a target=_top href=../TOC.html>Trail</a>&nbsp;&bull;&nbsp;<a target=_top href=problems.html>Next&nbsp;&raquo;</a>            </div>            <div id=PageTitle>How to Use Key Bindings</div>            <blockquote>The <code>JComponent</code> classsupports key bindingsas a way of responding to individual keys typed by a user.Here are some examples of whenkey bindings are appropriate:<ul><li> You're creating a custom component     and want to support keyboard access to it.     <br>     For example, you might want the component to react     when it has the focus and the user presses the Space key.<p><li> You want to override the behavior     of an existing key binding.     <br>     For example, if your application normally reacts to presses of the F2 key     in a particular way,     you might want it to perform a different action or ignore the key press.<p><li> You want to provide a new key binding for an existing action.     <br>     For example,     you might feel strongly that     Control-Shift-Insert should perform a paste operation.</ul><p>You often don't need to use key bindings directly.They're used behind the scenes bymnemonics (supported by all buttons and by tabbed panes)and accelerators (supported by menu items).You can find coverage of mnemonics and accelerators in the section<a class="TutorialLink" target="_top" href="../components/menu.html#mnemonic">Enabling Keyboard Operation</a>.<p>An alternative to key bindings is using<a class="TutorialLink" target="_top" href="../events/keylistener.html">key listeners</a>.Key listeners have their placeas a low-level interface to keyboard input,but for responding to individual keyskey bindings are more appropriateand tend to result in more easily maintained code.Some of the advantages of key bindingsare they're somewhat self documenting,take the containment hierarchy into account,encourage reusable chunks of code (<code>Action</code> objects),and allow actions to be easily removed, customized, or shared.Also, they make it easy to change the keyto which an action is bound.<p>The rest of this section gives you the details you needto use key bindings:<ul><li> <a href="#maps">How Key Bindings Work</a><li> <a href="#howto">How to Make and Remove Key Bindings</a><li> <a href="#api">The Key Binding API</a><li> <a href="#eg">Examples that Use Key Bindings</a></ul></blockquote><h3><a name="maps">How Key Bindings Work</a></h3><blockquote>The key binding supportprovided by <code>JComponent</code>relies on the<a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/InputMap.html"><code>InputMap</code></a> and<a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/ActionMap.html"><code>ActionMap</code></a> classes,which were introduced in 1.3.An input map binds key strokes to action names,and an action map specifies the<a class="TutorialLink" target="_top" href="action.html">action</a> corresponding to each action name.<font color=green>[PENDING: Add this footnote:<br>Technically, you don't need to use action names in the maps;you can use any object as the "key" into the maps.By convention, however, you use a string that names an action.]</font><blockquote><hr><strong>Version Note:</strong>&nbsp;     Prior to 1.3, the <code>JComponent</code> method <code>registerKeyboardAction</code> was     used instead of input and action maps.     <code>registerKeyboardAction</code> is now obsolete.     (To ensure compatibility for older programs,     <code>registerKeyboardAction</code> was reimplemented     to use <code>InputMap</code> and <code>ActionMap</code>.)<hr></blockquote><p>

⌨️ 快捷键说明

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