用Java创建一个小窗体,显示流浪地球字样,同时让流浪地球字体样式,字体颜色以及面板颜色每三秒发生一次变化
时间: 2023-04-08 18:03:01 浏览: 93
可以使用Java Swing库创建一个小窗体,并在窗体上添加一个JLabel组件来显示“流浪地球”字样。使用Timer类可以定时每三秒钟更改字体样式、字体颜色和面板颜色。以下是示例代码:
```
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MainFrame extends JFrame {
private JLabel label;
public MainFrame() {
setTitle("流浪地球");
setSize(300, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
label = new JLabel("流浪地球", SwingConstants.CENTER);
label.setFont(new Font("Arial", Font.BOLD, 24));
label.setForeground(Color.RED);
getContentPane().setBackground(Color.BLACK);
getContentPane().add(label, BorderLayout.CENTER);
Timer timer = new Timer(3000, new ActionListener() {
private int count = 0;
@Override
public void actionPerformed(ActionEvent e) {
switch (count % 3) {
case 0:
label.setFont(new Font("Times New Roman", Font.PLAIN, 24));
label.setForeground(Color.GREEN);
getContentPane().setBackground(Color.BLUE);
break;
case 1:
label.setFont(new Font("Courier New", Font.ITALIC, 24));
label.setForeground(Color.YELLOW);
getContentPane().setBackground(Color.RED);
break;
case 2:
label.setFont(new Font("Verdana", Font.BOLD | Font.ITALIC, 24));
label.setForeground(Color.BLUE);
getContentPane().setBackground(Color.GREEN);
break;
}
count++;
}
});
timer.start();
}
public static void main(String[] args) {
MainFrame frame = new MainFrame();
frame.setVisible(true);
}
}
```