问题
how to code java for set timer to my button for set button icon when i clicked and some time delay for process while my button icon has to show then the icon have to set null for time end.
i have tried following way but it is work when i am not to clicked the button
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
timer = new Timer(5000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(chromeShown) {
jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/r/ajaxloading.gif")));
chromeShown = true;
} else {
jButton3.setIcon(null);
chromeShown = false;
}
}
});
timer.start();
this.getContentPane().add(JButton);
this.setVisible(true);
回答1:
Based on my understanding of your problem, your logic is a little skewed, the actions should following along something like
Button Clicked -> Icon Changed -> Timer Started ...(waiting)... -> Timer triggered -> Icon Changed.
At the moment, you're trying to change the initial state of the icon in the Timer
, which doesn't make sense. I think you want to do something more like this...
click.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
click.setEnabled(false);
click.setText("I'm running >>");
Timer timer = new Timer(1000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
click.setText("I be done");
click.setEnabled(true);
}
});
timer.start();
}
});
Basically, when the button is clicked, this sets the button's text and disables the button (so you can't click it again) and the after 1 second, it changes the text and enables the button
来源:https://stackoverflow.com/questions/41950734/how-to-set-jbutton-icon-for-specific-time-to-show-when-it-is-clicked-and-how-to