Java红色爱心跳动代码
时间: 2024-06-09 22:03:27 浏览: 118
Java红色爱心跳动代码可以通过使用Java Swing组件库来实现。以下是一个示例代码:
```java
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class HeartBeat extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private JPanel panel;
private JLabel label;
public HeartBeat() {
panel = new JPanel();
label = new JLabel("❤");
label.setFont(new Font("Serif", Font.PLAIN, 50));
label.setForeground(Color.RED);
panel.add(label);
add(panel);
setTitle("Heart Beat");
setSize(300, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setResizable(false);
setVisible(true);
new Thread(new Runnable() {
boolean flag = true;
double size = 50;
@Override
public void run() {
while (flag) {
label.setFont(new Font("Serif", Font.PLAIN, (int) size));
if (size == 50) {
size = 60;
} else {
size = 50;
}
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
@Override
public void actionPerformed(ActionEvent e) {
// do nothing
}
public static void main(String[] args) {
new HeartBeat();
}
}
```
这个程序创建了一个带有红色爱心的窗口,在窗口中心显示一个红色的心形符号,并以一定的速度跳动。
阅读全文