英文:
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");
}
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论