我已经设置了Eclipse来使用Jython,如下所示:
http://www.jython.org/jythonbook/en/1.0/JythonIDE.html(在最小配置下)
我尽可能地遵循本教程,但由于某种原因,IDE无法理解Java导入。第javax.swing import JFrame, JLabel
行将JFrame和JLabel强调为未解析。
完整代码:
# -*- coding: utf-8 -*-
import sys
from optparse import OptionParser
greetings = dict(en=u'Hello %s!',
es=u'Hola %s!',
fr=u'Bonjour %s!',
pt=u'Al %s!')
uis = {}
def register_ui(ui_name):
def decorator(f):
uis[ui_name] = f
return f
return decorator
def message(ui, msg):
if ui in uis:
uis[ui](msg)
else:
raise ValueError("No greeter named %s" % ui)
def list_uis():
return uis.keys()
@register_ui('console')
def print_message(msg):
print msg
@register_ui('window')
def show_message_as_window(msg):
from javax.swing import JFrame, JLabel
frame = JFrame(msg,
defaultCloseOperation=JFrame.EXIT_ON_CLOSE,
size=(100, 100),
visible=True)
frame.contentPane.add(JLabel(msg))
if __name__ == "__main__":
parser = OptionParser()
parser.add_option('--ui', dest='ui', default='console',
help="Sets the UI to use to greet the user. One of: %s" %
", ".join("'%s'" % ui for ui in list_uis()))
parser.add_option('--lang', dest='lang', default='en',
help="Sets the language to use")
options, args = parser.parse_args(sys.argv)
if len(args) < 2:
print "Sorry, I can't greet you if you don't say your name"
sys.exit(1)
if options.lang not in greetings:
print "Sorry, I don't speak '%s'" % options.lang
sys.exit(1)
msg = greetings[options.lang] % args[1]
try:
message(options.ui, msg)
except ValueError, e:
print "Invalid UI name\n"
print "Valid UIs:\n\n" + "\n".join(' * ' + ui for ui in list_uis())
sys.exit(1)
当我运行它时,我选择了Jython。所以我不明白为什么Eclipse不理解。我是否需要在每个Jython项目中包含Jython JAR文件......?
提前致谢。
答案 0 :(得分:1)
你为此创建了一个新的PyDev项目吗?没有它,Eclipse将无法找到完整的Jython安装,这可以解释下划线。在我的环境中(Eclipse Kepler,PyDev和Jython 2.5.2),它可以正常工作。