2

Jython 2.5

我正在尝试将方法绑定到 JText 控件的 focusGained 事件,但我发现的所有示例都是 Java 示例,而不是 Jython。这是代码,我想在每个文本控件获得焦点时运行一个自定义方法(例如,选择所有控件的文本)

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

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 = JTextField('Type something here',15)
        # self.textfield1.addFocusListener(event.FocusListener())  # ???

        panel.add(self.textfield1)

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

        copyButton = JButton('Copy',actionPerformed=self.noAction)
        panel.add(copyButton)

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

        frame.setSelected(1)
        frame.moveToFront()

    def noAction (self, event):
        pass

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

2 回答 2

1

我昨天只是想自己解决这个问题……经过测试并且有效:

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

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 = JTextField('Type something here',15,focusGained=self.myOnFocus)


        panel.add(self.textfield1)

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

        copyButton = JButton('Copy',actionPerformed=self.noAction)
        panel.add(copyButton)

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

        frame.setSelected(1)
        frame.moveToFront()

    def myOnFocus(self,event):
        print "testing..."

    def noAction (self, event):
        pass

if __name__ == '__main__':
    test = Test()
    test.setLocation(100, 100)
    test.show()
于 2009-07-31T15:50:57.543 回答
1

很酷很简单。谢谢!我认为成语是

{var} = {constructor}({param}, {event}={function})
tf = JTextField('1.23', focusLost=tf_focus_lost)

其他选择:

from java.awt.event import FocusListener
class Enfoque(FocusListener):
    '''Add dynamically'''
# Left unimplemented
#    def focusGained(self, event):
#        print 'tf_b Enfoque.focusGained'

    def focusLost(self,event):     
        print 'tf_b Enfoque.focusLost'

enf = Enfoque()

tf_b = JTextField('2.34')
tf_b.addFocusListener(enf)
于 2011-07-13T17:33:09.693 回答