为按钮使用动作监听器

Using Action Listeners for buttons

代码:Java球体class

 import javax.swing.*;
 import java.awt.*;
 import java.awt.geom.Ellipse2D;

public class Sphere extends JPanel {
private boolean flashinglights = false;
private int x = 168;
private int y = 75;


public void paintComponent(Graphics g) { 
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D) g;
    if (flashinglights) { //This is the flash option. Here it should change between grey and orange
        g2.setColor(Color.ORANGE);
        Ellipse2D.Double ball = new Ellipse2D.Double(x, y, 50, 50);
        g2.draw(ball);
        g2.fill(ball);
    } else {
        g2.setColor(Color.gray); //This should stay grey as it does now.
        Ellipse2D.Double ball = new Ellipse2D.Double(x, y, 50, 50);
        g2.draw(ball);
        g2.fill(ball);
    }
}

public void chooseflashinglights(){ //Ignore these methods
    flashinglights = false;
}

public void choosesteady(){
    flashinglights = true;
}

public void flickerorange(int d) { y = y + d; }


public void flickergrey(int d) { y = y + d; }


public static void main(String[] args) {
    JFrame scFrame = new AnimationViewer();
    scFrame.setTitle("Circle");
    scFrame.setSize(400, 400);
    scFrame.setDefaultCloseOperation((JFrame.EXIT_ON_CLOSE));
    scFrame.setVisible(true);
}

}

动画查看器Class:

 import javax.swing.*;
 import java.awt.*;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;


public class AnimationViewer extends JFrame {
JButton jbtFlash = new JButton("Flash");
JButton jbtSteady = new JButton("Steady");
JPanel bPanel = new JPanel();
Sphere sphPanel = new Sphere();
Timer timer;

public AnimationViewer() {
    this.add(bPanel, BorderLayout.SOUTH);
    bPanel.add(jbtFlash);
    bPanel.setLayout(new GridLayout(1,2));
    bPanel.add(jbtSteady);

    this.add(sphPanel, BorderLayout.CENTER);


    jbtSteady.addActionListener(new SteadyLights());
    jbtFlash.addActionListener(new FlashingLights());

    timer = new Timer(100, new TimerListener());
    timer.start();
}



class TimerListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        sphPanel.flickerorange(0);
        sphPanel.flickergrey(0);
        repaint();
    }
}
class FlashingLights implements ActionListener{
    public void actionPerformed(ActionEvent e){
        sphPanel.chooseflashinglights();
    }
}
class SteadyLights implements ActionListener{
    public void actionPerformed(ActionEvent e){
        sphPanel.choosesteady();
    }
}

}

所以现在屏幕上出现了一个球体。下面显示了两个按钮。闪光和稳定。在稳定按钮上,它必须保持一种颜色(橙色),但它不会。

现在在 Flash 上,它必须每 100 毫秒从橙色变为灰色。

我知道这一定与 Action 侦听器有关,但我该如何实现呢?

我会做一些不同的事情。

  • 我的绘图 JPanel 中有一个字段 class 指示应该显示什么颜色。
  • 例如,如果只有两种颜色需要交换,我会使用一个布尔字段,并在我的计时器中交换它的值,然后根据它的值绘制颜色。
  • paintComponent 方法将使用该字段(布尔值)来决定 g.setColor(...) 使用哪种颜色。
  • 如果绘制了多种颜色,则该字段可以是 Color 数组和数组中的 int 索引的组合。然后我会根据需要增加索引,并使用 paintComponent 中的索引来决定使用哪种颜色绘制。
  • 我会在我的 Timer 的 ActionListener 中更改该字段 - 只需一次调用即可更改它,而不是您对闪烁橙色和闪烁灰色的奇怪调用。
  • 我会用一个按钮启动计时器
  • 我会用另一个按钮停止它。这就是按钮的动作侦听器要做的所有事情,只需启动或停止计时器即可。

你有很多额外的代码。我会这样做。在 paintComponent 方法中编写闪烁逻辑。然后只创建一个计时器。每 100 毫秒调用一次 paintComponent 方法。在闪光按钮上单击启动计时器,在稳定按钮上单击停止计时器并调用一次 paintComponent。

球体 class:

public class Sphere extends JPanel {
private boolean flashinglights = false;
private int x = 168;
private int y = 75;
private Color[] colors = new Color[] {Color.ORANGE, Color.GRAY };
private int colorIndex = 0;

public void paintComponent(Graphics g) {
   super.paintComponent(g);
   Graphics2D g2 = (Graphics2D) g;
   if (!flashinglights) {
       g2.setColor(Color.ORANGE);
       Ellipse2D.Double ball = new Ellipse2D.Double(x, y, 50, 50);
       g2.draw(ball);
       g2.fill(ball);
   } else {
       if(colorIndex > colors.length - 1)
           colorIndex = 0;

       g2.setColor(colors[colorIndex++]);
       Ellipse2D.Double ball = new Ellipse2D.Double(x, y, 50, 50);
       g2.draw(ball);
       g2.fill(ball);
   }
}

public void chooseflashinglights(){ //Ignore these methods
   flashinglights = true;
}

public void choosesteady(){
   flashinglights = false;
}

public static void main(String[] args) {
   JFrame scFrame = new AnimationViewer();
   scFrame.setTitle("Circle");
   scFrame.setSize(400, 400);
   scFrame.setDefaultCloseOperation((JFrame.EXIT_ON_CLOSE));
   scFrame.setVisible(true);
}

}

AnimationViewer class:

public class AnimationViewer extends JFrame {
JButton jbtFlash = new JButton("Flash");
JButton jbtSteady = new JButton("Steady");
JPanel bPanel = new JPanel();
Sphere sphPanel = new Sphere();
Timer timer;

public AnimationViewer() {
   this.add(bPanel, BorderLayout.SOUTH);
   bPanel.add(jbtFlash);
   bPanel.setLayout(new GridLayout(1,2));
   bPanel.add(jbtSteady);

   this.add(sphPanel, BorderLayout.CENTER);

   timer = new Timer(100, new TimerListener());
   jbtSteady.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            sphPanel.choosesteady();
            timer.stop();
            sphPanel.repaint();
        }
    });
   jbtFlash.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            sphPanel.chooseflashinglights();
            timer.start();
        }
    });

}

class TimerListener implements ActionListener {
   public void actionPerformed(ActionEvent e) {
       sphPanel.repaint();
   }
}

}