gpt4 book ai didi

java - 使用按钮的 Action 监听器

转载 作者:行者123 更新时间:2023-12-02 04:50:02 26 4
gpt4 key购买 nike

代码:Java Sphere 类

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

}

动画查看器类:

 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 监听器有关,但我到底如何实现它?

最佳答案

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

球体类:

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 类:

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

}

关于java - 使用按钮的 Action 监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29317575/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com