簡體   English   中英

將動作監聽器添加到JButton

[英]Adding an actionlistener to a JButton

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穩定和Flash創建了一個動作監聽器,我只是想為swapColors創建一個方法,以便在jbutton flash中對其進行初始化。 swapcolos方法最初應在橙色和灰色之間切換

    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(); 
} 

您寫道您希望它每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();
            }});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM