将actionlistener添加到JButton

时间:2015-04-16 11:28:41

标签: java swing jbutton actionlistener

public class BelishaBeacon { 

public class Design extends JPanel { 

    private boolean alternateColors = false; 

    public void paintComponent(Graphics g) { 
        super.paintComponent(g); 
        Graphics2D g2 = (Graphics2D) g; 
        //creating the shapes 
        Rectangle box1 = new Rectangle(163, 180, 16, 45); 
        Rectangle box2 = new Rectangle(163, 225, 16, 45); 
        Rectangle box3 = new Rectangle(163, 270, 16, 45); 
        Rectangle box4 = new Rectangle(163, 315, 16, 45); 
        Rectangle box5 = new Rectangle(163, 360, 16, 45); 
        Rectangle box6 = new Rectangle(163, 405, 16, 45); 
        //drawing the shapes 
        Ellipse2D.Double ball = new Ellipse2D.Double(a, b, 100, 100); 
        g2.draw(ball); 
        g2.draw(box1); 
        g2.draw(box2); 
        g2.draw(box3); 
        g2.draw(box4); 
        g2.draw(box5); 
        g2.draw(box6); 
        //coloring the shapes 
        g2.setColor(Color.BLACK); 
        g2.fill(box1); 
        g2.fill(box3); 
        g2.fill(box5); 
        g2.setColor(Color.YELLOW); 
        g2.fill(ball); 

        if (alternateColors) { 
            g2.setColor(Color.ORANGE); 
            g2.fill(new Ellipse2D.Double(a, b, 100, 100)); 
        } 

        alternateColors = false; 
    } 

    public void alternateColors() { 
        alternateColors = true; 
        repaint(); 
    } 
} 


public BelishaBeacon() { 
    //frame 
    JFrame frame = new JFrame(); 
    frame.setSize(330, 550); 
    frame.setTitle("Belisha Beacon"); 
    frame.setLayout(new BorderLayout(0, 0)); 
    final Design shapes = new Design(); 

    JButton jbtFlash = new JButton("Flash"); 
 jbtFlash.addActionListener( 
          new ActionListener() { 
               @Override
               public void actionPerformed(ActionEvent e) { 
                  Runnable r = new Runnable(){
                          @Override
                           public void run(){
                          while(/* user stops / toggleButton state*/ true)
                                {
                          swapColors(); // some method using static boolean
                                   try{
                                        Thread.sleep(500);
                                     }catch(Exception e){}
                                }

                           }

                        private void swapColors() {
                              boolean swapColors;
                            Graphics g2;
                            if (swapColors) { 
                               g2.setColor(Color.ORANGE); 
                             g2.fill(new Ellipse2D.Double(a, b, 100, 100)); 
                                } else {
                                    g2.setColor(Color.YELLOW); 
                             g2.fill(new Ellipse2D.Double(a, b, 100, 100)); 
                                }

                        }



                }; 
                Thread t = new Thread(r);
                t.start();
                }});
    JButton jbtSteady = new JButton("Steady"); 
    jbtSteady.addActionListener( 
            new ActionListener() { 
                public void actionPerformed(ActionEvent e) { 
                    shapes.alternateColors(); 
                } 
            }); 

我为jbutton stable和flash创建了一个动作监听器,我只是想为swapColors创建一个方法,让它在jbutton flash中初始化。 swapcolos方法最初应该在橙色和灰色之间交替

2 个答案:

答案 0 :(得分:0)

    if (alternateColors) { 
        g2.setColor(Color.ORANGE); 
        g2.fill(new Ellipse2D.Double(a, b, 100, 100)); 
    } 

    alternateColors = false; 

应该是:

    if (alternateColors) { 
        g2.setColor(Color.ORANGE); 
        g2.fill(new Ellipse2D.Double(a, b, 100, 100)); 
    } else {
        g2.setColor(Color.YELLOW); 
        g2.fill(new Ellipse2D.Double(a, b, 100, 100)); 
    }

public void alternateColors() { 
    alternateColors = true; 
    repaint(); 
} 

应该是:

public void alternateColors() { 

    //Flip boolean alternateColors
    alternateColors = !alternateColors;

    repaint(); 
} 

答案 1 :(得分:0)

您写道,您希望它每0.5秒闪烁一次。

为此,您需要启动新线程,例如

jbtFlash.addActionListener( 
        new ActionListener() { 
            @Override
            public void actionPerformed(ActionEvent e) { 
                Runnable r = new Runnable(){
                       @Override
                       public void run(){
                            while(/* user stops / toggleButton state*/ true)
                            {
                                 swapColors(); // some method using static boolean
                                 try{
                                    Thread.sleep(500);
                                 }catch(Exception e){}
                            }

                       }



            }; 
            Thread t = new Thread(r);
            t.start();
            }});