Linux使用Jython调用JAVA接口

什么是Jython?本身百度. java

Jython大概就JAVA版的Python, 一样地Python能够理解为C版的. python

下面开始以调用SWT为例: linux

先上jython代码, 文件名为"随便.py": shell

# -*- 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. eclipse

在linux控制台运行命令: spa

export CLASSPATH=.:/usr/java/jdk1.6.0_45/lib/swt.jar
以后运行:

jython 随便.py

完成. code

结论由以上能够得出: ip

能够用jython调用java所写的class文件, 相反其实也能够用java调用jython的. utf-8

相关文章
相关标签/搜索