Skip to content

Instantly share code, notes, and snippets.

@alcidesfp
Created April 24, 2012 13:42
Show Gist options
  • Save alcidesfp/2479764 to your computer and use it in GitHub Desktop.
Save alcidesfp/2479764 to your computer and use it in GitHub Desktop.
Ejemplo Swing Jython
#! /usr/bin/env jython
#-*- coding: utf-8; mode: Jython -*-
"Demuestra el uso de clases Java Swing en Jython"
from java.awt import BorderLayout
from javax import swing
#=========================================================================
class SimpleFrame(swing.JFrame):
def __init__(self):
swing.JFrame.__init__(self)
print "Inicializando ...\n"
#~ crea controles
self.label = swing.JLabel("This is a Swing app in Jython")
self.button = swing.JButton("Click me!")
#~ inicializa UI
self.setTitle("Hola Swing desde Jython")
self.add(self.label, BorderLayout.CENTER)
self.add(self.button, BorderLayout.SOUTH)
self.defaultCloseOperation = swing.JFrame.EXIT_ON_CLOSE
self.pack()
#~ asigna eventos
self.button.addActionListener(lambda event: self.onClickBtn(event))
def onClickBtn(self, event):
self.label.setText("Hizo click!")
#=========================================================================
def main():
frame = SimpleFrame()
frame.setVisible(True)
if __name__ == '__main__':
main()
@alcidesfp
Copy link
Author

Así es sr, de hecho lo mas que pude lograr sin un lambda es sustituyendo la línea 27 con:
self.button.actionPerformed = self.onClickBtn
pero al parecer esto es específico de Jython y no puede aplicarse en otros lenguajes de la máquina virtual de Java

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment