1

这个问题可能与我怀疑的不同,但我认为问题是我在学习接口时读错了,或者它在动作侦听器内部的应用不同。

首先,这是我的错误:

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: javax.swing.JButton cannot be cast to main.GUI
    at main.GUI$3.actionPerformed(GUI.java:224)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$000(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

我正在使用的代码如下:

this.select.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                ((GUI) e.getSource()).tool = new Selection();
            }
        });

我有一个名为 tool 的接口,我将它作为一个全局变量 Tool 工具;对于 GUI 类。然后我尝试在上面的方法中将 Selection 类分配给实现 Tool 的 var 工具。我做错了什么,我该如何解决?如果您需要更多代码,请询问。

此外,当我将新的 Selection() 分配给 ActionListener 之外的全局 var 工具时,它可以工作......所以这就是我不明白的。

4

2 回答 2

3

关于

我认为 java 接口可以包含一个子类,但可能不在 ActionListener 中……对吗?

不,这与您的问题无关。相反,您的问题是纯粹而简单的错误转换。返回的对象e.getsource()不是 GUI 对象,而是一个 AbstractButton,可能是一个 JButton(它是名为“select”的变量)。添加了 ActionListener 的组件是由ActionEvent#getSource().

另外,我看不到您在哪里有一个在任何地方都有一个子类的接口。

你能做到吗?:

    this.select.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            tool = new Selection(); //??
        }
    });

很难说你应该做什么,因为我们真的不知道你的其余代码的结构是什么样的。

于 2012-07-08T20:24:57.103 回答
3

我在工具栏内有按钮。

使用此处Action所示的 来封装功能。

于 2012-07-08T20:49:11.147 回答