3

いくつかのナビゲーション キーを再マップしようとしています。

  • ENTER: 標準の TAB 動作のように動作します (次のコントロールにフォーカス)
  • SHIFT+ENTER: SHIFT+TAB のように動作します (前のコントロールにフォーカス)
  • 上 / 下矢印: 前 / 次のコントロール

いくつかのオプションを試してみましたが、運が悪かった:

from javax.swing import *
from java.awt import *

class JTextFieldX(JTextField):

    def __init__(self, *args):
        # Thanks, Jack!!
        JTextField.__init__(
            self,
            focusGained=self.onGotFocus,
            focusLost=self.onLostFocus,
            *args)

    def onGotFocus (self, event):
        print "onGotFocus "
        self.selectionStart = 0
        self.selectionEnd = len(self.text)

    def onLostFocus (self, event):
        print "onLostFocus ", self.name


class Test(JFrame):
    def __init__(self):
        JFrame.__init__(self,
                        'JDesktopPane and JInternalFrame Demo',
                        size=(600, 300),
                        defaultCloseOperation=JFrame.EXIT_ON_CLOSE)

        self.desktop = JDesktopPane()
        self.contentPane.add(JScrollPane(self.desktop)) # This is the same as self.getContentPane().add(...)

        frame = JInternalFrame("Frame", 1, 1, 1, 1, size=(400, 400), visible=1)
        panel = JPanel()

        self.label = JLabel('Hello from Jython')
        panel.add(self.label)

        self.textfield1 = JTextFieldX('Type something here', 15)
        panel.add(self.textfield1)

        self.textfield2 = JTextFieldX('and click Copy', 15)
        panel.add(self.textfield2)

        panel.add(copyButton)

        frame.add(panel)
        frame.pack()
        self.desktop.add(frame)

        # ENTER=SPACE remapping for buttons (works ok, but only for buttons)
#        inputMap = UIManager.getDefaults().get("Button.focusInputMap")
#        pressedAction = inputMap.get(KeyStroke.getKeyStroke("pressed SPACE"));
#        releasedAction = inputMap.get(KeyStroke.getKeyStroke("released SPACE"));
#        # pressedAction = self.noAction
#        inputMap.put (KeyStroke.getKeyStroke("pressed ENTER"), pressedAction)
#        inputMap.put (KeyStroke.getKeyStroke("released ENTER"), releasedAction)

#        # Attemp to remap ENTER=TAB for TextFields (didn't work, no errors)
#        inputMap = UIManager.getDefaults().get("TextField.focusInputMap")
#        pressedAction = inputMap.get(KeyStroke.getKeyStroke("pressed TAB"));
#        releasedAction = inputMap.get(KeyStroke.getKeyStroke("released TAB"));
#        inputMap.put (KeyStroke.getKeyStroke("pressed W"), pressedAction)
#        inputMap.put (KeyStroke.getKeyStroke("released W"), releasedAction)

#        # Attemp to remap ENTER=TAB for all controls (didn't work, no errors)
#        spaceMap = self.textfield1.getInputMap().get(KeyStroke.getKeyStroke(event.KeyEvent.VK_TAB, 0, True));
#        self.textfield1.getInputMap().put(KeyStroke.getKeyStroke(event.KeyEvent.VK_ENTER, 0, True),spaceMap);

        frame.setSelected(1)
        frame.moveToFront()

    def noAction (self, event):
        print "noAction"
        pass

if __name__ == '__main__':
    test = Test()
    test.setLocation(100, 100)
    test.show()
4

3 に答える 3

1

読みやすくするために新しい投稿を作成しました。

self.textfield1 = JTextField('Type something here',15,focusGained=self.myOnFocus,keyPressed=self.myOnKey)

#create textfield2...must be created before can be referenced below.

self.textfield1.setNextFocusableComponent(self.textfield2)

次に、イベントハンドラーで:

def myOnKey(self,event):
    print str(event) # see all other info you can get.
    key_code = event.keyCode
    if key_code == 10:
        print "you pressed enter"
        # simulate the "tab" just focus next textbox...
        gotFocus = event.getComponent()
        nextToFocus = gotFocus.nextFocusableComponent
        nextToFocus.requestFocus()

やるべきです。

于 2009-07-31T19:05:38.367 に答える
1

最後に、ジャックの回答の一部 (keyPressed イベント) を使用しましたが、手動で setNextFocusableComponent を設定しませんでした:

keyFocusMgr = KeyboardFocusManager.getCurrentKeyboardFocusManager()
keyFocusMgr.focusNextComponent()
于 2009-08-01T16:39:01.453 に答える
0

キー押下を聞きたいスイングコンピテントに keyPressed を追加

self.textfield1 = JTextField('Type something here',15,focusGained=self.myOnFocus,keyPressed=self.myOnKey)

myOnKey は、そのメソッドで次のように任意の名前を付けることができます。

def myOnKey(self,event):
    print str(event) # see all other info you can get.
    key_code = event.keyCode
    if key_code == 10:
        print "you pressed enter"
        # simulate the "tab" by just focusing the next textbox...

次に、 print str(event) コマンドをいじって、必要なすべての適切なキーコードを取得できるようにする必要があります。

于 2009-07-31T17:37:08.733 に答える