问题:写了个example学习Swing:每次Timer Event(once per sec)发生时选择下一个RadioButton。但总是选择下下一个。
平台:Mac OS X, JDK1.6, Eclipse3.6
调试:经过调试,发现每秒fire两个Timer Event。
代码:Timer相关内容用粗体字标识package components;import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.Timer;public class RadioButtonDemo extends JPanel
 implements ActionListener{ private static final long serialVersionUID = 1L;

    static String birdString = "Bird";
    static String catString = "Cat";
    static String dogString = "Dog";
    static String rabbitString = "Rabbit";
    static String pigString = "Pig";

    JButton autoButton;
    JRadioButton birdButton, catButton, dogButton, rabbitButton, pigButton;
    
    Timer timer = new Timer(1000,this);
    
    public RadioButtonDemo() {
        super(new BorderLayout());
        
        
        //Create the AUTO button.
        autoButton = new JButton("auto");
        autoButton.setActionCommand("auto");
                //Create the radio buttons.
        birdButton = new JRadioButton(birdString);
        birdButton.setMnemonic(KeyEvent.VK_B);
        birdButton.setActionCommand(birdString);
        birdButton.setSelected(true);        catButton = new JRadioButton(catString);
        catButton.setMnemonic(KeyEvent.VK_C);
        catButton.setActionCommand(catString);        dogButton = new JRadioButton(dogString);
        dogButton.setMnemonic(KeyEvent.VK_D);
        dogButton.setActionCommand(dogString);        rabbitButton = new JRadioButton(rabbitString);
        rabbitButton.setMnemonic(KeyEvent.VK_R);
        rabbitButton.setActionCommand(rabbitString);        pigButton = new JRadioButton(pigString);
        pigButton.setMnemonic(KeyEvent.VK_P);
        pigButton.setActionCommand(pigString);        //Group the radio buttons.
        ButtonGroup group = new ButtonGroup();
        group.add(birdButton);
        group.add(catButton);
        group.add(dogButton);
        group.add(rabbitButton);
        group.add(pigButton);
        
                //Register a listener for the radio buttons.
        birdButton.addActionListener(this);
        catButton.addActionListener(this);
        dogButton.addActionListener(this);
        rabbitButton.addActionListener(this);
        pigButton.addActionListener(this);
        
        autoButton.addActionListener(this);        //Put the radio buttons in a column in a panel.
        JPanel radioPanel = new JPanel(new GridLayout(0, 1));
        radioPanel.add(birdButton);
        radioPanel.add(catButton);
        radioPanel.add(dogButton);
        radioPanel.add(rabbitButton);
        radioPanel.add(pigButton);        add(radioPanel, BorderLayout.LINE_START);
        add(autoButton,BorderLayout.CENTER);
        setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
    }    /** Listens to the radio buttons. */
    public void actionPerformed(ActionEvent e) {
     if ("timer".equals(e.getActionCommand())) {
     if(birdButton.isSelected()){
     catButton.setSelected(true);
     }
     else if(catButton.isSelected()){
     dogButton.setSelected(true);
     }
     else if(dogButton.isSelected()){
     rabbitButton.setSelected(true);
     }
     else if(rabbitButton.isSelected()){
     pigButton.setSelected(true);
     }
     else if(pigButton.isSelected()){
     birdButton.setSelected(true);
     }
     }
  
     else if("auto".equals(e.getActionCommand())){
    
     timer.addActionListener(this);
     timer.setActionCommand("timer");
     timer.setInitialDelay(1000);
     timer.start();
     }
    
    
    }    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event-dispatching thread.
     */
    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("RadioButtonDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        //Create and set up the content pane.
        JComponent newContentPane = new RadioButtonDemo();
        newContentPane.setOpaque(true); //content panes must be opaque
        frame.setContentPane(newContentPane);
        
        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}请给个思路fix这个问题。
谢谢您的时间

解决方案 »

  1.   

    忘记用Code tag了,抱歉
    package components;import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.Timer;public class RadioButtonDemo extends JPanel
     implements ActionListener{ private static final long serialVersionUID = 1L;

        static String birdString = "Bird";
        static String catString = "Cat";
        static String dogString = "Dog";
        static String rabbitString = "Rabbit";
        static String pigString = "Pig";

        JButton autoButton;
        JRadioButton birdButton, catButton, dogButton, rabbitButton, pigButton;
        
        Timer timer = new Timer(1000,this);
        
        public RadioButtonDemo() {
            super(new BorderLayout());
            
            
            //Create the AUTO button.
            autoButton = new JButton("auto");
            autoButton.setActionCommand("auto");
                    //Create the radio buttons.
            birdButton = new JRadioButton(birdString);
            birdButton.setMnemonic(KeyEvent.VK_B);
            birdButton.setActionCommand(birdString);
            birdButton.setSelected(true);        catButton = new JRadioButton(catString);
            catButton.setMnemonic(KeyEvent.VK_C);
            catButton.setActionCommand(catString);        dogButton = new JRadioButton(dogString);
            dogButton.setMnemonic(KeyEvent.VK_D);
            dogButton.setActionCommand(dogString);        rabbitButton = new JRadioButton(rabbitString);
            rabbitButton.setMnemonic(KeyEvent.VK_R);
            rabbitButton.setActionCommand(rabbitString);        pigButton = new JRadioButton(pigString);
            pigButton.setMnemonic(KeyEvent.VK_P);
            pigButton.setActionCommand(pigString);        //Group the radio buttons.
            ButtonGroup group = new ButtonGroup();
            group.add(birdButton);
            group.add(catButton);
            group.add(dogButton);
            group.add(rabbitButton);
            group.add(pigButton);
            
                    //Register a listener for the radio buttons.
            birdButton.addActionListener(this);
            catButton.addActionListener(this);
            dogButton.addActionListener(this);
            rabbitButton.addActionListener(this);
            pigButton.addActionListener(this);
            
            autoButton.addActionListener(this);        //Put the radio buttons in a column in a panel.
            JPanel radioPanel = new JPanel(new GridLayout(0, 1));
            radioPanel.add(birdButton);
            radioPanel.add(catButton);
            radioPanel.add(dogButton);
            radioPanel.add(rabbitButton);
            radioPanel.add(pigButton);        add(radioPanel, BorderLayout.LINE_START);
            add(autoButton,BorderLayout.CENTER);
            setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
        }    /** Listens to the radio buttons. */
        public void actionPerformed(ActionEvent e) {
         if ("timer".equals(e.getActionCommand())) {
         if(birdButton.isSelected()){
         catButton.setSelected(true);
         }
         else if(catButton.isSelected()){
         dogButton.setSelected(true);
         }
         else if(dogButton.isSelected()){
         rabbitButton.setSelected(true);
         }
         else if(rabbitButton.isSelected()){
         pigButton.setSelected(true);
         }
         else if(pigButton.isSelected()){
         birdButton.setSelected(true);
         }
         }
      
         else if("auto".equals(e.getActionCommand())){
        
         timer.addActionListener(this);
         timer.setActionCommand("timer");
         timer.setInitialDelay(1000);
         timer.start();
         }
        
        
        }    /**
         * Create the GUI and show it.  For thread safety,
         * this method should be invoked from the
         * event-dispatching thread.
         */
        private static void createAndShowGUI() {
            //Create and set up the window.
            JFrame frame = new JFrame("RadioButtonDemo");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        //Create and set up the content pane.
            JComponent newContentPane = new RadioButtonDemo();
            newContentPane.setOpaque(true); //content panes must be opaque
            frame.setContentPane(newContentPane);
            
            //Display the window.
            frame.pack();
            frame.setVisible(true);
        }    public static void main(String[] args) {
            //Schedule a job for the event-dispatching thread:
            //creating and showing this application's GUI.
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
                }
            });
        }
    }
      

  2.   


    else if("auto".equals(e.getActionCommand())){//timer.addActionListener(this);将这行去掉再试试。
    timer.setActionCommand("timer");
    timer.setInitialDelay(0);
    timer.start();
    }