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

相關文章
相關標籤/搜索