简体   繁体   English

键绑定与setMnemonic,setDisplayedMnemonic和setLabelFor

[英]Key bindings vs. setMnemonic, setDisplayedMnemonic, and setLabelFor

I feel like a kid in a candy store--in trying yet again to get a handle on key bindings I stumbled onto setMnemonic() (in Netbeans form designer and its ton of generated code) and [especially!] the setDisplayedMnemonic() / setActionFor() pair (from Googling "can jlabel have mnemonic"), which seems a godsend. 我感觉就像糖果店里的孩子一样-再次尝试处理键绑定时,我偶然发现了setMnemonic() (在Netbeans表单设计器及其大量生成的代码中)和[尤其是!] setDisplayedMnemonic() / setActionFor()对(来自谷歌搜索“可以让jlabel有助记符”),这似乎是天赐之物。

All I know is that the following code just made my latest app a whole lot more user friendly in that it has rendered mouse/touchpad unnecessary. 我所知道的是,以下代码仅使我的最新应用更加用户友好,因为它无需使用鼠标/触摸板。

btnRemoveScratchWords.setMnemonic(VK_C);
btnSearch.setMnemonic(VK_H);
btnClearOutput.setMnemonic(VK_O);
btnExit.setMnemonic(VK_X);
btnHelp.setMnemonic(VK_H);

lblPattern.setDisplayedMnemonic(VK_P);
lblPattern.setLabelFor(txtPattern);

lblLegal.setDisplayedMnemonic(VK_L);
lblLegal.setLabelFor(txtLegal);

lblMust.setDisplayedMnemonic(VK_R);
lblMust.setLabelFor(txtRequiredLetters);

lblMinimumPointsPerLetter.setDisplayedMnemonic(VK_R);
lblMinimumPointsPerLetter.setLabelFor(txtMinScore);

But is key bindings a better way to do this? 但是,键绑定是一种更好的方法吗?

I'm gonna answer my own question. 我要回答我自己的问题。 If I've made any errors or misinterpreted or misinformed, I hope to find out from someone. 如果我犯了任何错误或被误解或误传,我希望能从某人身上找到答案。 Question in advance: Did I mess anything up below? 预先提问:我在下面弄乱了什么吗?

Key bindings provide the only way to do some things. 按键绑定是做某些事情的唯一方法。 For instance... 例如...

I needed a keypress to cause the bottom line of a textarea to be displayed regardless of which component has the focus and to then select the contents of the 'main' textbox. 我需要按键以使文本区域的底行得以显示,而不管哪个组件具有焦点,然后选择“主要”文本框的内容。

The statements below link the physical F2 keypress to a logical button on the form, whose action will be defined in a class (named JumpToEndOfOutput ) that extends AbstractAction , as required by getActionMap . 下面的语句将物理F2按键链接到表单上的逻辑按钮,该按钮将在getActionMap要求的扩展AbstractAction的类(名为JumpToEndOfOutput )中定义。

   txaOutput.getInputMap(WHEN_IN_FOCUSED_WINDOW).put(getKeyStroke("F2"),
                                "jumpToEndOfOutput");
   txaOutput.getActionMap().put("jumpToEndOfOutput", jumpToEndOfOutput);

(note use of WHEN_IN_FOCUSED_WINDOW , without which F2 can't always do what's needed): (请注意,使用WHEN_IN_FOCUSED_WINDOW ,否则F2不能总是做所需的事情):

// in constructor for form... make action listener for button

   btnJumpToEndOfOutput.addActionListener( new ButtonListener() );

... ...

// inner class avoids anonymous inner class, for clarity

    class ButtonListener implements ActionListener // simulates click of logical form button
    {
        public void actionPerformed( ActionEvent bp )
        {    
          txaOutput.selectAll();
          txtPattern.grabFocus();
          txtPattern.select(0, 99);
        } 
    } 

... ...

// back to constructor... make action object to listen for F2 keystroke

      JumpToEndOfOutput jumpToEndOfOutput = new JumpToEndOfOutput();

... ...

// class required for getActionMap

   class JumpToEndOfOutput extends AbstractAction // catches physical F2 keystroke
    {
        public void actionPerformed(ActionEvent e) 
        {
           btnJumpToEndOfOutput.doClick(); 
        } 
    }

Is there a shorter way to accomplish this? 有没有更短的方法来做到这一点?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM