用Java创建一个小窗体,显示流浪地球字样,同时让字体样式,字体颜色以及面板颜色每三秒发生一次变化
时间: 2023-04-08 21:03:10 浏览: 105
可以使用Java Swing库来创建一个小窗体,并使用定时器来实现每三秒钟变化一次字体样式、字体颜色和面板颜色的效果。以下是示例代码:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Main {
private static final String[] FONT_NAMES = {"Arial", "Times New Roman", "Courier New"};
private static final int[] FONT_STYLES = {Font.PLAIN, Font.BOLD, Font.ITALIC};
private static final Color[] COLORS = {Color.RED, Color.GREEN, Color.BLUE};
public static void main(String[] args) {
JFrame frame = new JFrame("流浪地球");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 100);
JPanel panel = new JPanel();
panel.setBackground(Color.WHITE);
JLabel label = new JLabel("流浪地球");
label.setFont(new Font(FONT_NAMES[0], FONT_STYLES[0], 20));
label.setForeground(COLORS[0]);
panel.add(label);
frame.add(panel);
Timer timer = new Timer(3000, new ActionListener() {
int fontIndex = 0;
int styleIndex = 0;
int colorIndex = 0;
@Override
public void actionPerformed(ActionEvent e) {
fontIndex = (fontIndex + 1) % FONT_NAMES.length;
styleIndex = (styleIndex + 1) % FONT_STYLES.length;
colorIndex = (colorIndex + 1) % COLORS.length;
label.setFont(new Font(FONT_NAMES[fontIndex], FONT_STYLES[styleIndex], 20));
label.setForeground(COLORS[colorIndex]);
panel.setBackground(COLORS[colorIndex]);
}
});
timer.start();
frame.setVisible(true);
}
}
```