package tst1;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
public class 滚动字幕 extends JFrame {
static Calendar cal;
static SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日HH:mm:ss,EEEE");
static String mDateTime;
static MovingMessagePanel messagePanel;
public 滚动字幕() {
Font font1 = new Font("华文行楷", Font.BOLD, 16);
Calendar cal = Calendar.getInstance();
mDateTime = formatter.format(cal.getTime());
messagePanel = new MovingMessagePanel(mDateTime);
messagePanel.setFont(font1);
messagePanel.setBackground(Color.pink);
messagePanel.setForeground(Color.black);
add(messagePanel);
}
public static void main(String[] args) {
滚动字幕 frame = new 滚动字幕();
frame.setTitle("滚动字幕");
frame.setLocationRelativeTo(null);// 置于屏幕中心
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(320, 120);
frame.setVisible(true);
Timer timer = new Timer(100, frame.new TimerListener());
timer.start();
}
static class MovingMessagePanel extends JPanel {
private String message = " ";
private int x = 0;
private int y = 50;
public MovingMessagePanel(String message) {
this.message = message;
}
public void paintComponent(Graphics g) {
super.paintComponent(g);// 确保在显示新的图画之前清空视图区域
if (x > getWidth()) {
x = -200;
}
x += 3;
this.message = mDateTime;
g.drawString(message, x, y);
}
}
class TimerListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
cal = Calendar.getInstance();
mDateTime = formatter.format(cal.getTime());
repaint();
}
}
}
javaSwing之滚动弹幕(以不断更新的时间作为滚动内容)
猜你喜欢
转载自blog.csdn.net/Mr_Clutch/article/details/108177549
今日推荐
周排行