Java 对按钮使用操作侦听器
代码:JavaSphere类Java 对按钮使用操作侦听器,java,graphics,actionlistener,Java,Graphics,Actionlistener,代码:JavaSphere类 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)
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毫秒从橙色变为灰色
我知道这与动作监听器有关,但具体如何实现呢?我会做一些不同的事情
- 在我的drawing JPanel类中有一个字段,指示应该显示什么颜色
- 例如,如果只有两种颜色被交换,我会使用一个布尔字段,在我的计时器中交换它的值,然后根据它的值绘制颜色
- paintComponent方法将使用该字段(布尔值)来决定
使用哪种颜色g.setColor(…)
- 如果绘制了许多颜色,则该字段可以是颜色数组与数组中的int索引的组合。然后,我会根据需要增加索引,并使用paintComponent中的索引来决定使用哪种颜色进行绘制
- 我会在计时器的ActionListener中更改该字段——只需一次调用就可以更改它,而不是您对闪烁橙色和闪烁灰色的奇怪调用
- 我会用一个按钮启动计时器
- 我会用另一个按钮停止。这就是按钮的动作监听器所要做的,只需启动或停止计时器
- 您有很多额外的代码。我会这样做。在paintComponent方法中写入闪烁逻辑。然后只创建一个计时器。每100ms调用一次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();
}
}
}
好的,我只使用了两种颜色——灰色和橙色。我不明白的是,我到底应该在哪里编写代码,使其前后转换为橙色和灰色。它会在paintComponent方法下吗?@bob9123:paintComponent中的一个简单的if块,您可以根据布尔值选择要使用的颜色,它应该足够简单,可以进行编码,不会吗?非常感谢,我现在理解了。嘿,我已经尝试了一个多小时来解决这个问题,但是没有运气。是否可以使java程序以球体闪烁(在橙色/灰色之间)开始?