Java 将actionlistener添加到JButton

Java 将actionlistener添加到JButton,java,swing,jbutton,actionlistener,Java,Swing,Jbutton,Actionlistener,我已经为我的jbuttonstable和flash创建了一个动作监听器,我只是尝试为swapColors创建一个方法,以便在jbuttonflash中初始化它。swapcolos方法最初应在橙色和灰色之间交替使用 public class BelishaBeacon { public class Design extends JPanel { private boolean alternateColors = false; public void paintCompo

我已经为我的jbuttonstable和flash创建了一个动作监听器,我只是尝试为swapColors创建一个方法,以便在jbuttonflash中初始化它。swapcolos方法最初应在橙色和灰色之间交替使用

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(); 
                } 
            }); 
应该是:

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

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

应该是:

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

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

你写的是希望它每0.5秒闪烁一次

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

public void alternateColors() { 

    //Flip boolean alternateColors
    alternateColors = !alternateColors;

    repaint(); 
} 

因此,您需要通过单击JButton jbtFlash来交替形状的颜色?我认为您需要创建一个实现
ActionListener
的类,然后在
addActionListener
方法中使用这个新类是的,基本上我已经为名为stable的JButton完成了,它将颜色从黄色更改为橙色,但我不太确定如何为Flash制作同一个JButt,它从橙色到GRYYE每秒钟闪烁一次,可以考虑参数化<代码> AltAtNeCuCor(彩色CRONONE,ColorColorTwo)< /C> >尝试<代码> JBTFlash。AdActudiListNead(新ActudiListNEnter){………?我尝试过这种方法,但它只允许我在每次按下稳定键时从橙色切换到黄色,当我单击flash时,它不会在颜色之间切换。当我将它添加到flash按钮时,它会在整个代码块下显示一条红线,并显示“错位”construct@mdbuzz括号和分号有问题,请重试现在复制,我的netbeans显示没有错误是的,它去掉了红线,但是你知道你的方法SwapColor我应该创建它吗,因为我没有一个方法叫做SwapColor only AlternateColours是的,你必须自己写你的交换颜色,但是它很简单,你有其他答案。抱歉再次打扰你,我尝试为SwapColor创建一个方法,但它似乎不起作用,我不确定我是否做错了什么