JLabel 上的 Jython 鼠标侦听器导致 TypeError

发布于 2024-12-17 03:51:38 字数 895 浏览 3 评论 0原文

我正在 Jython 中创建一个应用程序,尝试捕获 JLabel 上的鼠标事件。我有一个包含 JLabel 的 JFrame,但是当我尝试向其中添加鼠标侦听器时,我得到:

TypeError: write only attribute

主窗口:

class Commander(JFrame):
    ...
    self.image = ImageIcon()
    self.label = JLabel(self.image)
    self.mouseListener = ScreenMouseListener()
    self.label.addMouseListener(self.mouseListener) <- This line causes the TypeError
    ...

鼠标侦听器扩展了 MouseAdapter:

class ScreenMouseListener(MouseAdapter):
    def mousePressed(self, event):
        print "Mouse pressed"

搜索错误导致我 PyBeanEvent,但我不明白为什么会发生。哪个属性是只写的?

编辑:删除导致错误的行后,我注意到 mousePressed 函数被调用! “mouseListener”可能是 JFrame 的属性吗?

I'm making an application in Jython where I try to capture mouse events on a JLabel. I have a JFrame that contains a JLabel, but when I try to add a mouse listener to it I get:

TypeError: write only attribute

The main window:

class Commander(JFrame):
    ...
    self.image = ImageIcon()
    self.label = JLabel(self.image)
    self.mouseListener = ScreenMouseListener()
    self.label.addMouseListener(self.mouseListener) <- This line causes the TypeError
    ...

The mouse listener extends MouseAdapter:

class ScreenMouseListener(MouseAdapter):
    def mousePressed(self, event):
        print "Mouse pressed"

Searching for the error leads me to PyBeanEvent, but I don't understand why it happens. Which attribute is write-only?

Edit: After removing the line that caused the error, I noticed that the mousePressed function gets called! Is "mouseListener" perhaps a property of JFrame?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

2024-12-24 03:51:38

我相信JFrame.mouseListener是一个只写属性,即没有实现get函数,所以当它传递给self.label.addMouseListener时,它无法被读取。
通过执行以下操作解决了该问题:

self.label.addMouseListener(ScreenMouseListener())

I believe JFrame.mouseListener is a write-only property, i.e. there is no get function implemented, so when it was passed to self.label.addMouseListener, it could not be read.
Solved it by doing the following instead:

self.label.addMouseListener(ScreenMouseListener())
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文