Java实现滚动字幕

Java实现滚动字幕
import java.awt.BorderLayout;
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.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;

public class Demo extends JFrame
{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public Demo()
	{

		Font font1 = new Font("幼圆", Font.BOLD, 16);
		Calendar cal = Calendar.getInstance();
		SimpleDateFormat formatter = new SimpleDateFormat("EEEE,MMMMdd日,yyyy年 HH:mm:ss");
		String mDateTime = formatter.format(cal.getTime());

		MovingMessagePanel messagePanel = new MovingMessagePanel(mDateTime);
		messagePanel.setFont(font1);
		messagePanel.setBackground(Color.BLACK);
		messagePanel.setForeground(Color.PINK);

		add(messagePanel);

	}

	public static void main(String[] args)
	{

		Demo frame = new Demo();
		JLabel label = new JLabel("开始调试时间:X月X日     结束调试时间:X月X日");
		label.setBackground(Color.black);
		frame.setTitle("走马灯");
		frame.setLocationRelativeTo(null);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(320, 120);
		frame.setVisible(true);
		frame.add(label, BorderLayout.SOUTH);
	}

	static class MovingMessagePanel extends JPanel
	{

		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;
		private String message = " ";
		private int xCoordinate = 0;
		private int yCoordinate = 40;
		
		public MovingMessagePanel(String message)
		{
			this.message = message;
			Timer timer = new Timer(100, new TimerListener());
			timer.start();
		}

		public void paintComponent(Graphics g)
		{
			super.paintComponent(g);

			if (xCoordinate > getWidth())
			{
				xCoordinate = -100;
			}

			xCoordinate += 5;
			g.drawString(message, xCoordinate, yCoordinate);
		}

		class TimerListener implements ActionListener
		{

			public void actionPerformed(ActionEvent e)
			{
				repaint();
			}
		}
	}
}

class ZiMupanel extends JFrame { public ZiMupanel() { Container con = this.getContentPane(); // 设置窗口名称 this.setTitle("滚动字幕"); // 设置布局管理器为 null ,方便 面板定位,一般使用null 布局时,将窗口设置不可改变大小 this.setLayout(null); // 设置窗口位置和大小 this.setBounds(300, 300, 460, 330); // 设置窗口可见 this.setVisible(true); // 设置不可改变大小 this.setResizable(false); // 设置当点击窗口的关闭按钮时退出 // 设置此窗体关闭,滚动面板不关闭 this.setDefaultCloseOperation(DISPOSE_ON_CLOSE); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Panelfont1 p1 = new Panelfont1(); con.add(p1); p1.setBounds(15, 15, 175, 60); Panelfont2 p2 = new Panelfont2(); con.add(p2); p2.setBounds(195, 15, 250, 60); Panelneizi p3 = new Panelneizi(); con.add(p3); p3.setBounds(15, 80, 175, 80); Paneldirection p4 = new Paneldirection(); con.add(p4); p4.setBounds(195, 80, 250, 80); Panel5 p5 = new Panel5(); con.add(p5); p5.setBounds(15, 155, 430, 70); Panel6 p6 = new Panel6(); con.add(p6); p6.setBounds(15, 220, 430, 70); } } class Panelfont1 extends JPanel implements ActionListener{ public Panelfont1() { this.setBorder(BorderFactory.createTitledBorder("滚动字幕的文大小控制"));//设置 面板边框 this.setLayout(new GridLayout(1, 2)); bigfont = new JRadioButton("大体", false); smallfont = new JRadioButton("小体", true); bigfont.addActionListener(this); smallfont.addActionListener(this); ButtonGroup sexRadioButtonGroup = new ButtonGroup();// 创建一个选按钮组 sexRadioButtonGroup.add(bigfont);// 将单选按钮对象添加到按钮组 sexRadioButtonGroup.add(smallfont);// 将单选按钮对象添加到按钮组 //Enumeration elements = sexRadioButtonGroup.getElements();// 遍历按钮组的所有按钮 this.add(bigfont); this.add(smallfont); } public void actionPerformed(ActionEvent e) { if(bigfont.isSelected()){ fontSize = 50; }else { fontSize = 36; } Font font = new Font(fontName,Font.BOLD,fontSize);
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值