Saturday, July 25, 2009

Swing using Jython

from java import awt
from pawt import swing

labels = ['7', '8', '9', '+',
'4', '5', '6', '-',
'1', '2', '3', '*',
'0', '.', '=', '/' ]

keys = swing.JPanel(awt.GridLayout(4, 4))
display = swing.JTextField()

def push(event): # Callback for regular keys
display.replaceSelection(event.actionCommand)

def enter(event): # Callback for '=' key
display.text = str(eval(display.text))
display.selectAll()

for label in labels:
key = swing.JButton(label)
if label == '=':
key.actionPerformed = enter
else:
key.actionPerformed = push
keys.add(key)

panel = swing.JPanel(awt.BorderLayout())
panel.add("North", display)
panel.add("Center", keys)
swing.test(panel)

No comments: