我想做一個簡單的動畫使用計時器,添加和刪除按鈕,但我不知道我在哪裏錯了。另外我添加了兩個打印只是爲了驗證。他們打印正確。我還觀察到,如果我刪除removeAll()方法,它會添加按鈕。JAVA時間動畫
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Animation extends JPanel{
JLabel l1 = new JLabel(new ImageIcon("tom1.png"));
JLabel l2 = new JLabel(new ImageIcon("tom2.png"));
JLabel l3 = new JLabel(new ImageIcon("tom3.png"));
JLabel l4 = new JLabel(new ImageIcon("tom4.png"));
JLabel l5 = new JLabel(new ImageIcon("tom5.png"));
JButton b1 = new JButton("HAHA");
JButton b2 = new JButton();
JPanel panel = new JPanel();
public Animation(){
setLayout(new BorderLayout());
Timer time = new Timer(1000,new ActionListener(){
public void actionPerformed(ActionEvent e){
System.out.println("paint");
add(b1, BorderLayout.CENTER);
repaint();
revalidate();
System.out.println("remove");
removeAll();
repaint();
revalidate();
}
});
time.start();
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.add(new Animation());
frame.setVisible(true);
frame.setSize(1000,400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
你要求添加按鈕,然後立即你removeAll()'。如果按鈕出現,它將是您永遠不會注意到的最閃爍的閃爍。你期望發生什麼? – dcsohl 2015-02-05 18:42:48
然後我可以做什麼? – zaa 2015-02-05 18:48:23
@zaa你想讓按鈕閃爍? – 2015-02-05 18:52:53