Jython - 尝试从 JFrame 调用函数,接收“NoneType”;错误
所以我正在尝试使用 Jython,尝试拼凑出一个通用的 GUI。除了 Jython Wiki 上提供的 Swing 示例之外,没有任何其他内容。所以我声明一个 JFrame,然后尝试添加一个面板、一些文本字段,以及所有这些好东西。但是,当我运行它时,我收到此错误。 “'NoneType'对象没有属性'add'”
这是我的基本代码。
from javax.swing import *
frame = JFrame('E-mail Gathering', defaultCloseOperation = JFrame.EXIT_ON_CLOSE, size =(600,400), locationRelativeTo = None).setVisible(True)
pnl = JPanel()
frame.add(pnl)
self.textfield1 = JTextField('username:',15)
pnl.add(self.textfield1)
self.textfield2 = JTextField('password:', 15)
pnl.add(self.textfield2)
mailButton = JButton('Login',actionPerformed=self.checkmail)
pnl.add(mailButton)
frame.pack()
frame.setVisible(True)
我知道崩溃的相关部分位于“frame.add(pnl)”,并出现上述错误。我想我应该把剩下的放在那里,以防万一我犯下更大的错误。我觉得我没有正确地将框架声明为 JFrame,但我知道情况并非如此,因为如果我不尝试向其中添加任何内容,它会很好地创建框架。
感谢您提出的任何意见或建议。
So I'm playing around with Jython, trying to slap together a generic GUI. Nothing beyond what they have on the Jython Wiki for swing examples. So I declare a JFrame, and then try to add a panel, some text fields, all that good stuff. I get this error when I run it, however. "'NoneType' object has no attribute 'add'"
Here's the basic code I have.
from javax.swing import *
frame = JFrame('E-mail Gathering', defaultCloseOperation = JFrame.EXIT_ON_CLOSE, size =(600,400), locationRelativeTo = None).setVisible(True)
pnl = JPanel()
frame.add(pnl)
self.textfield1 = JTextField('username:',15)
pnl.add(self.textfield1)
self.textfield2 = JTextField('password:', 15)
pnl.add(self.textfield2)
mailButton = JButton('Login',actionPerformed=self.checkmail)
pnl.add(mailButton)
frame.pack()
frame.setVisible(True)
I know the relevant part where it's crashing is at 'frame.add(pnl)' with the aforementioned error. I figured I'd throw the rest up there just in case I'm making some even greater mistakes. I feel like something's wrong where I'm not declaring frame as a JFrame properly, but I know that's not the case because it creates the frame just fine if I don't try to add anything to it.
Thanks for any advice or suggestions you have.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在这一行中:
您将创建一个
JFrame
,调用其setVisible
方法,并将setVisible
的返回值分配给frame.
setVisible
不返回值,因此frame
为None
。这会导致frame.add
失败。无论如何,当您在最后调用
setVisible
时,并且因为您可能不想在向框架添加其他组件并调用pack
之前使框架可见,所以只需删除setVisible
调用:In this line:
you are creating a
JFrame
, calling itssetVisible
method, and assigning the return value ofsetVisible
toframe
.setVisible
doesn't return a value, soframe
isNone
. This causesframe.add
to fail.As you call
setVisible
at the end anyway, and because you probably don't want to make the frame visible before you have added other components to it and calledpack
, just remove thesetVisible
call: