什么是Jython?自己百度.
Jython大概就JAVA版的Python, 同样地Python可以理解为C版的.
下面开始以调用SWT为例:
先上jython代码, 文件名为"随便.py":
# -*- coding: utf-8 -*-
from java.util import Date
from org.eclipse.swt.widgets import Display
from org.eclipse.swt.widgets import Shell
from org.eclipse.swt.widgets import Button
from org.eclipse.swt import events
class Adapter(events.MouseAdapter):
e = events.MouseEvent
def mouseDown(self, e):
d = Date()
print d
def main():
display = Display.getDefault()
shell = Shell()
shell.setSize(450, 300)
shell.setText("hello Jython!")
mBtn = Button(shell, 0)
mBtn.setBounds(63, 55, 62, 32)
mBtn.setText(u"确定")
ap = Adapter()
mBtn.addMouseListener(ap)
shell.open()
shell.layout()
while not shell.isDisposed():
if not display.readAndDispatch():
display.sleep()
if __name__ == '__main__':
main()
代码中需要swt的开发包, 即是swt.jar.
在linux控制台运行命令:
export CLASSPATH=.:/usr/java/jdk1.6.0_45/lib/swt.jar
之后运行:
jython 随便.py
完成.
结论由以上可以得出:
可以用jython调用java所写的class文件, 相反其实也可以用java调用jython的.