【发布时间】:2017-10-01 07:03:19
【问题描述】:
如何将焦点监听器改为只执行动作,这样当按钮点击时,会触发淡入淡出方法?
class FaderTimer implements FocusListener, ActionListener {
private ArrayList colors;
private JButton component;
private Timer timer;
private int alpha;
private int increment;
FaderTimer(ArrayList colors, JButton component, int interval) {
this.colors = colors;
this.component = component;
component.addFocusListener(this);
timer = new Timer(interval, this);
}
public void focusGained(FocusEvent e) {
alpha = 0;
increment = 1;
timer.start();
}
public void focusLost(FocusEvent e) {
alpha = steps;
increment = -1;
timer.start();
}
public void actionPerformed(ActionEvent e) {
alpha += increment;
component.setBackground((Color) colors.get(alpha));
if (alpha == steps || alpha == 0) {
timer.stop();
}
}
}
}
【问题讨论】:
-
您需要两个
ActionListeners,一个用于按钮,一个用于Timer -
您还必须考虑到原始代码正在淡入和淡出,因此您需要决定应该采取哪些操作(您是淡入还是淡出)
-
我想在单击按钮时淡入,那是怎么回事? @MadProgrammer
标签: java swing button event-listener eventhandler