用java写控制流水灯的定时装置,界面带一个定时按钮
时间: 2024-03-21 19:40:30 浏览: 22
好的,我可以为您提供一份Java代码,实现控制流水灯的定时装置,界面带有一个定时按钮。以下是代码:
```java
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
public class FlowingLights extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private static final int WIDTH = 400;
private static final int HEIGHT = 150;
private static final int DELAY = 1000; // 1秒钟
private static final int NUM_LIGHTS = 3;
private static final Color[] COLORS = { Color.RED, Color.GREEN, Color.BLUE };
private JLabel[] lights;
private Timer timer;
private int currentLight = 0;
private boolean isRunning = false;
public FlowingLights() {
super("Flowing Lights");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(new Dimension(WIDTH, HEIGHT));
setResizable(false);
setLocationRelativeTo(null); // 居中显示
JPanel contentPane = new JPanel();
contentPane.setLayout(new FlowLayout());
// 添加灯
lights = new JLabel[NUM_LIGHTS];
for (int i = 0; i < NUM_LIGHTS; i++) {
lights[i] = new JLabel();
lights[i].setPreferredSize(new Dimension(50, 50));
lights[i].setOpaque(true);
lights[i].setBackground(COLORS[i]);
contentPane.add(lights[i]);
}
// 添加定时按钮
JButton timerButton = new JButton("定时");
timerButton.setFont(new Font("宋体", Font.PLAIN, 24));
timerButton.addActionListener(this);
contentPane.add(timerButton);
setContentPane(contentPane);
setVisible(true);
}
// 开始流水灯
public void start() {
isRunning = true;
timer = new Timer(DELAY, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 关闭当前灯
lights[currentLight].setBackground(COLORS[currentLight]);
// 切换到下一个灯
currentLight = (currentLight + 1) % NUM_LIGHTS;
// 打开下一个灯
lights[currentLight].setBackground(Color.WHITE);
}
});
timer.start();
}
// 停止流水灯
public void stop() {
isRunning = false;
timer.stop();
// 关闭所有灯
for (int i = 0; i < NUM_LIGHTS; i++) {
lights[i].setBackground(COLORS[i]);
}
}
@Override
public void actionPerformed(ActionEvent e) {
if (isRunning) {
stop();
} else {
start();
}
}
public static void main(String[] args) {
new FlowingLights();
}
}
```
这个程序中,我们使用了Java的Swing框架来创建GUI界面。程序中有三个灯,颜色分别为红色、绿色和蓝色。我们使用了一个计时器来控制灯的开关。当用户点击“定时”按钮时,程序会切换流水灯的状态。如果流水灯正在运行,则停止;否则,开始运行。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)