如何给一个按钮同时添加两个click事件
importjava.awt.BorderLayout;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.util.ArrayList;
importjava.util.List;
importjava.util.Timer;
importjava.util.TimerTask;
importjavax.swing.JButton;
importjavax.swing.JFrame;
importjavax.swing.JLabel;
importjavax.swing.SwingUtilities;
publicclassTimerDemo{
publicstaticvoidmain(String[]args){
SwingUtilities.invokeLater(newRunnable(){
@Override
publicvoidrun(){
newTimerDemo().newTimerFrame();
}
});
}
classTimerFrameextendsJFrame{
privatestaticfinallongserialVersionUID=281511492706800685L;
JLabellabel=null;
JButtonbutton=null;
List<Timer>list=newArrayList<Timer>();
publicTimerFrame(){
JFrameframe=newJFrame();
frame.setSize(300,200);
label=newJLabel();
button=newJButton("开始计时");
button.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEventarg0){
finalTimertimer=newTimer();
for(Timertim:list){
tim.cancel();
}
list.clear();
if(list.isEmpty()){
list.add(timer);
}
timer.schedule(newTimerTask(){
privateintcount=30;
@Override
publicvoidrun(){
if(count>0){
SwingUtilities.invokeLater(newRunnable(){
@Override
publicvoidrun(){
label.setText(""+String.valueOf(count--)+"秒");
}
});
}else{
timer.cancel();
SwingUtilities.invokeLater(newRunnable(){
@Override
publicvoidrun(){
label.setText("时间到!");
}
});
}
}},0,1000);
}
});
frame.getContentPane().add(label,BorderLayout.NORTH);
frame.getContentPane().add(button,BorderLayout.SOUTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
}
扩展资料
同一个元素绑定多个onclick事件
<divclass="row"id="row"onclick="A();B()">
<scripttype="text/javascript"src="">
functionA(){
alert("aaa");
}
functionB(){
alert("bbb");
}
</script>
例如:
<input type="button" id="test" value="点击后两次弹框" />
<script>
document.getElementById('test').onclick = function(){
alert(1);
alert(2);
};
</script>