Jython - 尝试从 JFrame 调用函数,接收“NoneType”;错误

发布于 2024-09-26 01:50:11 字数 821 浏览 0 评论 0原文

所以我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

第几種人 2024-10-03 01:50:11

在这一行中:

frame = JFrame('E-mail Gathering', defaultCloseOperation = JFrame.EXIT_ON_CLOSE, size =(600,400), locationRelativeTo = None).setVisible(True)

您将创建一个 JFrame,调用其 setVisible 方法,并将 setVisible 的返回值分配给 frame. setVisible 不返回值,因此 frameNone。这会导致 frame.add 失败。

无论如何,当您在最后调用 setVisible 时,并且因为您可能不想在向框架添加其他组件并调用 pack 之前使框架可见,所以只需删除setVisible 调用:

frame = JFrame('E-mail Gathering', defaultCloseOperation = JFrame.EXIT_ON_CLOSE, size =(600,400), locationRelativeTo = None)

In this line:

frame = JFrame('E-mail Gathering', defaultCloseOperation = JFrame.EXIT_ON_CLOSE, size =(600,400), locationRelativeTo = None).setVisible(True)

you are creating a JFrame, calling its setVisible method, and assigning the return value of setVisible to frame. setVisible doesn't return a value, so frame is None. This causes frame.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 called pack, just remove the setVisible call:

frame = JFrame('E-mail Gathering', defaultCloseOperation = JFrame.EXIT_ON_CLOSE, size =(600,400), locationRelativeTo = None)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文