从超级`ActionListener`获取对象 | JAVA 8

huangapple go评论66阅读模式
英文:

get the Object from super ActionListener | JAVA 8

问题

我想将`ButtonStart01`的操作转发给`MouseListener`。这样我就可以为按钮添加悬停效果一个有效的方法是什么如果可能的话我希望在`public ButtonStart01() {...}` 构造函数内解决当我使用被注释掉的 `super.setIcon(imageIconHover);`它是指向了 `MouseListener` 而不是期望的 `ButtonStart01`。

package view.button;

import java.awt.Dimension;
import javax.swing.ImageIcon;
import javax.swing.JButton;

public class ButtonStart01 extends JButton {

    private static ImageIcon imageIcon = new ImageIcon("src/start01.png");
    private static ImageIcon imageIconHover = new ImageIcon("src/start01_hover.png");

    public ButtonStart01() {
        super(imageIcon);
        super.setOpaque(false);
        super.setContentAreaFilled(false);
        super.setBorderPainted(false);
        super.setPreferredSize(new Dimension(160, 60));
        super.setMinimumSize(new Dimension(160, 60));
        super.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                //super.setIcon(imageIconHover);
                System.out.println("in");
            }

            public void mouseExited(java.awt.event.MouseEvent evt) {
                //super.setIcon(imageIcon);
                System.out.println("out");
            }
        });
        super.setActionCommand("start01");
    }
}
英文:

I want to forward the ButtonStart01 to the MouseListener. So I can add a hover
effect to the button. What could be a valid approach to this? If possible I want
to solve it within the public ButtonStart01() {...}; constructor.
When I use the commented super.setIcon(imageIconHover); it is referring to the MouseListener
instead of the wanted ButtonStart01.

package view.button;
import java.awt.Dimension;
import javax.swing.ImageIcon;
import javax.swing.JButton;
public class ButtonStart01 extends JButton {
private static ImageIcon imageIcon = new ImageIcon("src/start01.png");
private static ImageIcon imageIconHover = new ImageIcon("src/start01_hover.png");
public ButtonStart01() {
super(imageIcon);
super.setOpaque(false);
super.setContentAreaFilled(false);
super.setBorderPainted(false);	
super.setPreferredSize(new Dimension(160, 60));
super.setMinimumSize(new Dimension(160, 60));
super.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseEntered(java.awt.event.MouseEvent evt) {
//super.setIcon(imageIconHover);
System.out.println("in");
}
public void mouseExited(java.awt.event.MouseEvent evt) {
//super.setIcon(imageIcon);
System.out.println("out");
}
});
super.setActionCommand("start01");
}
}

答案1

得分: 1

为了在MouseListener内部获取对ButtonStart01的引用,您应该执行以下操作:

@Override
public void mouseEntered(MouseEvent evt) {
    ButtonStart01 button = ButtonStart01.this;
    // 对 button 进行操作
}

而为了在MouseListener内部调用super方法,您应该执行以下操作(假设我想要禁用按钮):

@Override
public void mouseEntered(MouseEvent evt) {
    ButtonStart01.super.setEnabled(false);
}

然而,根据我所见,您使用super关键字来调用所有的super方法。这样做是可以的,但并不是必需的。当您extend一个类并调用一个当前类没有覆盖的方法时,将会调用父类的方法。

我的意思是,如果您只是使用setBorderPainted(false);而不是super.setBorderPainted(false);,效果是一样的。因为您在ButtonStart01类中没有@Override setBorderPainted方法。

英文:

In order to get a reference to the ButtonStart01 inside MouseListener you should be doing:

@Override
public void mouseEntered(MouseEvent evt) {
ButtonStart01 button = ButtonStart01.this;
//Do whatever with button
}

And in order to call a super method inside MouseListener you should be doing (let's say I want to disable the button):

@Override
public void mouseEntered(MouseEvent evt) {
ButtonStart01.super.setEnabled(false);
}

However, as I see, you use super keyword to call all super methods. This is ok, but it is not required. When you extend a class, and call a method that it is not overrided by the current class, the super method will be called.

What I want to say is, that if you simply setBorderPainted(false); instead of super.setBorderPainted(false);, it's the same thing. Because you don't @Override setBorderPainted method in ButtonStart01 class.

答案2

得分: -1

因为您在内部添加了MouseListener,我认为使用 @Override 应该可以得到您想要的结果。

        super.addMouseListener(new java.awt.event.MouseAdapter() {
            @Override
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                super.setIcon(imageIconHover);
                System.out.println("进入");
            }
            @Override
            public void mouseExited(java.awt.event.MouseEvent evt) {
                super.setIcon(imageIcon);
                System.out.println("退出");
            }
        });
英文:

Since you are add the MouseListener internally, I think @Override should get the result you want.

        super.addMouseListener(new java.awt.event.MouseAdapter() {
@Override
public void mouseEntered(java.awt.event.MouseEvent evt) {
super.setIcon(imageIconHover);
System.out.println("in");
}
@Override
public void mouseExited(java.awt.event.MouseEvent evt) {
super.setIcon(imageIcon);
System.out.println("out");
}
});

huangapple
  • 本文由 发表于 2020年8月25日 17:33:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/63575969.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定