of javax.swing

src: catalysoft | uselesspython

Create a (rather useless) jython application in (at most) 1 day:
0. download & install java SDK + jython
1. add /java/bin and /jython to the system path
2. save this snippet into test.py (e.g d:\test.py)
import javax.swing as swing

def printMessage(event):
print "Ouch!"

if __name__== "__main__":
frame=swing.JFrame(title="My Frame", size=(300,300))
frame.defaultCloseOperation=swing.JFrame.EXIT_ON_CLOSE;
button=swing.JButton("Push Me!", actionPerformed=printMessage)
frame.contentPane.add(button)
frame.visible=1
3. open a command prompt (drop to dos) in d:\ and execute this:
jythonc -c -j nuo.jar test.py

3a. I got this error 'ERROR DURING JAVA COMPILATION... EXITING', means the compiler can't locate javac.exe, so include javac.exe full path and (in this case) execute this instead
jythonc -C C:\j2sdk1.4.2_12\bin\javac.exe -c -j nuo.jar test.py

4. Doubleclick the resulting nuo,jar, a simple swing application came to life!!

explanation
- consider this real java code
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MyExample
implements ActionListener {

public void actionPerformed(ActionEvent e) {
System.out.println("Ouch!");
}

public static void main(String[] args) {
JFrame frame = new JFrame("My Frame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,300);
JButton button = new JButton("Push Me!");
frame.getContentPane().add(button);
ActionListener listener = new MyExample();
button.addActionListener(listener);
frame.setVisible(true);
}
}
it's python counterpart is smaller = faster development time..
- the above jythonc command means compile the python code into java bytecode (.class)
- the -c switch means includes all (OR relevant??) the jython library into the jar
- the -j switch means creates executable jar file, with the relevant manifest file
- a 455 bytes script (test.py) results in a 755 KB (nuo.jar) executable :lol:

No comments: