apr 2
So here are some results from my
initial "playing around" with eclipse, jython, and java awt/swing.
I'll be interested to hear yours.
eclipse notes
type 'import ...' first
The voice of experience: you really, really want to type the import statements *first*.
My session pretty much froze up completely when I typed
frame = JFrame("...")
before trying to type
from ja # vax.swing import JFrame (I almost couldn't type this!)
because it was so busy thinking about the error that JFrame wasn't defined
it wouldn't let me finish typing. And it put an "error" up that "ja"
wasn't a valid import. Sheesh.
cache
Make the cache/ folder within the jython/ installation directory writable;
that gets rid of many of the runtime jython warnings, though there
are still a few warnings about reloading modified .jar files from the system libraries.
jython book
Note that this covers objects and pretty much the rest of basic python too.
The idea is that GUI example shows enough of Java's Swing library that
you can then translate the rest of the java docs into the jython format.
Discuss the notion of Java "beans" and how that's been adapted by the Jython folks;
use the swing API for some samples.
Swing
Swing is the most popular of Java's window systems. It's newer and generally thought to be spiffier than AWT.
interactive exploration
$ jython
>>> import javax, java
>>> from java.awt import Color
>>> from javax.swing import JFrame
>>> jf = JFrame('testing')
>>> dir(jf)
>>> dir(java)
>>> dir(swing)
>>> jf.x
>>> jf.width
>>> jf.background = Color.red
>>> dir(java.awt)
example
#!/usr/bin/env jython
"""
A gui sample program using Jython and Swing.
See
* http://jythonpodcast.hostjava.net/jythonbook/en/1.0/GUIApplications.html
* http://java.sun.com/docs/books/tutorial/uiswing/components/tabbedpane.html
"""
from javax.swing import JFrame, JButton, JTabbedPane, JLabel
from java.awt import BorderLayout
def changeText(Event):
print 'You clicked the button.'
def main():
""" Create and set up the window. """
frame = JFrame('a Swing/Jython GUI demo',
defaultCloseOperation = JFrame.EXIT_ON_CLOSE,
size = (300, 300))
#
tabbedPane = JTabbedPane();
frame.add(tabbedPane, BorderLayout.CENTER)
#
button = JButton('Click here.', actionPerformed=changeText)
tabbedPane.add(button, 'Button')
#
label = JLabel('Hello there.')
tabbedPane.add(label, 'Label')
#
frame.visible = True
main()
Look at the Swing API, and discuss the Java vs Jython language differences,
as discussed in the Jython book.
I think that to actually draw lines and what-not in a window,
which have to create a class that inherits from an awt pane,
and override its paintComponent(g) method, which gets a Graphics object;
that's what actually has the low level line etc commands.
I haven't tested that yet, but I believe that the 'comp'
object in texttest.py, below, is one of these
one more example
# from http://www.akeric.com/blog/?p=943
01 # texttest.py
02 from javax.swing import *
03 from java.awt import *
04
05 class TextTest(JFrame):
06 def __init__(self):
07 JFrame.__init__(self, "TextTest")
08 self.setSize(256, 128)
09 self.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
10 pane = HelloPane()
11 self.add(pane)
12 self.setVisible(True)
13
14 class HelloPane(JPanel):
15 def paintComponent(self, comp):
16 f = Font("Arial", Font.PLAIN, 32)
17 comp.setFont(f)
18 comp.drawString("Hello World!", 32, 64)
19
20 if __name__ == "__main__":
21 tt = TextTest()
in class
- Add another pane that shows some radio buttons and an icon.
- Make the icon change when you change the radio button.