我想在事件开始时在 Java Swing 中的按钮单击事件上显示加载器图标,并且在事件完成时加载器需要消失。
如何做到这一点?目前我正在做这样的事情。
package swing;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class LoaderDisplay {
public static void main(String[] args) {
final JFrame frame = new JFrame();
JPanel panel = new JPanel();
JButton button1 = new JButton();
ImageIcon loading = new ImageIcon("loading_icon.gif");
final JLabel label = new JLabel("Loading... ", loading, JLabel.CENTER);
label.setVisible(false);
frame.add(panel);
panel.add(button1);
panel.add(label);
frame.setVisible(true);
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// event start
label.setVisible(true);
// some operation
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// event end
label.setVisible(false);
}
});
}
}
任何帮助?